PHP Luminova: Firebase Notification Message Model
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.
| Constant | Visibility | Type | Value |
|---|---|---|---|
DEFAULT | public | int | 1 |
ANDROID | public | int | 2 |
APN | public | int | 3 |
WEBPUSH | public | int | 4 |
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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$setter | array|null | An 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:
- Learn more about Notification fields
- Learn more about APNs specific configurations.
- Learn more about Android specific configurations
- Learn more about WebPush specific configurations
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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The value to associate with the key. |
$root | string|null | Optional 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): 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 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | mixed | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to add. |
$value | string | The 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<string,mixed> $notification): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$notification | array<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<string,mixed> $options): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$title | string | The notification title. |
Return Value:
self - Return notification message model instance for method chaining.
setBody
Set the display body for notification.
public setBody(string $body): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$body | string | Notification message body. |
Return Value:
self - Return notification message model instance for method chaining.
setImageUrl
Set the image URL for notification.
public setImageUrl(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$icon | string | The notification icon. |
Return Value:
self - Return notification message model instance for method chaining.
setSound
Set the sound for the notification.
public setSound(string $sound): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$sound | string | Notification sound. |
Return Value:
self - Return notification message model instance for method chaining.
setVibration
Set the vibration pattern for the notification.
public setVibration(array $vibrate): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$vibrate | array | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$tag | string | The notification tag. |
Return Value:
self - Return notification message model instance for method chaining.
setColor
Set a color for the notification.
public setColor(string $color): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$color | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$analytic | string | Set analytic label. |
Return Value:
self - Return notification message model instance for method chaining.
setPriority
Set the notification priority.
public setPriority(string $priority): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$priority | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$ttl | string | The ttl (e.g. 3600s). |
Return Value:
self - Return notification message model instance for method chaining.
setLink
Set a link to open when notification is clicked.
public setLink(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$action | string | The notification intent action. |
Return Value:
self - Return notification message model instance for method chaining.
setBadgeCount
Sets the number of badge count this notification will add.