在 iOS 上获取 GCM 推送通知的注册令牌的正确顺序?GCM 不可靠吗? [英] Proper sequence to get registration token for GCM push notification on iOS? Is GCM unreliable?

查看:23
本文介绍了在 iOS 上获取 GCM 推送通知的注册令牌的正确顺序?GCM 不可靠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经学习了在 iOS 上使用 GCM 的教程.它一直在间歇性地工作(这意味着所有的证书、权限和东西都没有问题).但是最近,我反复收到两条错误消息:

GCM |GCM 注册尚未准备好使用身份验证凭据.

此外,重新连接到 GCM 失败:

Error Domain=com.google.gcm Code=501 "(null)"

这部分与设备未获得 GCM 注册 ID 相关.最近有没有其他人更频繁地遇到这些问题?还是因为我调用 GCM API 的顺序不正确(尤其是 connectWithHandler:startWithConfig:tokenWithAuthorizedEntity 方法)?我怀疑原因是后者,因为我确实在一些延迟后获得了 GCM ID.

我也不总是收到 GCM ID.当我没有收到时,我通常必须通过 Xcode 再运行一次或两次应用程序.(或通过强制关闭应用程序).显然,这不是我的用户应该做的事情.

这是我的 GCM API 调用顺序:

  1. 设备获得一个 APNS 令牌
  2. 然后我打电话tokenWithAuthorizedEntity: 使用我的 APNS 令牌
  3. ^ 这通常是导致上述两个错误之一.
  4. 每当我实际上需要一个 GCM 令牌,我强制重新获取 GCM 令牌再次调用 tokenWithAuthorizedEntity.

此外,我的 applicationDidBecomeActive: 方法中也编写了 connectWithHandler: 调用.

几个问题:

  1. 如果我只对接收 GCM 推送消息感兴趣而向上游发送消息,是否需要在 applicationDidBecomeActive: 中调用 connectWithHandler: ?
  2. 如果对 (1) 的回答是肯定的,那么在该方法的完成处理程序中,如果发生错误,并且此时我没有 GCM 令牌,我是否应该再次尝试获取令牌?(即调用 tokenWithAuthorizedEntity?)
  3. 什么时候应该调用 startWithConfig?在获得 GCM 令牌之前还是之后?

有限的测试表明以下似乎有效:

  1. 首先获取 GGLInstance ID(即调用 getIDWithHandler:)
  2. 如果收到上述 GGLInstance ID 且没有任何错误,则请求 GCM 令牌(即调用 tokenWithAuthorizedEntity:)
  3. 这样做通常会出现以下错误,但至少在短时间内(~3-10 秒),收到令牌:

<块引用>

无法在缓存中找到令牌错误域=com.google.iid 代码=-25300(空)"

解决方案

是在applicationDidBecomeActive中调用connectWithHandler:如果我只对接收 GCM 推送消息感兴趣,并且不向上游发送?

是的,connectWithHandler 是必要的,无论如何其主要目的是与 GCM 端点建立连接.

<块引用>

如果对(1)的回答是yes,则在该方法的完成处理程序中,如果发生错误,我当时没有 GCM 令牌,应该我再次尝试获取令牌?(即调用 tokenWithAuthorizedEntity?)

所以它应该工作的方式是在请求令牌本身时检查错误,如果请求失败,则使用指数回退重试.更多信息请此处.另外,请阅读此处的说明.现在,如果您仍然想在任何时候重新调用 GGLInstanceIDTokenHandler,您还应该在获取新令牌之前实现 deleteTokenWithAuthorizedEntity.

<块引用>

什么时候应该调用 startWithConfig?在获得 GCM 令牌之前还是之后?

在您的 AppDelegate.m 中,您应该使用 startWithConfig 方法调用 GGLInstanceID 共享实例.本质上在GGLINstanceID.h 类中,它应该首先获得一个Instance ID;然后将项目授权为 Authorized Entity,然后通过 iid 服务获取一个 Registration Token.查看GGLInstanceID.h 类的详细实现此处.

希望这些回答有帮助!

编辑

能回答您的问题吗?要点是,确保目标的捆绑标识符与 info.plist 文件中的 BUNDLE_ID 相同.

希望这能解决错误,如果没有发布测试时发生的情况,我们可以从那里开始.:)

Hi I've followed the tutorial for using GCM on iOS. It has been working intermittently(which means all the certificates, permissions and stuff is okay). However of late, I have been getting the two error messages repeatedly:

GCM | GCM registration is not ready with auth credentials.

Also, reconnection to GCM fails with:

Error Domain=com.google.gcm Code=501 "(null)"

This co-relates,in part, to the device not getting a GCM registration ID. Has anyone else come across these issues more frequently of late? Or is it because I'm calling the GCM API in an incorrect sequence (especially the connectWithHandler:, startWithConfig: and tokenWithAuthorizedEntity methods)? I suspect that the reason is the latter since I do get a GCM ID after some delay.

I do not always receive a GCM ID either. When I don't receive one, I usually have to run the app once or twice more via Xcode. (Or by force-closing the app). Clearly this is not something that my users should have to do.

This is the sequence of my GCM API calls:

  1. The device gets an APNS token
  2. I then call tokenWithAuthorizedEntity: by using my APNS token
  3. ^ This usually results in one of those two errors mentioned above.
  4. Whenever I actually need a GCM token, I force a refetch of the GCM token by calling tokenWithAuthorizedEntity again.

Also, I have the connectWithHandler: call written inside my applicationDidBecomeActive: method also.

A couple of questions:

  1. Is the call to connectWithHandler: in applicationDidBecomeActive: necessary if I am only interested in receiving GCM push messages and not send them upstream?
  2. If answer to (1) is yes, in the completion handler of that method, if an error occurs, and I do not have the GCM token at that point, should I try to get a token again? (i.e. call tokenWithAuthorizedEntity?)
  3. When should the startWithConfig be called? Before getting a GCM token or after?

EDIT: Limited testing revealed that the following appears to work:

  1. Get the GGLInstance ID first (i.e. call getIDWithHandler:)
  2. If the above GGLInstance ID was received without any error, ask for a GCM token (i.e. call tokenWithAuthorizedEntity:)
  3. Doing this generally gives the following error, but at least in a short while(~3-10 seconds), the token is received:

Unable to find token in cache Error Domain=com.google.iid Code=-25300 "(null)"

解决方案

Is the call to connectWithHandler: in applicationDidBecomeActive: necessary if I am only interested in receiving GCM push messages and not send them upstream?

Yes, connectWithHandler is necessary regardless as its prime purpose is to make connection with GCM endpoint.

If answer to (1) is yes, in the completion handler of that method, if an error occurs, and I do not have the GCM token at that point, should I try to get a token again? (i.e. call tokenWithAuthorizedEntity?)

So the way it should work is you check for errors while you are requesting the token itself and retry with exponential back-off if the request fails. More info here. Also, read the note here. Now, if you still want to re-invoke the GGLInstanceIDTokenHandler at any point you should also implement deleteTokenWithAuthorizedEntity before getting a new token.

When should the startWithConfig be called? Before getting a GCM token or after?

In your AppDelegate.m you should invoke the GGLInstanceID shared instance using startWithConfig method. Essentially in GGLINstanceID.h class, it should first get an Instance ID; then authorize the project as Authorized Entity and then get a Registration Token through the iid service. See the detailed implementation for GGLInstanceID.h class here.

Hope this answers help!

EDIT

Does this answer your question? Gist of it is, make sure the Bundle Identifier for your target is the same as the BUNDLE_ID in the info.plist file.

Hopefully this resolves the error, if not post what happened when you test it and we can go from there. :)

这篇关于在 iOS 上获取 GCM 推送通知的注册令牌的正确顺序?GCM 不可靠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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