2

As I refer here Android GCM messages repeated the problem was I install and reinstall the app many times so it has many regId. I found many posts advice to use Canonical ID. I read many posts but I can't really know how to apply it. Here is my code to get regId:

   public String getRegId(){
            int noOfAttemptsAllowed = 3;   // Number of Retries allowed
              int noOfAttempts = 0;          // Number of tries done
              boolean stopFetching = false;     // Flag to denote if it has to be retried or not
              String regId = "";             


              while (!stopFetching) 
              {
                   noOfAttempts ++;
                   try {
                       regId = gcm.register(PROJECT_NUMBER);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                   try 
                   {

                      Thread.sleep(4000);   // Set this timing based on trial.

                      try {
                           regId = gcm.register(PROJECT_NUMBER);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
               } catch (InterruptedException e) {
                e.printStackTrace();
               }
                  /* try
                   {
                        // Get the registration ID
                        regId = gcm.register(PROJECT_NUMBER);
                   } catch (Exception e) {}*/


                   if (!regId.equals("") || noOfAttempts > noOfAttemptsAllowed)
                   {
                        // If registration ID obtained or No Of tries exceeded, stop fetching
                        stopFetching = true;
                    }
                    if (!regId.equals(""))
                    {
                        // If registration ID Obtained, save to shared preferences
                           SharedPrefrencesMethods.savePreferences(activity, "regid", regid);
                    }


               }
              return regId;
        }
Community
  • 1
  • 1
Radwa
  • 325
  • 4
  • 21

1 Answers1

2

Yes. You can use canonical IDs to help you more easily recover from your errors. It is the registration token of the last registration requested by the client app. This is the ID that the server should use when sending messages to the device.

If you try to send a message using an old registration token, GCM will process the request as usual, but it will include the canonical ID in the registration_id field of the response. Make sure to replace the registration token stored in your server with this canonical ID, as eventually the old registration token will stop working.

Based from this blog, GCM service returns the cannonical IDs in the order notifications were sent. Here is a sample response:

{
    "multicast_id": 7036866281258904189,
    "success": 1,
    "failure": 0,
    "canonical_ids": 1,
    "results": [
        {
            "registration_id": "CANNONICAL_REGISTRATION_ID",
            "message_id": "0:1415529915241995%64ac3713f9fd7ecd"
        }
    ]
}

The canonical id = 0 means that registration id which your push sever used is OK and not should be replaced by canonical id, i.e. often GCM server will be response canonical_id = 0. In the example response there's one cannonical id and it means that your server has to replace existing registrtation id on new value which you see in response. This case easy reproduce if user reinstall your client application, but your push server doesn't know about it and GCM server will pass in response new registration id.

Check these related SO questions about Canonical IDs:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
  • Alright thanks to you I understand now but i have small problem I don't know where to receive server response on which method I mean I have onHandleIntent intent method i tried to can get the message itself only ? – Radwa Apr 26 '16 at 08:56
  • Hello I mange to get the response from server and get canonical Ids but I found many canonical id which one should I use this is part of json response {"success":17,"failure":15,"canonical_ids":16} – Radwa Apr 26 '16 at 12:58