Using GCM to send notifications in the application returns an InvalidRegistration error

I'm trying to send a notification using GCM to my Android device, but always get an InvalidRegistration error.

Here is the PHP code that should 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; ?> 

Here's how to register:

 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  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 starting the application and calling the method that registers the application, I get the registration identifier. 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"}]} 
+7
source share
3 answers

Invalid registration ID Check the formatting of the registration ID that you are switching to the server. Make sure that it matches the registration ID the phone receives in com.google.android.c2dm.intent.REGISTRATION and that you do not truncate it or add additional characters. It happens when the error code is InvalidRegistration.

Basically, you make a mistake when sending the device identifier received by the phone to the server. Make sure your code does not change deviceId. You did not send the code to accept the identifier. There is an error or saving DeviceId in this part. I don’t think there is an error in the code you posted, because errorLog show Invalid Registration

+11
source

If you are retrieving a token from the database, make sure that the data type for the column is text or very long varchar, for example 200. The token is more than 100 characters long, so it can be cut in half to save in the database.

+9
source

I solved this problem by sending a device registration id, e.g.

 <?php require_once('loader.php'); // return json response $json = array(); $registatoin_ids = array("APA91bHhzESU4GQKwS_I-sMMpAPZ44GbC0wvDAO-Dch87_jwJzaB1gK5PPeaGmt6vFZx2Bvd6AC8EkYDl6PurNUDO6zLoxX50q7gLc8GMOLGcwmQgiWwSZS-PPOFn1MpA5hz_TFQ6S-4tstLO"); $message = array("product" => "shirt"); $result = send_push_notification($registatoin_ids, $message); echo $result; ?> 

in register.php file. U will get this device registration id from google cloud.

0
source

All Articles