当前位置: 首页 > 面试题库 >

在GoogleCloudMessaging API中,如何处理注册ID的续订或到期?

瞿文柏
2023-03-14
问题内容

如问题所述,如何确定何时在GoogleCloudMessaging
API中注册ID无效?我已经阅读了有关类似主题的几个问题的答案:GCM注册ID是否过期?以及Google Coud
Mesaging(GCM)和registration_id到期,我怎么知道?。这些问题的问题在于,答案是使用GCMRegistrar而不是GoogleCloudMessaging
API的旧GCMAPI。前两种方法已贬值。)C2DM

我将 逐步解决我的困惑/问题

1) 在“ 启用GCM
”标题下,第二点是:

Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

The registration ID lasts until the Android application explicitly unregisters itself, or until Google refreshes the registration ID for your Android application. Whenever the application receives a com.google.android.c2dm.intent.REGISTRATION intent with a registration_id extra, it should save the ID for future use, pass it to the 3rd-party server to complete the registration, and keep track of whether the server completed the registration. If the server fails to complete the registration, it should try again or unregister from GCM.

2)
现在,如果是这种情况,那么我应该在BroadcastReceiver中处理意图,然后再次发送register()请求以获取新的注册ID。但是问题在于,在同一页面上标题ERROR_MAIN_THREAD下的
内容是: GCM methods are blocking. You should not run them in the main thread or in broadcast receivers

3) 我还了解到,注册ID发生更改时,还有其他两种情况(如“
使注册状态保持同步”标题下的“高级主题” 所述):应用程序更新和备份与还原。我已经在打开应用程序时处理了它们。

4) 在GCMRegistrar
API,内部GCMBaseIntentService,过去有一个回调
onRegistered()方法中,当设备注册得到其中得到调用。在这里,我曾经保留注册ID并将其发送给第三方服务器

但是,现在我应该如何处理注册ID的更新或更新,将其保留并发送给第三方服务器?

可能是由于阅读全部内容而使我感到困惑,或者我错过了一些东西。非常感谢您的帮助。

更新资料

即使在Android线程上处理GoogleCloudMessaging中的注册ID更改时,也没有提及如何处理Google定期刷新ID?


问题答案:

我正在给出一种方法,就像我在应用程序中实现的一样

@Override
protected void onRegistered(Context context, String registrationId) {
     Log.i(TAG, "Device registered: regId = " + registrationId);
     //displayMessage(context, getString(R.string.gcm_registered));
     //ServerUtilities.register(context, registrationId);
     //1. Store this id to application Prefs on each request of device registration
     //2. Clear this id from app prefs on each request of device un-registration  
     //3. Now add an if check for new registartion id to server, you can write a method on server side to check if this reg-id matching for this device or not (and you need an unique identification of device to be stored on server)
     //4. That method will clear that if id is matching it meanse this is existing reg-id, and if not matching this is updated reg-id.
     //5. If this is updated reg-id, update on server and update into application prefs.
}

你也可以这样

if reg_id exists_into prefrences then
    if stored_id equals_to new_reg_id then
        do nothing
    else
        say server to reg_id updated
        update prefrences with new id
    end if
else
    update this id to application prefs
    say server that your device is registered
end if

但是,当用户清除应用程序数据并且您将丢失当前的注册表ID时,就会出现问题。

新API示例示例的更新
功劳归功于Eran及其Android上的Google
Cloud Messaging中的)他的答案处理注册ID更改

Google更改了其演示应用程序以使用新界面。他们通过在应用程序本地保留的值上设置过期日期来刷新注册ID。应用启动时,他们会加载其本地存储的注册ID。如果它已“过期”(在演示中这意味着它是7天前从GCM收到的),他们会gcm.register(senderID)再次致电。

这不适用于假设的情况,在这种情况下,Google会为长时间未启动的应用刷新注册ID。在这种情况下,应用程序将不会知道更改,第三方服务器也不会。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mDisplay = (TextView) findViewById(R.id.display);

    context = getApplicationContext();
    regid = getRegistrationId(context);

    if (regid.length() == 0) {
        registerBackground();
    }
    gcm = GoogleCloudMessaging.getInstance(this);
}

/**
 * Gets the current registration id for application on GCM service.
 * <p>
 * If result is empty, the registration has failed.
 *
 * @return registration id, or empty string if the registration is not
 *         complete.
 */
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;
}

/**
 * Checks if the registration has expired.
 *
 * <p>To avoid the scenario where the device sends the registration to the
 * server but the server loses it, the app developer may choose to re-register
 * after REGISTRATION_EXPIRY_TIME_MS.
 *
 * @return true if the registration has expired.
 */
private boolean isRegistrationExpired() {
    final SharedPreferences prefs = getGCMPreferences(context);
    // checks if the information is not stale
    long expirationTime =
            prefs.getLong(PROPERTY_ON_SERVER_EXPIRATION_TIME, -1);
    return System.currentTimeMillis() > expirationTime;
}


 类似资料:
  • 我想知道如何将应用程序/设备通知,如果GCM服务器决定刷新之前从GCM服务器检索的应用程序的注册ID。目前,我保存第一个检索到的注册ID,并将其保存在数据库中,并将此注册ID发送到应用服务器。所以,从下一次应用程序将不会说话的GCM服务器。在这种情况下,应用程序永远不会知道注册ID是否被GCM服务器更改。谷歌文件提出了一些处理此案的建议,但没有提到应该采取什么步骤。引用谷歌- 虽然是com。谷歌。

  • 问题内容: 在Google Cloud Messaging的文档中,它指出: Android应用程序应存储此ID供以后使用(例如,检查onCreate()是否已注册)。请注意,Google可能会定期刷新注册ID,因此您在设计android应用程序时应了解com.google.android.c2dm.intent.REGISTRATION意图可能会多次调用。您的Android应用程序需要能够做出相

  • 问题内容: 我收到此异常: 源自此代码: 哪里是。 所以这对我来说很好- 我知道数据库中存在零日期,我需要能够读取这些日期并将其转换为下游数据结构中的适当数据(可能为空)。问题是我不知道如何检索它并得到“软”错误。我考虑过将这一行包装在SQLException的try / catch中,但了解这会破坏ResultSet的有效性。 是否有可能以其他方式读取此值而不会引发SQLException? 问

  • 问题内容: 我正在开发命令行节点模块,希望能够通过网站上的链接启动它。 我想注册一个自定义协议,以使链接具有以下格式:单击它们将启动节点包。 如果没有为此的节点API(我确定不会),那么有什么办法可以通过调用系统命令从node做到这一点? 它必须在Windows,Linux和MacOS上运行。 问题答案: 这是一个有趣的想法。我认为目前没有跨平台的node.js解决方案。我确实遇到了要求同一件事的

  • 在谷歌云消息的文档中,它指出: Android应用程序应该存储此ID以供以后使用(例如,如果已注册,请检查oncreate())。请注意,Google可能会定期刷新注册ID,因此您应该在设计Android应用程序时了解com.google.android.c2dm.intent.注册意图可能会被多次调用。您的Android应用程序需要能够相应地响应。 我使用以下代码注册我的设备: GoogleCl

  • 注册表是Windows系统的关键要素之一。 它包含有关操作系统各个方面的大量信息。 几乎所有安装在Windows系统上的应用程序都以某种形式与注册表交互。 注册表包含两个基本元素:键和值。 注册表项是与文件夹类似的容器对象。 注册表值是与文件类似的非容器对象。键可能包含值或其他键。 使用类似于Windows路径名称的语法来引用键,并使用反斜杠来指示层次结构的级别。 本章介绍查询值,添加,删除和编辑