注销并重新注册为GCM的消息引起两个REGID的是有效的。这是因为预期?的是、这是因为、有效、两个

2023-09-12 08:11:35 作者:说再多不如沉默

做注册/注销用于在Android设备GCM消息时,我注意到一些奇怪的行为。

:从客户端设备的透视遵守以下用例 注册GCM - ID的 A 分配 注销 注册GCM - ID的 B 分配

如果,第2步后,服务器将尝试将消息发送给ID的 A ,将获得 NotRegistered 错误的作为记录和预期。

但是,现在的怪部分:第3步之后,的两个的ID的 A 和 B 的有效身份证件!两者的ID将触发装置上的意向接收器,导致两个消息到该应用

这是行为expeced还是我做错了什么?

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

 公共无效的onCreate(包savedInstanceState){
     super.onCreate(savedInstanceState);
     注销(getApplicationContext());
     注册(getApplicationContext());
}


/ ** *注册/此设备GCM消息
公共静态无效寄存器(上下文的背景下){
    GCMRegistrar.checkDevice(上下文);
    GCMRegistrar.checkManifest(上下文);
    字符串REGID = GCMRegistrar.getRegistrationId(上下文);
    如果(regId.equals()){
        GCMRegistrar.register(背景下,SENDER_ID);
    } 其他 {
        storeRegId(REGID); //还通知后端
    }
}

公共无效注销(上下文的背景下){
    GCMRegistrar.unregister(上下文);
}
 
为什么注会通过两科更加迷茫 还需要继续考下去吗

注1:的我只包括了注销() - 调用调试。我的应用程序通常保持注册的为人生(我也想收到GCM消息而暂停,终止的),但我还是想弄清楚这种行为的原因,因为我不知道,如果注销是唯一的情况下ID被再生。如果那用户卸载并重新安装应用程序?我想防弹系统 - 我的用户永远不会收到相同的GCM消息两次

注2:的问题是pretty的similar这,但我确实是用注册getApplicationContext()作为答案提示。

解决方案

当您注销,你应该送旧注册ID为您的服务器,并从数据库中删除。

假设你做不到的事情步骤1中,如果注册并获得新的注册ID后,您发送邮件与旧注册ID,从谷歌的响应将包含一个规范的注册ID(也就是新的注册ID )。这种反应表明您的服务器应该删除旧的注册ID,并只使用新的。

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:

Register for GCM -- ID A assigned Unregister Register for GCM -- ID B assigned

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

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?

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);
}

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.

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

解决方案

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

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.