How to implement web push notifications using FCM in php - php

I'm working on web push notifications using service worker. But I struck while sending notifications from server using FCM(Firebase Cloud Messaging). Here is my php script and I'm not getting how should I call this in service worker. Please guide me.
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$msg = array(
'message' => 'Wakeup Wakeup!!',
'title' => 'Wakeup call !',
);
$fields = array(
'registration_id' => xxxxxxxxxx,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '//gcm-http.googleapis.com/gcm/send' );
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 );
curl_close( $ch );
echo $result;

You could use the web-push library for PHP, which, as the name says, makes it easier to use the Web Push protocol via PHP.

Related

Unable to Send FCM Notification using PHP When app is closed

i am using the following php code to send push notifications using Firebase.
I have received notification on Android app successfully if app is opened, but in case if app is closed, then unable to receive notification. But echo display result success.
Is there any changes required in php code?
Anybody, Have an idea?
Here is my PHP code :-
function send_notification($token,$title,$desc){
$msg = array
(
'body' => $desc,
'title' => $title,
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $token,
'data' => $msg
);
$headers = array
(
'Authorization: key=AIzaSyBP9nF9ntawf_sirR9c2eQ0CQUHTVaBpRs',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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 );
curl_close( $ch );
#Echo Result Of FireBase Server
$new_result = json_decode($result);
echo($result);
}
FCM Token : dvbsfdkjNH8:APA91bGfvS61gL2XBCq4p0kHOJAjG-ReWpZWO4oWHHflV-baFpSsjm4FjBQCS8dWs_vpbvXJJr7yvOuEmg7rAWNsZfL2r3C0WKrib2dDZSzKZNXaFkHHifYjSvCMntD2bUveovkcidHS
Your php code is fine. This needs to be handled from the app side.
You need to check the FCM intent listner.
An app developer would help you better in this.

Send firebase push notification to multiple users not working -php

I'm trying to send push notification to multiple devices using below PHP script.when i send notification to one device ,it's working.but not working for multiple devices.
protected function pushNotification($reg_id){
define( 'API_ACCESS_KEY', 'AAAAgUh9ZSQ:APA91bEjQ2nO3Po9_-T_kNey4q3Eizcoc3gLsD04Bjh0KYXQu_33O2gpLBvmMV_DtbE3xP2evZ1Dwki6WRqgqu8ZYhOG2CYi-6Wpxq2CLy3SQPb1nD6QkzwNN0249pt-H2mAgB8L-sjm' );
$registrationIds = array($reg_id);
$msg = array
(
'body' => 'Hey! You have a new order in queue',
'title' => 'New Order Receieved',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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 );
curl_close( $ch );
error_log("Result ".$result) ;
}
The problem lies in your use of to in $fields. to is to be used only when you have an individual String FCM ID. If you want to send notifications to an array of FCM IDs (which is your case), you must use registration_ids in place of to. Please note that you can only pass an array of size lesser than 1000 at a time in this operation.
For more information, https://firebase.google.com/docs/cloud-messaging/http-server-ref .

Firebase : php push notification to multiple devics

i am using following code to send push notifications to one Android device
function send_notification($message, $token){
define( 'API_ACCESS_KEY', 'mykey');
$msg = array
(
'body' =>$message,
'title' => 'You have a new message ',
);
$fields = array
(
'to' => $token,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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 );
echo $result;
curl_close( $ch );
}
i am successfully sending it to one device what if i want to send it to multiple device id's where do i have to loop and what format data would be . Please help
Docs here show how to specify registration_ids for recipients of multicast messages. Replace your $fields['to'] with a $fields['registration_ids'] which will be an array of strings containing your recipients' tokens.
try this one :
for($i=0;$i<count($TokenArray);$i++){
send_notification($message, $TokenArray[$i]);
}

How to send notification to all devices that installed my app using Firebase and PHP?

I'm making an existing application which includes notification when there is new content.
Notification is sent to all the gadgets that have installed the application.
How to do it using FCM and PHP?
Here is the PHP code to send notification using FCM.
<?php
define('API_ACCESS_KEY', ''); // API access key from Firebase Console
$registrationIds = array(''); //Token IDs of devices
$msg = array
(
'text' => 'Test Text',
'title' => 'Test Title',
);
$fields = array
(
'to' => $registrationIds[0],
'notification' => $msg,
"priority"=> "high"
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json',
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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 );
curl_close( $ch );
echo $result;
?>

Internal Server Error using Google API on PHP

Running this code on Godaddy, Plesk account returns 'Internal Server Error'. Should I make any changes to code or any configuration to run this code?
Code ran perfectly in easyphp local server. Please help. Thank you.
sendPush($to,$title,$message);
function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'AIzlkplhm-KrtLlR-p-Mg');
$registrationIds = array($to);
$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1
// you can also add images, additionalData
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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 );
curl_close( $ch );
echo $result;
}

Categories