Tag Archives: Push Notification

How to implement firebase push notification in android and iphone

push_notification_in_iphone_and_android

If you need send Firebase notification using PHP then simple use this tutorials which can help you send the notification to android and iPhone devices. Basically there is a GCM(Google Cloud Messaging) which is sending the notification but now google has been launched Firebase Cloud Messaging (FCM)

Step 1)

Generate the Firebase Server Key. for this you need to follow below steps.

  • Login to the Firebase Developer Console and if you haven’t created a project yet, click “Create Project”.
  • Enter a project name, accept the firebase terms and press “CREATE PROJECT”.
  • Does setup google analytics for your project? select “Not Right Now” and press “CREATE PROJECT”.
  • The project will be created and you get the message “Your new project is ready”, press the “Continue” button to redirect your project dashboard page.
  • On the dashboard page click the Gear icon in the top left and select “Project settings”.
  • Go to the “Cloud Messaging Section” section and you will have the Server Key.
  • Done! I hope you successfully Generate a Firebase Server Key

Step 2)

Create a file named fcm.php and add the below push notification php code. Be sure to replace the Your Firebase Server API Key Here with a proper one from the Google API’s Console page.

<?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)

Finally, create an index.php and add the below code. Before testing please replace Your Device Token with your real device token.

<?php
$regId ="Your Device Token";

// INCLUDE YOUR FCM FILE
include_once 'fcm.php';    

$notification = array();
$arrNotification= array();			
$arrData = array();											
$arrNotification["body"] ="Test by Ravi Sukhadia.";
$arrNotification["title"] = "TECHIBUCKET";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1;

$fcm = new FCM();
$result = $fcm->send_notification($regId, $arrNotification,"Android");
?>

Step 4)

Send Push Notification to IOS Using PHP. You just need to change “Android” to “IOS” in your index.php file. like below…


FOR ANDROID

$result=$fcm->send_notification($regId,$arrNotification,"Android");

FOR IPHONE

$result=$fcm->send_notification($regId, $arrNotification,"IOS");

Step 5)

Now run your index.php file and you can get the notification to your devices.