【发布时间】:2016-04-25 10:40:37
【问题描述】:
正如我在这里提到的
Android GCM messages repeated
问题是我多次安装并重新安装该应用程序,所以它有很多regId。我发现很多帖子建议使用 Canonical ID。我读了很多帖子,但我真的不知道如何应用它。这是我获取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;
}
【问题讨论】:
标签: android google-cloud-messaging