InstanceID returns 无效令牌

InstanceID returns invalid token

我在 Android 上使用 GCM

我使用 InstanceID.getInstance(...).getToken(...) 接收推送令牌,但在某些情况下(应用程序更新或重新安装后)我收到无效令牌。

当服务器 return 出现 NotRegistered 错误时,我连接了调试器并调用了 InstanceID.getInstance(...).getToken(...)。但是此令牌无效(我已尝试使用此令牌通过 curl -s "https://android.googleapis.com/gcm/send" ... 发送推送),我收到 NotRegistered 错误。

为什么 instanceID 会 return 无效令牌?

它不应该给出无效的令牌。

你申请了所有程序吗?

  • InstanceIDListenerService:当令牌通过应用更改时 更新等
  • RegistrationIntentService:当令牌发生变化时,您通过 InstanceIDListenerService 接收它并调用此 Intent 以获取新的 令牌。

终于找到了解决办法

我在两个不同的线程中使用 instanceID。我同时打了两次getToken(...)。如果实例没有缓存,它会从网络获取令牌。我认为,它在我的案例中发送了两个请求,并且不能保证它的顺序。所以 instanceID 缓存了一个令牌,但 google 云了另一个。