PHP Send Push Notification To Android & IOS using Google FCM
Php 01-Apr-2021

PHP Send Push Notification To Android & IOS using Google FCM

Send push notification to android and ios using php fcm. In this tutorial, you will learn how to send push notification to android and ios using php fcm.

This tutorial will explain each thing is a very simple and easy example for send push notifications to android in php using google firebase fcm.

Send Push Notification to Android and IOS using PHP FCM

Just follow the few steps for send push notification 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, ope 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

In this tutorial, you have learned how to send push notification to ios and android using php fcm.