Basically, we have used gcm(google cloud messaging) for sending push notification to android and ios devices.
Recently google has been launched firebase cloud messaging (fcm), the latest version of gcm (google cloud messaging) with more properties.
This article is useful for the php and codeigniter developers who want to send push notifications by using php/codeigniter with fcm(firebase cloud messaging).
Push notification was a viral protocol which gives bloom to business in today’s digital world. Here we will explain to you about, what is push notification? How to create a push notification using different platforms/services like php/codeigniter (hypertext preprocessor), gcm (google cloud messaging) and fcm (firebase cloud messaging)? Let’s start
What is 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 in 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.
You can send push notification to android using php / CodeIgniter . here are the steps, lets, check it out.
How to Send a Push Notification Using PHP / CodeIgniter?
We need 2 important things to send notification
- Firebase Server key
- Sender Id Or Device Id
First of all we create a function inside which we will code the FCM notification
function send_push_notification($senderids,$title,$description,$imgurl = null){
}
After this we code, in which we have to do curl also
function send_push_notification($senderids,$title,$description,$imgurl = null){
$serverkey = '';// this is a Firebase server key
$data = array(
'registration_ids' => $senderids,
'notification' =>
array(
'body' => $description,
'title' => $title,
"image"=> $imgurl),
"data"=> array(
"click_action"=> "FLUTTER_NOTIFICATION_CLICK",
"sound"=> "default",
"status"=> "done"
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key='.$serverkey));
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
Use send_push_notification Function in Codeigniter
$this->send_push_notification(array('senderid string 1','senderid string 2'),'What is 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 in either way form users or from clients. ','https://stackoverlode.com/no-image/image.jpg');
Response success
{"multicast_id": 5735939659427266909,"success": 1,"failure": 0,"canonical_ids": 0}
Response fail
{"multicast_id": 5735939659427266909,"success": 0,"failure": 1,"canonical_ids": 0,"results": [{"error": "InvalidRegistration"}]}
Use send_push_notification Function in PHP
send_push_notification(array('senderid string 1','senderid string 2'),'What is 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 in either way form users or from clients. ','https://stackoverlode.com/no-image/image.jpg');
Response success
{"multicast_id": 5735939659427266909,"success": 1,"failure": 0,"canonical_ids": 0}
Response fail
{"multicast_id": 5735939659427266909,"success": 0,"failure": 1,"canonical_ids": 0,"results": [{"error": "InvalidRegistration"}]}