android - 处理 GCM 客户端中的注册 ID 更改

标签 android push-notification google-cloud-messaging

我正在阅读这里的文档:http://developer.android.com/google/gcm/gs.html

查看此方法:

private String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    if (registrationId.length() == 0) {
        Log.v(TAG, "Registration not found.");
        return "";
    }
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion || isRegistrationExpired()) {
        Log.v(TAG, "App version changed or registration expired.");
        return "";
    }
    return registrationId;
}

SharedPreferences 中没有保存 id 或应用程序版本更改或注册过期时,它返回空字符串。

在所有这三种情况下,都会调用一个方法 registerBackground()。 此方法向我的 Web 服务器发出 HTTP 请求,服务器将注册 ID 存储在数据库中。

但如果发生这种情况,每次版本更新或注册到期都会在我服务器上的 TABLE 中创建一个新行。

有没有办法在客户端发生注册 ID 更改事件?因为如果发生这种情况,我将更新 TABLE 中的行。

最佳答案

您可以稍微更改代码以实现您想要的。

当存储在共享首选项中的旧注册 ID 过期时,将其存储在某个地方。然后,在从 Google 获得新的注册 ID 后,将旧 ID 和新 ID 发送到您的服务器,并更新数据库而不是创建新行。

关于android - 处理 GCM 客户端中的注册 ID 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17007326/

相关文章:

ios - 应用程序在前台时未触发通知-iOS

android - 在某些情况下,Android 中 GCM ID 不可用的原因是什么

Android - GCMRegistrar.checkDevice() 的 UnSupportedOperation 异常

没有根点的android崩溃异常

android - 使用 TextView 显示表情符号

android - Android Youtube API 是否使用官方 Youtube 应用程序播放视频?

android - 当应用程序在后台使用数据负载时如何处理 Android FCM 消息?

android.database.CursorIndexOutOfBoundsException : Index -1 requested, 大小为 5

ios - iOS 的 FCM 自定义通知

android - 无法解析 Intent Service Android