我知道这个问题已经问过几次了,但我没有找到令人满意的答案,因为Google的GCM教程中有一部分内容让我很不爽:
给定的regID不能无限期地持续,所以应用程序应该做的第一件事就是检查它是否有一个有效的regID (如上面的代码片段所示)。
我保存返回的ID和应用程序版本的表上设备和每次在启动活动应用程序检查ID和注册设备,如果ID不存在。
但是,它们意味着"regID不能保证无限期地持续下去“。如果设备只在共享首选项或表中本地检查,设备如何知道它是否丢失了GCM服务器上的regID?
发布于 2014-11-21 10:02:20
通过代码,谷歌似乎只想让我们重新注册应用程序更新。
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
// Check if app was updated; if so, it must clear the registration ID
// since the existing regID is not guaranteed to work with the new
// app version.
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}GCMRegistrar.getRegistrationId也做了同样的事情。现在是已弃用。
public static String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
// check if app was updated; if so, it must clear registration id to
// avoid a race condition if GCM sends a message
int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int newVersion = getAppVersion(context);
if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
Log.v(TAG, "App version changed from " + oldVersion + " to " +
newVersion + "; resetting registration id");
clearRegistrationId(context);
registrationId = "";
}
return registrationId;
}编辑
通过文档和代码(至少),我找不到任何其他方式让客户知道它是否需要重新注册。但是,当试图发送通知到过期或重复的注册ID时,您可以得到一些服务器错误响应。如果注册ID已过期,应用服务器可以要求客户重新注册,如果是复制应用服务器,则应将现有的reg id替换为Google返回的canonical_id。
有关如何解释这些错误消息的更多细节,请阅读此链接。
希望这能有所帮助。
发布于 2014-11-21 09:51:41
我已经在Splash屏幕上放置了一个代码片段,它检查来自Google服务器的regId,并根据结果进行相应的处理。如果regId为null,则创建一个新的。如果它返回一个有效的regId,我们将根据保存在首选项中的Id来检查这个Id。如果发生任何更改,我们将更新首选项中的Id以及服务器的Id。
/**
* Method is used to get/check regId for Google cloud messaging.
*
* @param context
* - Context of the class from where it is called.
*
* @return void
*/
public static String myRegistrationId(Context context) {
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
context.registerReceiver(mHandleMessageReceiver, new IntentFilter(GCMUtilities.DISPLAY_MESSAGE_ACTION));
// this line of code checks if there exists a valid regId for this
// device otherwise it will return null value.
String regId = GCMRegistrar.getRegistrationId(context);
System.out.println("My registeration id is " + regId);
//if null value is returned we will create a new regId
if (regId.equals("")) {
System.out.println("Registering app on start up");
GCMRegistrar.register(context, GCMUtilities.GCM_APP_KEY);
System.out.println("Registering done");
regId = GCMRegistrar.getRegistrationId(context);
System.out.println("My registeration id is " + regId);
//Utilities.showDLog("~~~~~~~" +"" +"" +"~~~~~~~", "=====regId=====" + regId);
}
else{ //returns a valid regId
/**
* Compare this regId against our existing RegId stored in preferences and in
* case of any change update it to our server as well as update preferences.
*/
// here goes the code to update preferences
// and also update the new Id at the server
}
/*
* Save GCM registration id
*/
Constants.GCM_DEVICE_TOKEN = regId;
return regId;
}https://stackoverflow.com/questions/27057925
复制相似问题