PHP Luminova: Build Firebase Notification Payloads
Build Firebase Cloud Messaging payloads with notification content, custom data, and Android, APNs, or Web Push settings.
Use Luminova\Notifications\Models\Message to prepare the data passed to the Firebase messaging client. Choose a destination, set the content, and add any platform-specific configuration needed by your application.
Class Definition
- Full namespace:
Luminova\Notifications\Models\Message - This class is final.
Constants
| Constant | Type | Value |
|---|---|---|
DEFAULT | string | 'default' |
ANDROID | string | 'android' |
APN | string | 'apns' |
WEBPUSH | string | 'webpush' |
Build a Message
Use the setter methods or pass an array with the same configuration to Message::fromArray(). The example below prepares a topic message with an APNs badge and a custom data field.
use Luminova\Notifications\Models\Message;
$message = Message::fromArray([
'platforms' => [Message::APN],
'topic' => 'announcements',
'notification' => [
'title' => 'Service update',
'body' => 'The maintenance window is complete.',
],
'data' => ['category' => 'service'],
'apns' => [
'payload' => ['aps' => ['badge' => 1]],
],
]);Choose platform for one platform or platforms for a list. With neither field set, the constructor uses Message::DEFAULT. The messaging client applies Android, APNs, and Web Push configuration for this default. For direct control over the FCM request body, construct the message with raw => true and supply the Firebase target field and payload in that same array.
| Configuration key | Purpose |
|---|---|
platform, platforms | Select one platform identifier or a list of identifiers. The list takes precedence. |
token, tokens, topic, conditions | Set the destination for the corresponding messaging method. |
notification | Set display fields such as title, body, and image. |
data | Supply custom data as string keys and values. |
android, apns, webpush | Supply platform-specific configuration arrays. |
headers | Supply shared headers for APNs and Web Push. |
raw | Supply a complete FCM payload, including the destination. |
Methods
__construct
Initialize the message and its destination. The constructor reads token, tokens, topic, and conditions as destination fields. Put title, body, and image inside notification.
public function __construct(?array $options = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<string,mixed>|null | Message configuration. See Build a Message. Defaults to null. |
fromArray
Create a message from configuration.
public static function fromArray(array $configs): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$configs | array<string,mixed> | Message configuration. See Build a Message. |
Return:
self - A new message.
has
Check for a nonempty payload or metadata value.
public function has(string $name): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | Field name, such as android or data. |
Return:
bool - True when the selected value is nonempty.
hasAndroid
Check whether the Android configuration array is nonempty.
public function hasAndroid(): boolReturn:
bool - True when the array is nonempty.
hasWebpush
Check whether the Web Push configuration array is nonempty.
public function hasWebpush(): boolReturn:
bool - True when the array is nonempty.
hasApn
Check whether the APNs configuration array is nonempty.
public function hasApn(): boolReturn:
bool - True when the array is nonempty.
hasData
Check whether the custom data array is nonempty.
public function hasData(): boolReturn:
bool - True when the array is nonempty.
hasNotification
Check whether the notification section array is nonempty.
public function hasNotification(): boolReturn:
bool - True when the array is nonempty.
hasFcmOption
Check whether the FCM options array is nonempty.
public function hasFcmOption(): boolReturn:
bool - True when the array is nonempty.
add
Set a top-level payload field or write under a named section. When both the existing nested value and the supplied value are arrays, the method merges them recursively.
public function add(string $key, mixed $value, ?string $root = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
$root | string|null | Parent field for a nested value. Defaults to null, which writes directly to the payload. |
Return:
self - The current message.
addNested
Set a nested field using a dot-separated path such as apns.payload.aps.badge.
public function addNested(string $keys, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$keys | string | The dot-separated keys representing the nested structure. |
$value | mixed | The value to set at the specified nested location. It can be of any type (string, array, etc.). |
Return:
self - The current message.
addApns
Add APNs specific configuration key-value pair to notification payload.
public function addApns(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
Return:
self - The current message.
addWebpush
Add WebPush specific configuration key-value pair to the notification payload.
public function addWebpush(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
Return:
self - The current message.
addAndroid
Add Android specific configuration key-value pair to the notification payload.
public function addAndroid(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
Return:
self - The current message.
addData
Add a custom key-value pair to notification data object.
public function addData(string $key, string $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | string | The value to associate with the key. |
Return:
self - The current message.
addNotification
Set a field in the notification section. An empty string leaves the existing value in place.
public function addNotification(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
Return:
self - The current message.
setNotification
Merge fields into the notification section. Incoming values replace existing values for the same keys.
public function setNotification(array $notification): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$notification | array<string, mixed> | The notification payload object. |
Return:
self - The current message.
setFcmOptions
Merge values into the payload’s fcm_options section.
public function setFcmOptions(array $options): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<string, mixed> | The FCM options. |
Return:
self - The current message.
setHeaders
Merge headers used by the message’s APNs and Web Push configurations.
public function setHeaders(array $headers): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$headers | array<string,mixed> | Header names and values. |
Return:
self - The current message.
setTitle
Set the display title for notification.
public function setTitle(string $title): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$title | string | The notification title. |
Return:
self - The current message.
setBody
Set the display body for notification.
public function setBody(string $body): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$body | string | Notification message body. |
Return:
self - The current message.
setImageUrl
Set the image URL for notification.
public function setImageUrl(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The image url to set. |
Return:
self - The current message.
setIcon
Set the icon for the notification.
public function setIcon(string $icon): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$icon | string | The notification icon. |
Return:
self - The current message.
setSound
Set the sound for the notification.
public function setSound(string $sound): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$sound | string | Notification sound. |
Return:
self - The current message.
setVibration
Set the vibration pattern for the notification.
public function setVibration(array $vibrate): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$vibrate | array<array-key, mixed> | The vibrate pattern e.g. [200, 100, 200]. |
Return:
self - The current message.
setTag
Set a tag for the notification.
public function setTag(string $tag): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$tag | string | The notification tag. |
Return:
self - The current message.
setColor
Set a color for the notification.
public function setColor(string $color): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$color | string | The notification color. |
Return:
self - The current message.
setAnalytic
Set the analytic label for the notification.
public function setAnalytic(string $analytic): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$analytic | string | Set analytic label. |
Return:
self - The current message.
setPriority
Set the notification priority.
public function setPriority(string $priority): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$priority | string | The notification priority (e.g normal). |
Return:
self - The current message.
setTtl
Set TTL for the notification.
public function setTtl(string $ttl): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$ttl | string | The ttl (e.g. 3600s). |
Return:
self - The current message.
setLink
Set a link to open when notification is clicked.
public function setLink(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The notification action url. |
Return:
self - The current message.
setClickAction
Set click action, an activity with a matching intent filter is launched when a user clicks on the notification.
public function setClickAction(string $action): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$action | string | The notification intent action. |
Return:
self - The current message.
setBadgeCount
Sets the number of badge count this notification will add.
public function setBadgeCount(int $count): selfThis may be displayed as a badge count for launchers that support badging.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$count | int | The number of badge to add for this notification. |
Return:
self - The current message.
setPackage
Sets package restriction, the package name of your application where the registration token must match in order to receive the message.
public function setPackage(string $package): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$package | string | The notification package restriction (e.g: com.app.name.foo). |
Return:
self - The current message.
isRaw
Determine if notification should be sent raw from payload array.
public function isRaw(): boolReturn:
bool - Return true if should send notification as raw, otherwise false.
setRaw
Set raw flag, to send notification raw from payload array, instead of building notification.
public function setRaw(bool $raw = true): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$raw | bool | Should send notification as raw, otherwise false. |
Return:
self - The current message.
setTopic
Set the notification topic to use when called channel method.
public function setTopic(string $topic): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$topic | string | The notification topic name. |
Return:
self - The current message.
setConditions
Set the notification topic conditional expression to use when called condition method.
public function setConditions(string $conditions): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$conditions | string | The conditional expression. |
Return:
self - The current message.
setTokens
Set the notification device tokens to use when called broadcast method.
public function setTokens(array $tokens): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$tokens | array<int, string> | The device notification tokens. |
Return:
self - The current message.
setToken
Set the notification device token to use when called send method.
public function setToken(string $token): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$token | string | The notification device token. |
Return:
self - The current message.
setPlatform
Set a platform by adding its identifier to the selected platforms. To start with only one platform, use the platform constructor option.
public function setPlatform(string $platform): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$platform | string | A platform constant, such as Message::APN. |
Return:
self - The current message.
getTokens
Get the array of notification device tokens.
public function getTokens(): arrayReturn:
array<array-key, mixed> - Return the array of notification device tokens.
getConditions
Get notification topic conditional expression.
public function getConditions(): stringReturn:
string - Return notification topic expression.
getToken
Get notification token.
public function getToken(): stringReturn:
string - Return notification token.
getPlatforms
Read the platform identifiers selected for the message.
public function getPlatforms(): arrayReturn:
array<int,string> - The selected platform identifiers.
getPriority
Get notification priority.
public function getPriority(): stringReturn:
string - Return notification priority.
getTitle
Get notification title.
public function getTitle(): stringReturn:
string - Return notification title.
getBody
Get notification body.
public function getBody(): stringReturn:
string - Return notification body.
getData
Get notification custom data.
public function getData(): arrayReturn:
array<string, mixed> - Returns the notification data.
getImageUrl
Get notification image url.
public function getImageUrl(): stringReturn:
string - Return notification image url.
getTopic
Get notification channel topic.
public function getTopic(): stringReturn:
string - Returns notification channel topic.
getAnalytic
Get notification analytic label.
public function getAnalytic(): stringReturn:
string - Return notification analytics label.
get
Read a field from the notification section.
public function get(string $key, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Notification field to read. |
$default | mixed | Fallback value. Defaults to null. |
Return:
mixed - The field value or the fallback if the field is absent or null.
toArray
Export the message payload to an array. The standard payload export describes the message content and platform configurations.
public function toArray(): arrayReturn:
array<string,mixed> - The generated payload.