取消注册和重新注册 GCM 消息会导致两个 regId 有效.这是预期的吗? [英] Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended?

查看:14
本文介绍了取消注册和重新注册 GCM 消息会导致两个 regId 有效.这是预期的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 设备上注册/取消注册 GCM 消息时,我注意到一些奇怪的行为.从客户端设备的角度观察以下用例:

I have noticed some strange behaviour when doing registering/unregistering for GCM messages on an Android devices. Observe the following use case from the perspective of the client device:

  1. 注册 GCM -- 分配了 ID A
  2. 取消注册
  3. 注册 GCM -- 已分配 ID B

如果在第 2 步之后,服务器尝试向 ID A 发送消息,它将收到 NotRegistered 错误,如记录和预期.

If, after step 2, the server attempts to send a message to ID A, it will receive a NotRegistered error, as documented and expected.

但现在奇怪的部分是:在第 3 步之后, ID AB 都是有效 ID!这两个 ID 都会触发设备上的 Intent 接收器,从而向应用发送两条消息.

But now the strange part: After step 3, both ID A and B are valid IDs! Both IDs will trigger the Intent receiver on the device, resulting in two messages to the app.

这种行为是否符合预期,还是我做错了什么?

Is this behaviour as expeced or am I doing something wrong?

这是我的注册和注销代码,在我的应用程序上启动的第一个活动时从 onCreate() 触发:

This is my code to register and unregister, triggered from onCreate() on the first activity launching on my app:

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     unregister(getApplicationContext());
     register(getApplicationContext());
}


/** Registers this device for GCM messages */
public static void register(Context context) {
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    String regId = GCMRegistrar.getRegistrationId(context);
    if (regId.equals("")) {
        GCMRegistrar.register(context, SENDER_ID);
    } else {
        storeRegId(regId); // Also notifies back-end
    }
}

public void unregister(Context context) {
    GCMRegistrar.unregister(context);
}

注意 1: 我只包含了用于调试目的的 unregister() 调用.我的应用程序通常会终身"注册(我也想在暂停和终止时接收 GCM 消息),但我仍然想找出这种行为的原因,因为我不确定取消注册是否是唯一的情况重新生成 ID.如果用户卸载并重新安装应用程序怎么办?我想要一个防弹系统 - 我的用户永远不会收到两次相同的 GCM 消息.

Note 1: I've only included the unregister()-call for debugging purposes. My app usually stays registered for "for life" (I also want to receive GCM messages while suspended and terminated), but I still want to figure out the cause of this behaviour as I'm not sure if unregistering is the only case where the IDs are regenerated. What if the user uninstalls and reinstalls the app? I want a bullet proof system - my users shall never receive the same GCM message twice.

注意 2: 问题很类似对此,除了我确实按照答案所暗示的那样使用 getApplicationContext() 注册.

Note 2: The problem is pretty similar to this, except that I am indeed registering with getApplicationContext() as the answer suggests.

推荐答案

  1. 当您取消注册时,您应该将旧的注册 ID 发送到您的服务器并将其从您的数据库中删除.

  1. When you un-register, you should send the old registration ID to your server and remove it from your DB.

假设您没有执行第 1 步,如果您在注册并获取新注册 ID 后使用旧注册 ID 发送消息,则来自 Google 的响应将包含规范注册 ID(即新注册 ID).此响应表明您的服务器应删除旧的注册 ID 并仅使用新的注册 ID.

Assuming you failed to do step 1, if after registering and getting the new registration ID you send a message with the old registration ID, the response from Google will contain a Canonical Registration ID (which is the new registration ID). This response indicates that your server should delete the old registration ID and use only the new one.

这篇关于取消注册和重新注册 GCM 消息会导致两个 regId 有效.这是预期的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆