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]);
}
Related
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
);
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!
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 .
I am trying to send the push notification to multiple devices using php. following is my code
define( 'API_ACCESS_KEY', 'mykey');
$message =' some message ' ;
$msg = array
(
'body' =>$message,
'title' => 'You have a new message ',
);
$regids = array( 'registration_ids' =>'firstid');
$fields = array
(
'to' => json_encode($regids),
'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 );
but following is the result i get .
{"multicast_id":idhere,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Please help me how do i fix this
Put Devices ID's in This array
$dv_id [] = array(
'dv_id' => $re['dv_id'],
);
//Loop through your id's array
for ($i = 1; $i < count($dv_id); $i++) {
//Call your send notification function link this
send_notification($dv_id[$i]['dv_id'],$title,$msg);
}
function send_notification($device_id,$title,$message){
// API access key from Google API's Console
// prep the bundle
$msg = array
(
'to'=>$device_id,
'notification' => array('body'=>$message,'title'=>$title,
'click_action'=>'MY_ACTIVITY_1','sound'=>'tone'),
'data' => array('message'=>$message,'title'=>$title)
);
$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( $msg ) );
$result = curl_exec($ch );
curl_close( $ch );
}
Happy Coding.
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;
?>