Firebase Cloud Messaging — PHP integration, push notifications

Aleksa Smolović
Mar 17 · 3 min read

Time is fire in which we burn.

Firebase is Google’s Cloud platform offering many great services which are easy to integrate into your apps. Firebase boasts with over 18 versatile products which provide developers apps with cutting edge functions that enhances better experience for both the user and the development teams.

Welcome to part one of my three part series on one of Firebase’s most prominent services, used for cross-platform messaging — Cloud Messaging. What it really means is that you can use it to communicate (send data) between different mediums. It provides support for multiple languages such as php, javascript, android, iOS (and many more) and it’s free to use.

In this article I am going to run through back-end app setup which will utilize PHP to send push notifications. Push notifications are notifications we receive on our mobile devices or in our browsers and are Cloud Messaging most used application of its service.

First I am going to create Firebase Project which is done in the Firebase Console. After clicking Add Project and naming my project I am prompted to include Firebase Analytics which are recommended to include especially if we are building mobile app. Once project building is finished I am redirected to project’s console from where I can further monitor and configure built project.

For this tutorial purposes I need to fetch project server key which I will use to send notifications. To obtain I need to go Project Settings -> Cloud Messaging -> Server Key.

Console -> Project Settings -> Cloud Messaging (Tab) -> Server Key

First I will define data I am going to send. Push notification can contain some standard elements such as title and body or any other data you wish to send. Data is formed in an associative array which is later json encoded. I can pack my data into data and/or notification key.

$notification = [    ‘title’ => ‘This is notification title’,    ‘body’ =>‘This is notification text’,    “alert” => “Test Push Message”,    “sound” => “default”,];$data = [    ‘title’ => ‘This is notification title’,    ‘body’ =>‘This is notification text’,    ‘priority’ => ‘high’,    ‘content_available’ => true];

Notifications can be sent to specific user or group of users. If we want to send it to one user we need user’s Firebase Token, and for group we use topic/{topic name}.

$fcmNotification = [    ‘to’ => ‘/topics/alerts’,    ‘notification’ => $notification,    ‘data’ => $data,    ‘priority’ => 10];

We also have to specify Content-Type being sent and our project’s server key.

$headers = [    'Authorization: key=' . $serverKey,    'Content-Type: application/json'];

Finally we will send our data. To do so we will need to create CURL request to Firebase server with our data and server key. CURL is a tool which can be used to send HTTP requests and is PHP’s native function.

$fcmUrl = 'https://fcm.googleapis.com/fcm/send';$cRequest = curl_init();curl_setopt($cRequest, CURLOPT_URL, $fcmUrl);curl_setopt($cRequest, CURLOPT_POST, true);curl_setopt($cRequest, CURLOPT_HTTPHEADER, $headers);curl_setopt($cRequest, CURLOPT_RETURNTRANSFER, true);curl_setopt($cRequest, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($cRequest, CURLOPT_POSTFIELDS, json_encode($fcmNotification));$result = curl_exec($cRequest);curl_close($cRequest);echo $result;

If your response looks like this, you have successfully integrated Firebase Cloud Messaging service into your back-end app.

Request response

All is left to do is tweak data as you see fit and with it you can: send push notification, create chat functionality, update live data and so much more…

This is just brief explanation on integrating cloud messaging into your apps. Until next time …

exit(‘bye’);

Aleksa Smolović

Written by

Software developer from Podgorica, Montenegro.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade