PHP Luminova: Send Firebase Push Notifications from Luminova
Build Firebase Cloud Messaging payloads for devices, topics, and topic conditions, including platform-specific options.
Configure a Firebase project and keep its service account credentials outside the public directory. Start with one test registration token or topic. Set validateOnly to true when you want Firebase to validate a payload before delivery. See the Send Firebase Cloud Messages with Luminova.
Requirements
- Firebase Account: Create a Firebase account and a project.
- Firebase SDK: Run
composer require kreait/firebase-phpfrom the project root.
1. Create a Firebase Project
- Go to the Firebase Console.
- Click "Add Project" and follow the setup wizard.
2. Add Firebase to Your App
- Configure your app or website to receive messages. Copy a registration token from a test device for the first example.
- Save the service account JSON in
/writeable/credentials/. Keep it out of version control and outside the public document root.
Initialization
Create one Notification instance. It uses ServiceAccount.json by default. Pass the filename to the constructor if you saved the credentials under a different name.
use Luminova\Notifications\Firebase\Notification;
// Instantiate the Notification class
$notification = new Notification();Simple Configuration
The following examples demonstrate how to send notifications using a simple configuration array.For more advanced use cases, consider using the Message model or Full Array configuration to create a notification payload object.
Use a fresh copy of this base configuration for each example. Each send method expects its own target.
Example Configuration
$simpleConfig = [
'title' => "New Notification Title",
'body' => "This is a new notification message",
'image' => 'https://example.com/assets/images/foo.png'
];Sending Notifications
To a Single Device Token
Send a notification to a single device token using the simple configuration array.
$config = $simpleConfig;
$config['token'] = 'DEVICE_TOKEN';
$notify = $notification->send($config);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}To Multiple Device Tokens
Send a notification to multiple device tokens using the simple configuration array.
$config = $simpleConfig;
$config['tokens'] = [
'DEVICE_TOKEN_1', 'DEVICE_TOKEN_2', // Add more tokens as needed
];
$notify = $notification->broadcast($config);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}To a Topic
Send a notification to all devices subscribed to a specific topic using the simple configuration array.
$config = $simpleConfig;
$config['topic'] = 'NEW_PRODUCT_AVAILABLE';
$notify = $notification->channel($config);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}To a Conditional Topic
Send a notification to devices that match certain conditions based on topic subscriptions.
$config = $simpleConfig;
$config['conditions'] = "'ADMINS' in topics && !('USERS' in topics)";
$notify = $notification->condition($config);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}Configuration Model
The Message model allows you to configure notifications with various options to suit different platforms and use cases.Below is an example of how to create and configure a Message object. Refer to the official documentation for all available methods.
Use a fresh Message configured as shown below for each target example. The custom fields that follow illustrate payload structure separately.
Basic Configuration
use Luminova\Notifications\Models\Message;
$message = (new Message())->setImageUrl('https://example.com/assets/images/foo.png')
->setTitle('Notification Title')
->setBody('Notification Body')
->setClickAction('MainActivity')
->setBadgeCount(1)
->setColor('#eeeeee')
->setVibration([200, 100, 200])
->setSound('dingbong.aiff')
->setPlatform(Message::ANDROID)
->setPackage('com.example.foo.bar')
->setIcon('large')
->setLink('https://example.com/')
->setPriority('normal')
->setTag('tag-name')
->setTtl('3s')
->setAnalytic('analytic-id');In this example, the Message object is configured with properties such as title, body, image, clickAction, and platform-specific settings.
Adding More Configuration
You can add custom data and platform-specific configurations to the Message object using various methods.
Adding Custom Data
To add key-value pairs to the data payload of the notification.
$message->addData('data-key', 'data-key-value');This will add a custom data field to the notification.
{
"data": {
"data-key": "data-key-value"
}
}Adding Android-Specific Configuration
To add Android-specific configuration options:
$message->addAndroid('foo-key', 'foo-key-value');This will add the specified key-value pair under the Android configuration.
{
"android": {
"foo-key": "foo-key-value"
}
}Adding APNs-Specific Configuration
To add APNs (Apple Push Notification service) specific configuration.
$message->addApns('foo-key', 'foo-key-value');This will add the specified key-value pair under the APNs configuration:
{
"apns": {
"foo-key": "foo-key-value"
}
}Adding WebPush-Specific Configuration
To add WebPush specific configuration options:
$message->addWebpush('foo-key', 'foo-key-value');This will add the specified key-value pair under the WebPush configuration:
{
"webpush": {
"foo-key": "foo-key-value"
}
}Adding Notification-Specific Configuration
To add general notification-specific configuration options:
$message->addNotification('foo-key', 'foo-key-value');This will add the specified key-value pair under the notification configuration.
{
"notification": {
"foo-key": "foo-key-value"
}
}Advanced Usages
You can also add nested configuration options to target specific sub-properties within the configuration objects.
Adding Nested Configuration for General Use
$message->addNested('foo.bar.baz.boz.bra', 'bra value');This will add the nested configuration as follows:
{
"foo": {
"bar": {
"baz": {
"boz": {
"bra": "bra value"
}
}
}
}
}Adding Nested Configuration for Android
$message->addNested('android.light_settings.light_on_duration', '3.5s');This will add the nested configuration to the Android settings:
{
"android": {
"light_settings": {
"light_on_duration": "3.5s"
}
}
}Adding Nested Configuration for APNs
$message->addNested('apns.payload.aps.category', 'GAME_INVITATION');This will add the nested configuration to the APNs payload:
{
"apns": {
"payload": {
"aps": {
"category": "GAME_INVITATION"
}
}
}
}These examples demonstrate how to add various configurations to your Message object, enabling you to send platform specific notifications.
Sending Notifications from the Model
The following examples demonstrate how to send notifications using the Message model.This approach provides a more structured and customizable way to configure and send notifications compared to using simple configuration arrays.
Sending Notification to a Single Device Token
To send a notification to a single device token, set the token on the Message object and use the send method.
$message->setToken('DEVICE_TOKEN');
$notify = $notification->send($message);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}Sending Notification to a Topic
To send a notification to all devices subscribed to a specific topic, set the topic on the Message object and use the channel method.
$message->setTopic('NEW_PRODUCT_AVAILABLE');
$notify = $notification->channel($message);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}Sending Notification to Multiple Device Tokens
To broadcast a notification to multiple device tokens, set the tokens on the Message object and use the broadcast method.
$message->setTokens([
'Token1', 'Token2', // Add more tokens as needed
]);
$notify = $notification->broadcast($message);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}Sending Notification with Conditional Topic
To send a notification based on conditions that match specific topics, set the conditions on the Message object and use the condition method.
$message->setConditions("'ADMINS' in topics && !('USERS' in topics)");
$notify = $notification->condition($message);
if ($notify->isDone()) {
echo 'Firebase returned a send report.';
} else {
var_dump($notify->getReport());
}Full Array Configuration
This section provides a comprehensive structure for configuring notification payloads using an array.This method allows for detailed customization of the notification for different platforms and scenarios.
Platform Key Values
1- Default notification payload.2- Android notification payload.3- APNs (Apple Push Notification service) notification payload.4- WebPush notification payload.
Example Configuration Array
$config = [
'platform' => 1, // Default platform
'raw' => false, // Send raw notification payload
'topic' => 'Name of the topic', // Topic name for broadcasting
'conditions' => 'Topic condition expressions', // Conditions for topic targeting
'token' => 'Single token', // Single device token
'tokens' => [
'Token1', 'Token2', // Multiple device tokens
],
'data' => [
'key1' => 'data 1',
// Additional data fields
],
'notification' => [
'title' => "Notification title", // Title of the notification
'body' => "Notification message", // Body text of the notification
'image' => 'Optional image url for notification' // URL for the notification image
],
'apns' => [], // APNs-specific configurations
'android' => [], // Android-specific configurations
'webpush' => [], // WebPush-specific configurations
'fcm_options' => [] // Additional FCM options
];Important NotesMutually Exclusive Keys: You cannot use
topic,conditions,token, andtokenskeys together in one configuration.Select the key based on the target recipient(s) of the notification.Additionally if
rawis enabled, you don't need to includeplatformkey in your array.
- topic: For sending notifications to all subscribers of a topic.
- conditions: For sending notifications based on complex conditions involving multiple topics.
- token: For sending a notification to a single device.
- tokens: For broadcasting a notification to multiple devices.
Summary
Using the full array configuration allows for control over your notifications, ensuring they are targeted to the appropriate platform and recipients.Adjust the array to fit your specific needs, making sure to adhere to the mutually exclusive key restrictions for targeting your audience accurately.