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;
}