Basically, We have used Google Cloud Messangin(GCM) for sending push Notification to Android Devices. And then Google launched Firebase Cloud Messaging (FCM), the latest version of GCM with more properties.

This blog is helpful for the Developer who want to send push notification by using PHP with FCM (Firebase Cloud Messaging).

what is actually Push Notification?

In general, Push notifications are the message / Notifications you will receive in Android. This makes the customers engage with the clients on digital platforms. This service can be initiated by either way form users or from clients.

This makes the basic difference and leads to subdivisions of protocol into two forms. The first one is User registration and the next one is Client broadcasting.

In User registration, the user will send the request for the service from the clients, through GCM/FCM in a messaging format. Then the cloud server will initiate a token with the unique information to connect the user with the client. This token id of the user will be stored in a remote database. Then the connection between the user and client will be open.

In Client Broadcasting, the client will raise the request for the external user. This request will have a unique Authorization key and Device token for a list of users. The cloud service (FCM/GCM) accepts the request and broadcasts the message to all users on the list. The user will get a notification on Android App.

If you want to send Push Notification to Android Device using PHP. Then You can Learn for that from this blog.

Send Push Notification using PHP

Here given Prerequisite for sending Notification using PHP:

  1. Authorization key (Server Key)
  2. Device Token

Go to Your WAMP or XAMPP Directory and create a new folder named notification inside htdocs or www.

And then create a file named fcm.php and add push notification code which is given below. You also need the Firebase Server API Key for sending Notification. You can do google for this.


class FCM {
function __construct() {
}
/*
For 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 ','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);
}
}

you have need to replace your Your Firebase Server API Key Here with from the Google API’s Console page.

Then Create index.php file and add given below code


$regId ="Your Device Token";

// Here, INCLUDE YOUR FCM FILE
include 'fcm.php';
$notification = array();
$arrNotification= array();
$arrData = array();
$arrNotification["body"] ="Test by Futurelens.";
$arrNotification["title"] = "PHP Notification";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1;

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

Please replace your Device Token with your real device token before the testing.