Send Push Notification to Android & IOS using PHP + 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.

PHP + FCM Firebase Send Push Notification to Android & IOS

  • Step 1 – Create PHP Project
  • Step 2 – Create Index.php File
  • Step 3 – Create firebase-fcm.php File
  • Step 4 – Create send-push-notification.php File
  • Step 5 –  Test This PHP Send Push Notification Application

Step 1 – Create PHP Project

Navigate to your xampp/htdocs/ directory. And inside xampp/htdocs/ directory, create one folder. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Create Index.php File

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 - Laratutorials.com</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-push-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 3 –  Create firebase-fcm.php File

Create a file named firebasefcm.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 4- Create send-push-notification.php File

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 'firebase-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 5-  Test This PHP Send Push Notification Application

Open your terminal and run 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.

Recommended PHP Tutorials

Leave a Comment