简体   繁体   English

如何知道我的应用是否具有实例ID /是否已在GCM中注册?

[英]How to know if my app has an instance id/is registered with GCM?

I need to know if i have an instance id when my user logs in to the app in a deterministic manner. 我需要确定用户登录应用程序时是否有实例ID。 Is there any other way to know other than trying to get an instance id and handling the possible exception? 除了尝试获取实例ID和处理可能的异常之外,还有其他方法可以知道吗? - That approach is prone to failing because the documentation only mentions an IOException, which might be raised because of a network exception or some other IO error. -这种方法很容易失败,因为文档仅提到IOException,它可能是由于网络异常或其他IO错误而引发的。

I am tracking the value myself with SharedPreferences but that seems a bit fickle as the app may not be used for a bit of time and the value might get out of sync. 我自己使用SharedPreferences跟踪值,但这似乎有些变幻莫测,因为该应用程序可能没有使用一段时间,并且该值可能不同步。

An Android application running on a mobile device registers to receive messages by calling the Google Cloud Messaging method register. 在移动设备上运行的Android应用程序通过调用Google Cloud Messaging方法注册器进行注册以接收消息。 This method registers the application for GCM and returns he registration ID. 此方法注册GCM的申请并返回注册ID。

Based from this Demo App , they refresh the registration ID by setting an expiration date on the value persisted locally by app. 他们基于此演示应用程序 ,通过在应用程序本地保留的值上设置一个过期日期来刷新注册ID。 they load their locally stored regstration id. 他们加载其本地存储的注册ID。 If it is expired they call gcm.register() . 如果expired则调用gcm.register()

This doesn't handle the hypothetical scenario in which a registration ID is refreshed by Google for an app that hasn't been launched for a long time. 这不适用于假设的情况,在这种情况下,Google会为长时间未启动的应用刷新注册ID。

Your app can request tokens from the Instance ID service as needed using the getToken() method, and like InstanceID , your app can also store tokens on your own server. 您的应用程序可以根据需要使用getToken()方法从实例ID服务请求令牌,并且像InstanceID一样,您的应用程序还可以将令牌存储在您自己的服务器上。 All tokens issued to your app belong to the app's InstanceID. 发行给您应用的所有令牌均属于该应用的InstanceID。

Tokens are unique and secure, but your app or the Instance ID service may need to refresh tokens in the event of a security issue or when a user uninstalls and reinstalls your app during device restoration. 令牌是唯一且安全的,但是如果出现安全问题或在设备还原期间用户卸载并重新安装您的应用程序,则您的应用程序或实例ID服务可能需要刷新令牌。 Your app must implement a listener to respond to token refresh requests from the Instance ID service. 您的应用必须实现一个侦听器,以响应来自实例ID服务的令牌刷新请求。

You may check this documentation of how to do proper implementation: 您可以查看此文档以了解如何正确实施:

Here's a related SO ticket, discuss how to check if the device is registered: 这是相关的SO凭单,讨论如何检查设备是否已注册:

When using InstanceID you call getToken to retrieve the token from local cache (in Google Play services) if it already exists or make a network call to retrieve a new token. 使用InstanceID时,您可以调用getToken从本地缓存(在Google Play服务中)检索令牌(如果已存在),或者进行网络调用以检索新令牌。 So if your app already has a token then a call to getToken should return immediately since it is a local call. 因此,如果您的应用程序已经具有令牌,则对getToken的调用应立即返回,因为它是本地调用。 If you get an IOException then that is a clear signal that your app does not currently have a token. 如果您收到IOException,则表明您的应用当前没有令牌。

Another option that you should consider is using the new FCM client library. 您应该考虑的另一种选择是使用新的FCM客户端库。 It handles the token requesting and refreshing for you, so all you need is a call to FirebaseInstanceId.getToken() which will return a token if one exists or null if one does not. 它为您处理请求和刷新的令牌,因此您需要的是对FirebaseInstanceId.getToken()的调用,如果存在一个令牌,它将返回一个令牌;如果不存在,则返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM