DEV Community

Cover image for Using Firebase Cloud Messaging(FCM) for Push Notifications in PHP: A Complete Guide
Ramin Omrani
Ramin Omrani

Posted on • Edited on

Using Firebase Cloud Messaging(FCM) for Push Notifications in PHP: A Complete Guide

Push notifications are an essential tool for engaging users and keeping them informed about updates, messages, and other important events. Firebase Cloud Messaging (FCM) is a cross-platform solution that allows you to send notifications to web, Android, and iOS devices for free. In this guide, we'll use the lkaybob/php-fcm-v1 package to set up FCM and send push notifications with PHP.

To get up and running with FCM basics (client and server setup) you can watch this great tutorial on youtube : https://www.youtube.com/watch?v=iz5arafmatc

Now how do we send a push notification with lkaybob/php-fcm-v1 after setting up our FCM client and server?

Here is an example with comments explaining how the code snippet works :

<?php
require_once __DIR__ . '/vendor/autoload.php';

use phpFCMv1\Client;
use phpFCMv1\Notification;
use phpFCMv1\Recipient;

// Client instance should be created with path to service account key file
$client = new Client("/path/to/service_account.json");
$recipient = new Recipient();

// Either Notification or Data (or both) instance should be created
$notification = new Notification();

// Recipient could accept individual device token,
// the name of topic, and conditional statement
$recipient->setSingleREcipient('DEVICE_TOKEN');

// Setup Notificaition title and bod
$notification->setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY');

//if you have extra data to send to the client
if(!empty($extraData)) {
    //create a data object
    $data = new Data();

    //add extra data to the payload
    $data->setPayload(["data" => $dataArray]);

    // Build FCM request payload
    $client->build($recipient, $notification, $data);
} else {
    $client->build($recipient, $notification);
}

// You can check the result
// If successful, true will be returned
// If not, error message will be returned
$result = $client->fire();

if(!$result) {
    //do something in case of error
} else {
    //do something in case of success
}
Enter fullscreen mode Exit fullscreen mode

By following the steps in the example above, you can successfully integrate Firebase Cloud Messaging into your PHP application and start sending push notifications using the lkaybob/php-fcm-v1 package.

Happy coding!

Top comments (3)

Collapse
 
ngtduc693 profile image
Duc Nguyen Thanh

great topic but I have a question, is it totally free or does it have a quota for free

Collapse
 
omr4ni profile image
Ramin Omrani • Edited

Thanks. It is completely free. Although Google doesn't enforce strict limits on the number of notifications you can send, high-volume sending may be subject to throttling, especially if it affects the stability of the FCM service or violates Google's policies.

Collapse
 
ngtduc693 profile image
Duc Nguyen Thanh

thanks, I will try it in the feature