Android GCM设备ID是否会改变?

时间:2013-09-10 15:12:20

标签: android google-cloud-messaging

我正在将GCM构建到我的应用程序中,并且我想确保我最终没有重复的设备ID。

是否有处理设备ID的“最佳做法”?我原来的是,当应用程序加载时调用gcm.register,并将ID发布到我的服务器,如果密钥不在我的数据库中,我将存储它,然后当我发出通知时,循环数据库和发送消息。

但我想知道ID是否有变化,我不想向一台设备发送多条消息。

3 个答案:

答案 0 :(得分:11)

是的,ID可能会发生变化。如果您查看SDK中的教程,每次将应用程序更新为新版本时,它都将获取新ID,因为之前的ID无法保证正常工作。

具体来看看这个页面

http://developer.android.com/google/gcm/client.html

 Check if app was updated; if so, it must clear the registration ID
 since the existing regID is not guaranteed to work with the new
 app version.

答案 1 :(得分:5)

当GCM注册ID可能发生变化时,我读了here的两个原因:

  1. 每次更新时,您都需要重新注册每台设备 应用
  2. 如果设备的Android版本已更新,您还需要重新注册设备
  3. P.S:以下旧答案的参考已从Google的页面中删除。

    除了@ tyczj在更新应用程序时更改ID的答案,谷歌表示它还可以自动刷新ID。如果您阅读Architectural Overview页面上的启用GCM标题下的第二个点,则会显示:

    Note that 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.

    只是一个附加信息,为了处理这种情况,您应该有Broadcast Listener可以处理com.google.android.c2dm.intent.REGISTRATION意图,Google必须刷新注册ID时才会发送给应用。广播接收器将具有带有Intent的onReceive方法。根据意图,您可以使用Bundle从中提取Google的新注册ID。您可以将其保存并发送到第三方服务器,以替换该用户以前注册的ID。

答案 2 :(得分:0)

我认为如果您可以在每次登录时验证或更新基于登录的应用和ID,那会更好。我有一个类似的应用程序,它正在为我工​​作。