Luminova Framework

PHP Luminova: Firebase Notification Message Model

Last updated: 2026-03-17 07:24:37

Build Firebase notification message payload configurations using the Luminova Message model.

The notification Message model enables you to build a notification payload efficiently. It provides various methods to add custom fields to the notification payload, to customize your application requirements and platform configuration settings.


Class Definition

  • Class namespace: Luminova\Notifications\Models\Message
  • This class is marked as final and can't be subclassed
  • This class is a Final class

Constants

This constants represent platform specific notifications configuration.

ConstantVisibilityTypeValue
DEFAULTpublicint1
ANDROIDpublicint2
APNpublicint3
WEBPUSHpublicint4

Methods

Example Usage

<?php
use Luminova\Notifications\Models\Message;

$message = new Message();
$message->setPlatform(Message::ANDROID)
  ->setRaw(false)
  ->setToken('your_single_notification_token')
  ->setTopic('your_topic')
  ->setTokens(['token1', 'token2'])
  ->add('foo', 'value', 'android')
  ->add('bar', [
    'baz' => 1,
    'bra' => 'foo'
  ])
  ->addNested('foo.bar.baz', 'baz value')
  ->addData('key', 'value1')
  ->addAndroid('key', 'value')
  ->addApns('key', 'value')
  ->addWebpush(['key' => 'value'])
  ->setFcmOptions(['key' => 'value'])
  ->setNotification([
      'title' => 'Your Notification Title',
      'body' => 'Your Notification Message',
      'image' => 'https://example.com/image.png'
  ]);

APNs Payload Example

<?php 
use Luminova\Notifications\Firebase\Notification;
use Luminova\Notifications\Models\Message;
use Luminova\Exceptions\LuminovaException;

$message = (new Message())->setPlatform(Message::APN)
    ->setTopic('FORUM_CHAT')
    ->addNested('apns.payload.aps', [
        'badge' => 1,
        'mutable-content' => 1,
        'content-available' => 1,
        'sound' => 'default'
    ])
    ->add('data', [
        'type' => 'chat',
        'senderId' => 'some-user-id',
        'imageLink' => 'https://example.com/assets/images/picture.jpg'
    ])
    ->setImageUrl('https://example.com/image.png')
    ->setNotification([
        'title' => 'Hello Forum!',
        'body' => 'Am new here, below is my picture.'
    ]);

try{
    $notification = (new Notification())->channel($message);

    if(!$notification->isDone() && is_array($notification->getReport())){
        print_r($notification->getReport());
    }

    echo 'Notification sent';
}catch(LuminovaException $e){
    logger('exception', $e->getMessage());
}

constructor

Initialize new message model, you can optionally pass an array to build your notification payload from.

public __construct(?array $setter = null): mixed

Parameters:

ParameterTypeDescription
$setterarray|nullAn optional array to initialize model from.

Setter Fields

  • platform (int) Notification specific platform (default: 1).
  • raw bool - Send notification from your build payload or array without modifying.
  • token string - Single notification token, (required) if you are sending notification to a single device.
  • topic string - Single notification topic, (required) if you are sending notification to a topic/channel.
  • tokens array<int,string> - Multiple notification tokens, (required) if you are sending notification to multiple device tokens.
  • data array<string,mixed> - Optional data to send with the notification.
  • android array<string,mixed> - Android specific configuration.
  • apns array<string,mixed> - APNs specific configuration.
  • webpush array<string,mixed> - WebPush specific configuration.
  • fcm_options array<string,mixed> - Optional firebase configurations.
  • notification array<string,mixed> - Notification payload information.
      • title (string) Notification title.
      • body (string) Notification message body.
      • image (string) Notification image URL.

Setter Fields Array Example

<?php 
$setter = [
  'platform' => 1, // Default for all platforms
  'raw' => false, // Default no raw payload
  'token' => 'your_single_notification_token',
  'topic' => 'your_topic',
  'tokens' => ['token1', 'token2'],
  'data' => ['key1' => 'value1', 'key2' => 'value2'],
  'android' => [
    'key' => 'value'
  ],
  'apns' => [
    'key' => 'value'
  ],
  'webpush' => [
    'key' => 'value'
  ],
  'fcm_options' => [
    'key' => 'value'
  ],
  'notification' => [
    'title' => 'Your Notification Title',
    'body' => 'Your Notification Message',
    'image' => 'https://example.com/image.png'
  ]
];

See Also:


add

The add method allows you to add key-value pairs to the notification payload. It supports nested payload structures and can merge values recursively if needed. If the root key is specified, the method ensures that the payload under the root is an array before adding or merging the key-value pair.

public add(string $key, mixed $value, string|null $root = null): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe value to associate with the key.
$rootstring|nullOptional root key for nested payloads, if NUll, the key will be store in payload root instead. and replace any existing key value.

Return Value:

self - Return notification message model instance for method chaining.

Example

<?php
use Luminova\Notifications\Models\Message;

$message = new Message();

// Add top-level key-value pair
$message->add('title', 'New Message');

// Add nested key-value pair
$message->add('body', 'You have a new message', 'notification');

// Add and merge array values
$message->add('data', ['key1' => 'value1'], 'extra');
$message->add('data', ['key2' => 'value2'], 'extra');

Example payload after additions

[
    'title' => 'New Message',
    'notification' => [
        'body' => 'You have a new message'
    ],
    'extra' => [
        'data' => [
            'key1' => 'value1',
            'key2' => 'value2'
        ]
    ]
]

addNested

Add a nested configuration key-value pair to the payload using dot (.) notation string as a delimiter to represent the nested structure of keys.

The addNested method is designed to enhance the addition of values to deeply nested structures within the payload. It parses a dot notation string to determine the nested keys and sets the provided value at the correct depth in the payload.

public 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 Value:

self - Return notification message model instance for method chaining. Returns the updated instance of the class, allowing method chaining.

Example

Add a value to a deeply nested structure using dot notation.

<?php
use Luminova\Notifications\Models\Message;

$message = new Message();
$message->addNested('notification.alerts.urgent', 'Immediate attention required');

Example payload after addition.

[
  'notification' => [
      'alerts' => [
          'urgent' => 'Immediate attention required'
      ]
  ]
];

Chain multiple additions

$message->addNested('notification.alerts.low', 'Routine checkup')
  ->addNested('notification.alerts.medium', 'Scheduled maintenance');

Example payload after chaining additions.

[
  'notification' => [
      'alerts' => [
          'urgent' => 'Immediate attention required',
          'low' => 'Routine checkup',
          'medium' => 'Scheduled maintenance'
      ]
  ]
];

addApns

Add APNs specific configuration key-value pair to notification payload.

public addApns(string $key, mixed $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe value to associate with the key.

Return Value:

self - Return notification message model instance for method chaining.


addWebpush

Add WebPush specific configuration key-value pair to the notification payload.

public addWebpush(string $key, mixed $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe value to associate with the key.

Return Value:

self - Return notification message model instance for method chaining.


addAndroid

Add Android specific configuration key-value pair to the notification payload.

public addAndroid(string $key, mixed $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuemixedThe value to associate with the key.

Return Value:

self - Return notification message model instance for method chaining.


addData

Add a custom key-value pair to notification data object.

public addData(string $key, string $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuestringThe value to associate with the key.

Return Value:

self - Return notification message model instance for method chaining.


addNotification

Add a key-value pair to the notification object.

public addNotification(string $key, string $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to add.
$valuestringThe value to associate with the key.

Return Value:

self - Return notification message model instance for method chaining.


setNotification

Set array of key-value pair to the notification object.

public setNotification(array&lt;string,mixed&gt; $notification): self

Parameters:

ParameterTypeDescription
$notificationarray<string,mixed>The notification payload object.

Return Value:

self - Return notification message model instance for method chaining.


setFcmOptions

Set FCM options. array of key-value pair to the fcm_options object.

public setFcmOptions(array&lt;string,mixed&gt; $options): self

Parameters:

ParameterTypeDescription
$optionsarray<string,mixed>The FCM options.

Return Value:

self - Return notification message model instance for method chaining.


setTitle

Set the display title for notification.

public setTitle(string $title): self

Parameters:

ParameterTypeDescription
$titlestringThe notification title.

Return Value:

self - Return notification message model instance for method chaining.


setBody

Set the display body for notification.

public setBody(string $body): self

Parameters:

ParameterTypeDescription
$bodystringNotification message body.

Return Value:

self - Return notification message model instance for method chaining.


setImageUrl

Set the image URL for notification.

public setImageUrl(string $url): self

Parameters:

ParameterTypeDescription
$urlstringThe image url to set.

Return Value:

self - Return notification message model instance for method chaining.


setIcon

Set the icon for the notification.

public setIcon(string $icon): self

Parameters:

ParameterTypeDescription
$iconstringThe notification icon.

Return Value:

self - Return notification message model instance for method chaining.


setSound

Set the sound for the notification.

public setSound(string $sound): self

Parameters:

ParameterTypeDescription
$soundstringNotification sound.

Return Value:

self - Return notification message model instance for method chaining.


setVibration

Set the vibration pattern for the notification.

public setVibration(array $vibrate): self

Parameters:

ParameterTypeDescription
$vibratearrayThe vibrate pattern e.g. [200, 100, 200].

Return Value:

self - Return notification message model instance for method chaining.


setTag

Set a tag for the notification.

public setTag(string $tag): self

Parameters:

ParameterTypeDescription
$tagstringThe notification tag.

Return Value:

self - Return notification message model instance for method chaining.


setColor

Set a color for the notification.

public setColor(string $color): self

Parameters:

ParameterTypeDescription
$colorstringThe notification color.

Return Value:

self - Return notification message model instance for method chaining.


setAnalytic

Set the analytic label for the notification.

public setAnalytic(string $analytic): self

Parameters:

ParameterTypeDescription
$analyticstringSet analytic label.

Return Value:

self - Return notification message model instance for method chaining.


setPriority

Set the notification priority.

public setPriority(string $priority): self

Parameters:

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

Return Value:

self - Return notification message model instance for method chaining.


setTtl

Set TTL for the notification.

public setTtl(string $ttl): self

Parameters:

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

Return Value:

self - Return notification message model instance for method chaining.


Set a link to open when notification is clicked.

public setLink(string $url): self

Parameters:

ParameterTypeDescription
$urlstringThe notification action url.

Return Value:

self - Return notification message model instance for method chaining.


setClickAction

Set click action, an activity with a matching intent filter is launched when a user clicks on the notification.

public setClickAction(string $action): self

Parameters:

ParameterTypeDescription
$actionstringThe notification intent action.

Return Value:

self - Return notification message model instance for method chaining.


setBadgeCount