How to Send Silent Push Notifications Using PHP And Firebase
This article is useful for PHP developers who want to send silent push notifications by using PHP with FCM (Firebase Cloud Messaging).
What Is Silent Push Notifications?
If you send a silent push notification, call it a background notification that does not trigger any sound or alert. It wakes up your apps in the background and allows you to perform any action/operations. Generally, silent push notifications are used in background content updates.
If you are unable to send silent push notifications to iOS using FCM, This article is useful for you. In this article, let’s look into how you can send a silent push notification to your iOS device using PHP with firebase cloud messaging.
If you want to send a simple push notification to Android/IOS, Check out our article Send Push Notification to Android Using PHP And Firebase.
How to Send a Silent Push Notification Using PHP?
Prerequisite:
1) Authorization key (Server Key)
2) Device Token
You can get the device token at the time of registering by the android users using API.
3) You need to add “Background Modes” capabilities in Xcode in order for your app to be able to receive a silent push notification. See the below image.
Lets Create PHP Files
- Create a file named fcm.php and past below push notification code. Here we define the Firebase Server API Key to send a request to firebase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php class FCM { function __construct() { } /** * Sending Push Notification */ public function send_notification($registatoin_ids, $notification,$device_type) { $url = 'https://fcm.googleapis.com/fcm/send'; if($device_type == "Android"){ $fields = array( 'to' => $registatoin_ids, 'data' => $notification ); } else { $fields = array( 'to' => $registatoin_ids, 'data' => $notification, 'content_available' => true, 'priority' => "high" ); } // Firebase API Key $headers = array('Authorization:key=Your Firebase Server API Key Here','Content-Type:application/json'); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disabling SSL Certificate support temporarly curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); } curl_close($ch); } } ?> |
Be sure to replace the Your Firebase Server API Key Here with a proper one from the Google API’s Console page.
2. Finally, create an index.php and paste the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $regId ="Your Device Token"; // INCLUDE YOUR FCM FILE include_once 'fcm.php'; $notification = array(); $arrNotification= array(); $arrData = array(); $arrNotification["body"] ="Test by PHP ADVICES"; $arrNotification["title"] = "PHP ADVICES"; $arrNotification["sound"] = "default"; $fcm = new FCM(); $result = $fcm->send_notification($regId, $arrNotification,"IOS"); ?> |
Before testing please replace Your Device Token with your real device token.
Today, We learn How to Send Silent Push Notifications Using PHP And Firebase.
Hope it helped you with an easy way to send silent push notifications to android and IOS using PHP. Please let us know in the comment section if you have any concerns.
Thanks for your time, to know about me.
Hello I Am Vijay Poshiya. 🙂
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming. I can give you a cake walkthrough platform where a complex program can make it easier for you. I will also provide you with the rare collections of task sets over the internet.
I hope you will find your solutions in better understanding shape within my blog.
You can read more about PHPAdvices at the “About” page.
Curl failed: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
Add the following line to your curl request.
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);