Send firebase push notification to multiple users not working -php - 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 .

Related

I have implemented an FCM server side, Server side not delivering

I am trying to implement a php curl fcm topic broadcasting and it just displays {"message_id":4731721763997571462} and does not deliver.
I have gone through a lot of search and to no end, I can't seem to find the problem.
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
'to' => "/topics/hello",
'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 );
echo $result;
I just expect to get a notification on the device and maybe there is a way I can trace successful notifications on the google console?
You need to add registration ids with field parameter
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
$registrationIds = array( "/topics/obajemusagmailcom" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
//'to' => "/topics/hello",
'registration_ids' => $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 );
echo $result;
and $registrationIds = array( "/topics/obajemusagmailcom" ); is not registration id of any app, please correct it.
Finally after much horror and tries, turned out the array fields were incorrect and were meant to be
$msg = array
(
'body' => $body,
'title'=>$title
);
$data = array
(
'to' => "/topics/".$topic,
'notification' => $msg
);

How to send firebase (fcm) to multiple users and different data per user

i want to send fcm to multiple users but i want to send different data to each user , like this.
$deviceid2 ="da10bCm38LU:APA91bHC-XB";
$deviceid1 = "dKF-20xsI9Y:APA91bE";
$API_ACCESS_KEY='AAAA_';
$registrationIds = array( "$deviceid1","$deviceid2" );
$title1="title1";
$title2="title2";
$message1="message1";
$message2="message2";
$title = array( "$title1","$title2" );
$body = array( "$message1","$message2" );
$fields = array
(
'registration_ids' => $registrationIds,
'data' => array(
'title' => $title,
'body' => $body
)
);
$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_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch );
curl_close( $ch );
i tried it,the response was success from curl, but no notification appeared on my phone. can anyone help for this?
Then you need to send as many different FCM messages as users. You cannot have one message that is different for different users!

Send push notification using php and firebase console account

to all,
ehm i have this php code:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_ACCESS_KEY' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'body' => $_GET['body'],
'title' => $_GET['title'],
'vibrate' => 1,
'sound' => 1,
);
$fields = array
(
'registration_ids' => $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 );
echo $result;
?>
I've just searched but what i would want is:
1) Which is the YOUR_FIREBASE_API_ACCESS_KEY? In settings of my app in firebase
i see SERVER KEY, PREVIOUS SERVER KEY (translated from italian) and SENDER ID.
2) How can i get registration ids from firebase account app? If i want send push notification to all device that have installed this app how can i DO it?
3) When button clicked:
Connect to firebase account throught API key (Which is?)
Send to all devices that have my app installed
(topic or not?)
Please, help me.
I'm confused.
More than clear of that i can't.
Thanks
Davide

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;
?>

Categories