gpt4 book ai didi

android - GoogleCloudMessaging#register 在同一应用程序/设备中返回不同的 ID?

转载 作者:太空狗 更新时间:2023-10-29 13:22:57 24 4
gpt4 key购买 nike

我正在使用 GoogleCloudMessaging 在我的应用中实现通知功能。但我有一个问题,当我卸载我的应用并重新安装时,我得到了2 个不同的注册 ID,这导致向我的应用发送重复通知 .

the document ,他们说:“重复调用此方法将返回原始注册 ID

public String register (String... senderIds)

Register the application for GCM and return the registration ID. You must call this once, when your application is installed, and send the returned registration ID to the server.

Repeated calls to this method will return the original registration ID.

但是在another document ,他们想出了一个叫做“Canonical IDs”的东西:

Canonical IDs On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

那么我怎样才能使它在所有设备上保持一致呢?我的(第三方)服务器仅存储注册 ID,这些 ID 将用于发送通知。现在,“规范 ID”来了,一切都变得如此复杂!

这是否意味着我必须在注册时为每个设备发送一个唯一的标识号?

这是我用来将设备注册到 GoogleCloudMessaging 的代码块:

try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
Log.d(TAG, "########################################");
Log.d(TAG, "Current Device's Registration ID is: " + regid);
Map<String, String> data = new HashMap<String, String>();
data.put("regId", regid);
try {
Utils.post(Constants.SERVER_NOTIFY_REG_LINK, data);
Log.d(TAG, "ID registered: " + regid);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
Log.d(TAG, msg);
}

最佳答案

是的,在重新安装后注册该应用程序可能会为您提供一个新的注册 ID。您可以在服务器端或客户端处理它:

  • 客户端:为应用程序的每个实例分配一个唯一的实例 ID。将该 ID 存储在外部存储中,以便在卸载应用程序时不会将其删除。将该 ID 与注册 ID 一起发送到您的服务器,如果您在数据库中发现与同一实例 ID 关联的旧注册 ID,则将旧注册 ID 替换为新注册 ID。

  • 服务器端:每当您从 GCM 的响应中获得规范注册 ID 时,请从您的数据库中删除旧注册 ID(如果规范注册 ID 不在您的数据库中,则插入规范注册 ID)。

我建议你两者都做,因为第二个只能在你收到重复消息后解决问题,而第一个不是完整的证明(因为外部存储可能被删除或不可用)。

关于android - GoogleCloudMessaging#register 在同一应用程序/设备中返回不同的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com