Fundamentally, we have utilized GCM(Google Cloud Messaging) for sending pop-up message to Android gadgets.
As of late Google has been dispatched Firebase Cloud Messaging (FCM), the most recent variant of GCM (Google Cloud Messaging) with more properties.
This article is helpful for the PHP engineers who need to send pop-up messages by utilizing PHP with FCM (Firebase Cloud Messaging).
Message pop-up was a viral convention which offers sprout to business in Today's computerized world. Here we will disclose to you about, What is Push Notification? How to make a Push Notification utilizing various stages/administrations like PHP (Hypertext Preprocessor ), GCM (Google Cloud Messaging) and FCM (Firebase Cloud Messaging)?
Also Read:- How to Implement Real Time Notification Using Socket.io and Node.JS?
What is Push Notification?
When all is said in done, Push warnings are the message/Notifications you will get in Android. This causes the clients to draw in with the customers on advanced stages. This administration can be started in whichever way structure clients or from customers. This has the fundamental effect and prompts regions of convention into two structures. The first is User enlistment and the following one is Client broadcasting.
In User enlistment, the client will send the solicitation for the administration from the customers, through GCM/FCM in an informing design. At that point the cloud worker will start a token with the novel data to interface the client with the customer. This symbolic id of the client will be put away in a distant information base. At that point the association between the client and customer will be open.
In Client Broadcasting, the customer will raise the solicitation for the outer client. This solicitation will have a novel Authorization key and Device token for a rundown of clients. The cloud administration ( FCM/GCM ) acknowledges the solicitation and broadcasts the message to all clients on the rundown. The client will get a notice on Android App.
Also Read:- How to Put a Calculator in the Mac Notifications Center
You can send message pop-up to android utilizing php. here are the means, lets, look at it.
How to Send a Push Notification Using PHP?
Essential:
- Approval key (Server Key)
- Device Token
You can get gadget token at the hour of enrolling by the android clients utilizing API.
Lets Create PHP Files
- Goto your WAMP/XAMPP registry and make another envelope named warning inside htdocs or www.
- Make a record named fcm.php and add the beneath pop-up message php code. Here we characterize the Firebase Server API Key to send a solicitation to firebase.
<?php
class FCM {
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
);
}
$headers = array('Authorization:key=Your Firebase Server API Key Here','Content-Type:application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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);
}
}
?>
Also Read:- PayPal Standard Payment Gateway Integration in PHP
Make certain to supplant the Your Firebase Server API Key Here with an appropriate one from the Google API's Console page.
- Finally, create an index.php and add the below code.
<?php
$regId ="Your Device Token";
include_once 'fcm.php';
$notification = array();
$arrNotification= array();
$arrData = array();
$arrNotification["body"] ="Its testing notification.";
$arrNotification["title"] = "Notification";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1;
$fcm = new FCM();
$result = $fcm->send_notification($regId, $arrNotification,"Android");
?>
Prior to testing please supplant Your Device Token with your genuine gadget token.
Send Push Notification to IOS Using PHP
Also Read:- PayPal Standard Payment Gateway Integration in PHP
You simply need to change "Android" to "IOS" in your index.php record. like beneath…
$result=$fcm->send_notification($regId,$arrNotification,"Android");
To
$result=$fcm->send_notification($regId, $arrNotification,"IOS");
Expectation it caused you in a simple manner to send message pop-up to android utilizing php. If you don't mind let us know in the remark segment in the event that you have any worry.
You can likewise impart to your companions, who need to send message pop-up to android and IOS utilizing PHP.