im getting mismatch senderid error.
in gcm, i double checked the sender id as client app project id and api_key.
My code is,
$ids = array( 'APA91bFDc......._p' );
$data = array( 'message' => 'Hello World!' );
$apiKey = 'AIzaSyA0_CFcI.........saGySJBE';
$url = 'https://gcm-http.googleapis.com/gcm/send';
$sender_id = '1063000000016';
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Sender: id=' .$sender_id,
'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_POSTFIELDS, json_encode( $post ) );
$result = curl_exec( $ch );
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close( $ch );
echo $result;
my response is
{"multicast_id":7107329859481637548,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
why i'm facing the problem?
I refered the previous questions like,
GCM Sender Id Mismatch, Android C2DM sender id, Unique SMS sender id? and etc
edit
I just added the sender id myself to the headers. where to mention the sender id and how to mention in php
Related
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 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]);
}
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 created a simple php code to send a notification to my android devices which have google log in method from firebase implementation. Their tokens are already stored in my database. When I execute my php, it doesn't send the notification. Whoever , if i send a notification trough firebase notification console it works. This is my php code .
function sendGCM($message, $registration_ids) {
//FCM URL
$url = "https://fcm.googleapis.com/fcm/send";
//prepare data
$fields = array (
'registration_ids' => array ($registration_ids),
'data' => array ("message" => $message)
);
$fields = json_encode ( $fields );
//header data
$headers = array ('Authorization: key=<YOUR_API_KEY>', 'Content-Type: application/json');
//initiate curl request
$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_POSTFIELDS, $fields );
// execute curl request
$result = curl_exec ( $ch );
//close curl request
curl_close ( $ch );
//return output
return $result;
}
Error:
when I execute this php file, throw this error:
{
"multicast_id": 5359746182596118281,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
Try this:-
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'your_key' );
$registrationIds = array($id); //$id is string not array
// prep the bundle
$notification = array
(
'title' => 'title',
'body' => 'body',
'icon' => 'logo',
'sound' => 'default',
'tag' => 'tag',
'color' => '#ffffff'
);
$data = array
(
'message' => 'message body',
'click_action' => "PUSH_INTENT"
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $notification,
'data' => $data,
'priority' => 'normal'
);
$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 do know there are several posts here which talks about my issue but none works for me.Its either incomplete solution mentioned or left as is.
My query:
I am getting
Field "data" must be a JSON array: [{"sound":1,"vibrate":1,"message":"Push Notification Message","title":"Push Notification Title"}]
error when trying to use GCM. My regID, Sender ID, and server key looks fine.
May know how to resolve this.
PHP Code:
<?php
$to="";
if($_GET['id']){
$to = $_GET['id'];
}
$title="Push Notification Title";
$message="Push Notification Message";
sendPush($to,$title,$message);
function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'API_HIDDEN');
$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' => array($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;
}
?>
Replace the line
$message="Push Notification Message";
by
$message=array( 'response' => json_encode("Push Notification Message"));
GCM requires the response data in JSONArray format.
OR Use below method
function sendGoogleCloudMessage( $data, $ids ) {
$apiKey = 'YOUR_KEY';
$url = 'https://android.googleapis.com/gcm/send';
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
$result = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
$result = $result . 'GCM error: ' . curl_error( $ch );
}
curl_close( $ch );
return $result;
}
and call it using
$response = array();
$response["title"] = "title";
$response["message"] = "Notification message.";
$data = array( 'response' => json_encode($response));
$result = sendGoogleCloudMessage($data, $ids);
It is working in my application.
Hope it'll help.