Luminova Framework

PHP Luminova: Build Firebase Notification Payloads

Last updated: 2026-11-28 20:57:12

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

ConstantTypeValue
DEFAULTstring'default'
ANDROIDstring'android'
APNstring'apns'
WEBPUSHstring'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 keyPurpose
platform, platformsSelect one platform identifier or a list of identifiers. The list takes precedence.
token, tokens, topic, conditionsSet the destination for the corresponding messaging method.
notificationSet display fields such as title, body, and image.
dataSupply custom data as string keys and values.
android, apns, webpushSupply platform-specific configuration arrays.
headersSupply shared headers for APNs and Web Push.
rawSupply 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:

ParameterTypeDescription
$optionsarray<string,mixed>&#124;nullMessage configuration. See Build a Message. Defaults to null.

fromArray

Create a message from configuration.

public static function fromArray(array $configs): self

Parameters:

ParameterTypeDescription
$configsarray<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): bool

Parameters:

ParameterTypeDescription
$namestringField 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(): bool

Return:

bool - True when the array is nonempty.


hasWebpush

Check whether the Web Push configuration array is nonempty.

public function hasWebpush(): bool

Return:

bool - True when the array is nonempty.


hasApn

Check whether the APNs configuration array is nonempty.

public function hasApn(): bool

Return:

bool - True when the array is nonempty.


hasData

Check whether the custom data array is nonempty.

public function hasData(): bool

Return:

bool - True when the array is nonempty.


hasNotification

Check whether the notification section array is nonempty.

public function hasNotification(): bool

Return:

bool - True when the array is nonempty.


hasFcmOption

Check whether the FCM options array is nonempty.

public function hasFcmOption(): bool

Return:

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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe value to associate with the key.
$rootstring&#124;nullParent 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): self

Parameters:

ParameterTypeDescription
$keysstringThe dot-separated keys representing the nested structure.
$valuemixedThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuestringThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe 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): self

Parameters:

ParameterTypeDescription
$notificationarray<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): self

Parameters:

ParameterTypeDescription
$optionsarray<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): self

Parameters:

ParameterTypeDescription
$headersarray<string,mixed>Header names and values.

Return:

self - The current message.


setTitle

Set the display title for notification.

public function setTitle(string $title): self

Parameters:

ParameterTypeDescription
$titlestringThe notification title.

Return:

self - The current message.


setBody

Set the display body for notification.

public function setBody(string $body): self

Parameters:

ParameterTypeDescription
$bodystringNotification message body.

Return:

self - The current message.


setImageUrl

Set the image URL for notification.

public function setImageUrl(string $url): self

Parameters:

ParameterTypeDescription
$urlstringThe image url to set.

Return:

self - The current message.


setIcon

Set the icon for the notification.

public function setIcon(string $icon): self

Parameters:

ParameterTypeDescription
$iconstringThe notification icon.

Return:

self - The current message.


setSound

Set the sound for the notification.

public function setSound(string $sound): self

Parameters:

ParameterTypeDescription
$soundstringNotification sound.

Return:

self - The current message.


setVibration

Set the vibration pattern for the notification.

public function setVibration(array $vibrate): self

Parameters:

ParameterTypeDescription
$vibratearray<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): self

Parameters:

ParameterTypeDescription
$tagstringThe notification tag.

Return:

self - The current message.


setColor

Set a color for the notification.

public function setColor(string $color): self

Parameters:

ParameterTypeDescription
$colorstringThe notification color.

Return:

self - The current message.


setAnalytic

Set the analytic label for the notification.

public function setAnalytic(string $analytic): self

Parameters:

ParameterTypeDescription
$analyticstringSet analytic label.

Return:

self - The current message.


setPriority

Set the notification priority.

public function setPriority(string $priority): self

Parameters:

ParameterTypeDescription
$prioritystringThe notification priority (e.g normal).

Return:

self - The current message.


setTtl

Set TTL for the notification.

public function setTtl(string $ttl): self

Parameters:

ParameterTypeDescription
$ttlstringThe ttl (e.g. 3600s).

Return:

self - The current message.


Set a link to open when notification is clicked.

public function setLink(string $url): self

Parameters:

ParameterTypeDescription
$urlstringThe 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): self

Parameters:

ParameterTypeDescription
$actionstringThe notification intent action.

Return:

self - The current message.


setBadgeCount

Sets the number of badge count this notification will add.

public function setBadgeCount(int $count): self

This may be displayed as a badge count for launchers that support badging.

Parameters:

ParameterTypeDescription
$countintThe 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): self

Parameters:

ParameterTypeDescription
$packagestringThe 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(): bool

Return:

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): self

Parameters:

ParameterTypeDescription
$rawboolShould 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): self

Parameters:

ParameterTypeDescription
$topicstringThe 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): self

Parameters:

ParameterTypeDescription
$conditionsstringThe conditional expression.

Return:

self - The current message.


setTokens

Set the notification device tokens to use when called broadcast method.

public function setTokens(array $tokens): self

Parameters:

ParameterTypeDescription
$tokensarray<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): self

Parameters:

ParameterTypeDescription
$tokenstringThe 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): self

Parameters:

ParameterTypeDescription
$platformstringA platform constant, such as Message::APN.

Return:

self - The current message.


getTokens

Get the array of notification device tokens.

public function getTokens(): array

Return:

array<array-key, mixed> - Return the array of notification device tokens.


getConditions

Get notification topic conditional expression.

public function getConditions(): string

Return:

string - Return notification topic expression.


getToken

Get notification token.

public function getToken(): string

Return:

string - Return notification token.


getPlatforms

Read the platform identifiers selected for the message.

public function getPlatforms(): array

Return:

array<int,string> - The selected platform identifiers.


getPriority

Get notification priority.

public function getPriority(): string

Return:

string - Return notification priority.


getTitle

Get notification title.

public function getTitle(): string

Return:

string - Return notification title.


getBody

Get notification body.

public function getBody(): string

Return:

string - Return notification body.


getData

Get notification custom data.

public function getData(): array

Return:

array<string, mixed> - Returns the notification data.


getImageUrl

Get notification image url.

public function getImageUrl(): string

Return:

string - Return notification image url.


getTopic

Get notification channel topic.

public function getTopic(): string

Return:

string - Returns notification channel topic.


getAnalytic

Get notification analytic label.

public function getAnalytic(): string

Return:

string - Return notification analytics label.


get

Read a field from the notification section.

public function get(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringNotification field to read.
$defaultmixedFallback 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(): array

Return:

array<string,mixed> - The generated payload.