如何检查设备是否在GCM注册,因为官方教程声明"regID不能无限期地持续下去“
通过代码,谷歌似乎只想让我们重新注册应用程序更新。
代码语言:javascript运行复制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也做了同样的事情。现在是已弃用。
代码语言:javascript运行复制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。
有关如何解释这些错误消息的更多细节,请阅读此链接。
希望这能有所帮助。