If you need to send a Firebase notification using PHP, then simply use these 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 launched Firebase Cloud Messaging (FCM).
Step 1)
Generate the Firebase Server Key. For this you need to follow the below steps.
- Log in 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 it set up Google Analytics for your project? Select “Not Right Now” and press “CREATE PROJECT.”
- The project will be created, and you will get the message “Your new project is ready.” Press the “Continue” button to redirect to 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 on your devices.