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.
Related
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;
?>
Please help me with this issue while sending push notification to more than thousands of user i stuck with 405 error.Below is code for push notification
function androidPush($regid,$message,$title,$imageUrl='',$url)
{
if (!defined('API_ACCESS_KEY')) define('API_ACCESS_KEY', 'mykey');
$registrationIds = array_filter($regid);
$Regid = array();
foreach($registrationIds as $rid){
$Regid[] = $rid;
}
if(empty($registrationIds) || $title==""){
return false;
}else{
$msg = array
(
'message' => html_entity_decode(trim($message)),
'title' => $title,
'image' => $imageUrl,
'url' =>html_entity_decode(trim($url)),
'vibrate' => 1,
'sound' => 1
);
$fields = array();
$fields['data'] = $msg;
$Idcount = count($Regid);
if($Idcount > 1000)
{
$newId = array_chunk($Regid, 1000);
for($i = 0;$i < count($newId);$i++){
$fields['registration_ids'] = $newId[$i];
$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 );
}
return $result;
} else{
$fields['registration_ids'] = $Regid;
$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 );
return $result;
}
}
}
405 error states that Method Not Allowed more
try adding curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
because i think gcm code expects post request..
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;
}
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