I'm trying to send a notification using GCM on my android device, but I always get InvalidRegistration error.
Here is the PHP code that is supposed to send the notification:
<?php
define('API_ACCESS_KEY', 'API KEY HIDDEN');
$registrationIds = array( $_GET['id']);
// prep the bundle
$msg = array
(
'message' => 'TestMessage',
'title' => 'TestTitle',
'subtitle' => 'TestSubtitle',
'tickerText' => 'TestTicker',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$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;
?>
This is how the registration is done:
private void registerInBackground() {
new AsyncTask<Void, Object, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
//Here I save the registration ID, and when I try to use it manually, by running the PHP script above, I get the error.
sendRegistrationIdToBackend();
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(context, msg + "\n", Toast.LENGTH_SHORT).show();
}
}.execute(null, null, null);
After running the application, and calling the method that registers the app, I get a registration ID. Then, when I type localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx
I get
{"multicast_id":8460288429151356251,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...
WhatsApp(and several other Apps) rely upon GCM/FCM(as option 1 - the default) as it is present as a system App on lot of devices and therefore holds a special status where it is very less likely to be killed unlike a normal App. For devices that do not have play services, your custom socket connection is relied upon.
GCM stands for Google Cloud Messaging. Every push notification receive on any Android device is sent by the GCM only. when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.
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
If you are retrieving token from db make sure the data type for the column is either text or a very long varchar like 200. The token is more than a 100 characters long so it might be getting cut in half to be stored in the db
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With