Using GCM to send notifications on app, returns InvalidRegistration error
Invalid Registration ID Check the formatting of the registration ID
that you pass to the server. Make sure it matches the registration ID
the phone receives in the com.google.android.c2dm.intent.REGISTRATION
intent and that you're not truncating it or adding additional
characters. Happens when error code is InvalidRegistration.
So basically you are making an error in sending the DeviceID received by the phone to the server. Make sure your code does not alter the deviceId in any way. You have not posted the code for accepting the Id's. There is a mistake in that part or the storing of the DeviceId's. I don's think there is any error in the code you have posted as the errorLog show's Invalid Registration
Google Cloud Messaging returns Invalid Registration
mysqli_query returns Boolen value (true or false) so this will not going to works directly.
we have to convert mysqli_query return values to array using fetch array, fetch assoc.
$results = mysqli_query($Thesisdb, $sql);
$registrationIds = array($results);
Code to combile all GCM Tokens.
registrationIds=array();
$sql="SELECT `user_id`,`push_notification_registration_id` FROM `tbl1` WHERE `user_status`='Active' AND `push_notification_registration_id`!=''";
$fire=mysqli_query($this->conn,$sql);
$users=array();
if( (isset($fire))&&(!empty($fire)) ){
while($result=mysqli_fetch_assoc($fire)){
array_push($users,$result);
}
}
foreach($users as $single_user){
$register_id=$single_user['push_notification_registration_id'];
array_push($registrationIds,$register_id);
}
$msg = array
(
'title' => $news_title,
'subtitle' => $news_description,
'tickerText' => 'Ticker Text',
'vibrate' => 1,
'sound' => 1,
'news_id' => $news_id,
'notification_type' => 'news'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . YOUR_GOOGLE_API_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,true ) );
$result = curl_exec($ch );
curl_close( $ch );
Invalid registration while sending push notification in php
I am implementing your code.
Use firebase registration token
it generated in device (ios/android)
instead of the Device token .'registration_ids' => (array)$token,
Hope it's helpful
GCM returns BAD Request when invalid registration id is used
Yes, you are right.
But;
If you send only one token with 'to' parameter, GCM returns 400.
if you try to send multiple tokens(some of them is valid and others not), GCM returns 200 and error list has 'InvalidRegistration' code.
As this scenario, I can say;
if use 'to', GCM returns 400.
if use 'registration_ids', GCM returns 200 and error list in response.
You can also check it with this library's test cases.
Always get the invalid registration from the php (server side) in GCM
Did u defined the vaule for API_KEY in your file.?
See below php file. This is working for me.
<?php
//Generic php function to send GCM push notification
function sendMessageThroughGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "API_SERVER_KEY");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
?>
Define your API_SERVER_KEY in this line.
define("GOOGLE_API_KEY", "API_SERVER_KEY");
refer this site : http://programmerguru.com/android-tutorial/how-to-send-push-notifications-using-gcm-service/
Related Topics
Mysql_Fetch_Array Returns Duplicate Data
Illegal Command Error Code 127 in PHP Exec Function
PHP Session Seemingly Not Working
How to Get the File Path in HTML <Input Type="File"> in PHP
Regex Matching Table Rows in HTML
Laravel Password Validation Rule
Laravel Middleware with Multiple Roles
How to Convert MySQL Time to Unix Timestamp Using PHP
Need Response Body of Http 500 with File_Get_Contents (Php)
PHP Remember File Field Contents
Php: Session Lost on Subdomain
Calling Perl Script from PHP and Passing in Variables, While Also Using Variablized Perl Script Name