GCM令牌何时过期以及什么是InstanceID? [英] When do GCM Tokens Expire and What is the InstanceID?

查看:149
本文介绍了GCM令牌何时过期以及什么是InstanceID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于GCM不断更新,我搜索到的大部分资源似乎过时或不清楚。基本上,我对令牌和ID到期时感到困惑。 (仅供参考,我正在使用Android。)



据我所知(如果我错了,请纠正我),我的服务器有一个API密钥和一个发件人ID。使用发件人ID我可以让我的客户端通过本地存储在我的客户端上的InstanceID请求一个令牌。我在这里已经有点困惑了。 InstanceID是在我的应用上线的时候分配的?它有没有改变?什么时候应用程序升级或卸载并重新安装(或设备恢复)?通过调用InstanceID.getInstance将我总是检索相同的InstanceID,或者它最终会过期并给我一个新的?通过调用getID()来存储您检索的字符串是否有任何价值?这些文档似乎表明,当您调用getID()时,实际上您会检索到新的InstanceID,从而使事情变得更复杂。 (仅供参考,我指的是: https://developers.google.com/instance-id/

使用InstanceID,我的客户端可以从GCM服务器请求一个令牌,然后发送到我的应用服务器。我的应用程序服务器存储这个令牌,并可以使用它将消息发送到GCM服务器,然后这些服务器将消息发送到设备。我相信设备使用存储的InstanceID实际接收这些消息。因此,拥有一个扩展GcmListenerService的类将允许我使用onMessageReceived接收这些消息?我不必做任何特别的事情(除了在AndroidManifest中定义它)?我不必实际告诉它使用InstanceID?它只是奇迹般地知道?



何时这些ID和令牌过期?他们过期了吗?我将令牌作为字符串存储在服务器上,但如果其中一个过期,我怎么知道他们已经过期?我总是可以生成一个新的InstanceID和Token,这看起来很简单,但是旧的那些保持活跃?我如何从服务器擦除旧的令牌?似乎有一种简单的方法可以在iOS上对APNS执行此操作,您可以在其中检索所有过期令牌的列表,并从数据库中将其擦除。

解决方案

当我更新GCM实施时,我发现自己问自己大部分问题。几天后,这里是我对你的提问。


从我的理解中(请纠正我,米错),我的服务器有一个API密钥和发件人ID。使用发件人ID我可以让我的客户端通过客户端本地存储的InstanceID请求一个令牌。


这是正确的。 p>


InstanceID是在我的应用程序上线的时候分配的?


看起来只要您的应用程序启动即可分配,即使设备无法访问互联网。


它会改变吗?什么时候应用程序升级或卸载并重新安装(或设备恢复)?通过调用InstanceID.getInstance将我总是检索相同的InstanceID,或者它最终会过期并给我一个新的?

根据 InstanceID文档
$ b


实例ID很稳定,但可能会失效,如果:


  • 应用程序删除实例ID

  • 设备出厂重置


  • $ b

    如果实例ID是


  • 用户清除应用程序数据
  • 已经失效,应用程序可以调用getId()来请求新的实例ID。


我已经测试了卸载应用程序并清除数据,结果指出上述所有内容都是正确的。


存储通过调用getID()检索的字符串是否有任何价值?

它看起来像API句柄将它存储在您应用程序的本地存储中。


使用InstanceID,我的客户端可以从GCM服务器请求一个令牌,然后将其发送到我的应用服务器。我的应用程序服务器存储这个令牌,并可以使用它将消息发送到GCM服务器,然后这些服务器将消息发送到设备。我相信设备使用存储的InstanceID实际接收这些消息。因此,拥有一个扩展GcmListenerService的类将允许我使用onMessageReceived接收这些消息?我不必做任何特别的事情(除了在AndroidManifest中定义它)?我不必实际告诉它使用InstanceID?它只是神奇地知道吗?

据我所知,在前面的实现中没有任何类型的InstanceId,它不会看起来它也被明确地用在这个中。如果是,则在 GcmReceiver GcmListenerService


何时这些ID和令牌过期?他们过期了吗?


我已经解决了ID的过期问题,我们可以了解有关在 Android InstanceID实施指南
$ b


实例ID服务定期(例如,每6个月)启动一次回调,请求您的应用刷新其令牌。它可能还会在以下情况下启动回调:


  • 存在安全问题;例如,SSL或平台问题。

  • 设备信息不再有效;例如,备份和还原。

  • 实例ID服务受到其他影响。


本指南指出要继承 InstanceIDListenerService 并覆盖 onTokenRefresh()来处理这些情况。


I将令牌作为字符串存储在服务器上,但是如果其中任何一个过期,我怎么知道它们已经过期?


在您的服务器上实施GCM的指南指出,GCM服务器会响应您的服务器,并提供有关用于发送推送通知的令牌的一些信息。


我总是可以生成一个新的InstanceID和Token,这看起来很容易,但是旧的仍然保持活动状态?

我的测试表明是的,他们这样做。


如何从服务器擦除旧的令牌?似乎有一种简单的方法可以在iOS上使用APNS执行此操作,您可以从中检索所有过期令牌的列表,并从数据库中将其擦除。


我仍在研究这个问题,如果能找出问题,我会更新它。

Since GCM keeps getting updated, most of the resources I have searched seem outdated or unclear. Basically, I am confused over when the tokens and ID's expire. (For reference, I am working with Android.)

From what I understand (and please correct me if I'm wrong), my server has an API Key and a Sender ID. Using the Sender ID I can have my client request a Token via the InstanceID stored locally on my client. I'm already a bit confused here. The InstanceID is assigned the moment my app goes online? Does it ever change? What about when the app upgrades or is uninstalled and reinstalled (or the device is restored)? By calling InstanceID.getInstance will I always retrieve the same InstanceID, or will it eventually expire and give me a new one? Is there any value to storing the string you retrieve by calling getID()? The docs seem to indicate that you actually retrieve a new InstanceID when you call getID(), so that complicates things even more. (For reference, I'm referring to: https://developers.google.com/instance-id/)

Using the InstanceID, my client can request a Token from the GCM servers, which it then sends to my app server. My app server stores this Token, and can use this to send messages to the GCM servers, which will then send the message to the device. The device uses the stored InstanceID to actually receive these messages, I believe. So having a class that extends GcmListenerService will allow me to receive these messages with onMessageReceived? I don't have to do anything special (other than defining it in the AndroidManifest)? I don't have to actually tell it to use the InstanceID? It just magically knows?

When do these ID's and Tokens expire? Do they expire? I store the Token as a string on the server, but if at any point one of these expires, how do I know they have expired? I can always generate a new InstanceID and Token, that much seems easy, but then do the old ones stay active? How do I wipe the old tokens from the server? There seems to be an easy way to do this with APNS on the iOS side of things, where you can retrieve a list of all the expired tokens and just wipe them from your database.

解决方案

I've found myself asking most of these questions myself as I update my GCM implementation. After messing around with it a few days, here's my take on your questions.

From what I understand (and please correct me if I'm wrong), my server has an API Key and a Sender ID. Using the Sender ID I can have my client request a Token via the InstanceID stored locally on my client.

This is correct.

The InstanceID is assigned the moment my app goes online?

It looks like it's assigned as soon as your app launches, even if the device can't access the Internet.

Does it ever change? What about when the app upgrades or is uninstalled and reinstalled (or the device is restored)? By calling InstanceID.getInstance will I always retrieve the same InstanceID, or will it eventually expire and give me a new one?

According to the InstanceID documentation:

Instance ID is stable but may become invalid, if:

  • App deletes Instance ID
  • Device is factory reset
  • User uninstalls the app
  • User clears app data

If Instance ID has become invalid, the app can call getId() to request a new Instance ID.

I've tested uninstalling the app and clearing the data, and the results point to all of the above being true.

Is there any value to storing the string you retrieve by calling getID()?

It looks like the API handles storing this in your app's local storage for you.

Using the InstanceID, my client can request a Token from the GCM servers, which it then sends to my app server. My app server stores this Token, and can use this to send messages to the GCM servers, which will then send the message to the device. The device uses the stored InstanceID to actually receive these messages, I believe. So having a class that extends GcmListenerService will allow me to receive these messages with onMessageReceived? I don't have to do anything special (other than defining it in the AndroidManifest)? I don't have to actually tell it to use the InstanceID? It just magically knows?

As far as I can tell there wasn't any sort of InstanceId in the previous implementation, and it doesn't look like it's being explicitly used in this one either. If it is, it's being called within either GcmReceiver or GcmListenerService.

When do these ID's and Tokens expire? Do they expire?

I've already addressed ID's expiring and we can find out about Tokens expiring in the Android InstanceID implementation guide:

The Instance ID service initiates callbacks periodically (for example, every 6 months), requesting that your app refreshes its tokens. It may also initiate callbacks when:

  • There are security issues; for example, SSL or platform issues.
  • Device information is no longer valid; for example, backup and restore.
  • The Instance ID service is otherwise affected.

The guide says to subclass InstanceIDListenerService and override onTokenRefresh() to handle these scenarios.

I store the Token as a string on the server, but if at any point one of these expires, how do I know they have expired?

The guide for implementing GCM on your server says that the GCM server will respond to your server with some information about the token you used to try to send the push notification.

I can always generate a new InstanceID and Token, that much seems easy, but then do the old ones stay active?

My tests suggest that yes, they do.

How do I wipe the old tokens from the server? There seems to be an easy way to do this with APNS on the iOS side of things, where you can retrieve a list of all the expired tokens and just wipe them from your database.

I'm still looking into this and will update if I can figure something out.

这篇关于GCM令牌何时过期以及什么是InstanceID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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