PHP Send Push Notification To Android & IOS using Google FCM Firebase

To send push notification to android and ios mobiles using PHP + FCM firebase. In this tutorial, i am going to show you how to send push notifications to android and ios mobiles using PHP + Google FCM firebase.
It is very easy to send push notifications from any PHP application to Android and iOS mobiles. For this you have to register yourself in Google FIREBASE. And Google FIREBASE fcm will give you the ID. Using which you can easily send push notifications from your Php application to Android and iOS mobiles.
Send Push Notification to Android and IOS using PHP + FCM Firebase
Just follow the few steps for send push notifications to ios using PHP firebase fcm:
- Step 1 – Create PHP Files
- Step 2 – Create fcm.php File
- Step 3 – Create send-notification.php File
- Step 4 – Run Application
Step 1 – Create Index.php File
In this step, create a php file which named index.php. And then add the following code into it:
<!DOCTYPE html> <html> <head> <title>PHP Send Push Notification To Android & IOS using Google FCM Example</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <br> <div class="row"> <div class="col-md-9"> <form action="send-notification.php" method="post" accept-charset="utf-8"> <div class="form-group"> <label for="formGroupExampleInput">Device Type</label> <select class="form-control" id="device_type" name="device_type" required=""> <option value="">Select Device type</option> <option value="Android">Android</option> <option value="Iphone">Iphone</option> </select> </div> <div class="form-group"> <label for="formGroupExampleInput">Notification Id</label> <input type="text" name="nId" class="form-control" id="formGroupExampleInput" placeholder="Please enter notification id" required=""> </div> <div class="form-group"> <button type="submit" id="send_form" class="btn btn-success">Submit</button> </div> </form> </div> </div> </div> </body> </html>
Step 2 – Create fcm.php File
In this step, create a file named fcm.php and add the following code for push notification into it:
Note that, Please make sure to define the Firebase Server API Key to send a request to firebase.
<?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, 'notification' => $notification ); } // 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); } } ?>
Step 3 – Create send-notification.php File
In this step, create send-notification.php file and add the following code into it:
<?php //save crop image in php if(isset($_POST["nId"])) { $regId =$_POST["nId"]; $dType =$_POST["device_type"]; // INCLUDE YOUR FCM FILE include_once 'fcm.php'; $arrNotification= array(); $arrNotification["body"] ="PHP Push Notification"; $arrNotification["title"] = "PHP Push Notification"; $arrNotification["sound"] = "default"; $arrNotification["type"] = 1; $fcm = new FCM(); $result = $fcm->send_notification($regId, $arrNotification,$dType); print_r($result); } ?>
Step 4 – Run Application
In this step, open your terminal and execute the following command to quick run this application:
cd project_directory php -S localhost:8000
Now you can open the following URL on your browser:
http://localhost:8000
Conclusion
To send push notification to android and ios mobiles using PHP + FCM firebase. In this tutorial, You have learned how to send push notifications to android and ios mobiles using PHP + Google FCM firebase.