推送通知委托回调未被调用
我通过调用以下代码来注册推送通知:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];
但在某些罕见的情况下,不会调用以下委托:
- (void)application:(UIApplication *)iApplication didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)iNewDeviceToken {
甚至不会调用以下方法:
- (void)application:(UIApplication *)iApplication didFailToRegisterForRemoteNotificationsWithError:(NSError *)iError {
这可能是什么原因。收到设备令牌后,我将继续我的应用程序流程。因此,在某些罕见的情况下,我的应用程序会挂起。
有什么线索吗?
I am registering for Push Notification by calling the following piece of code:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];
But in some rare cases following delegate does not get called:
- (void)application:(UIApplication *)iApplication didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)iNewDeviceToken {
Not even following method gets called:
- (void)application:(UIApplication *)iApplication didFailToRegisterForRemoteNotificationsWithError:(NSError *)iError {
What could be the reason for this. I am proceeding with my app flow once I received the device token. So, in some rare scenarios my app hungs.
Any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据文档,在设备与推送服务器建立持久连接之前,这两个回调都不会发生。因此,如果没有可用的 wifi 或数据连接,回调将不会发生 - 苹果不会将此视为错误情况。据我所知,可能导致 didFail... 回调的唯一错误是不正确的证书/应用程序权限问题(开发问题),或者用户拒绝权限(尽管我只能偶尔重现此问题)通过更改日期并关闭手机)。
According to the documentation, neither of the callbacks will happen until the device has a persistent connection with the push server. So if there is no wifi or data connection available, the callbacks won't happen - and apple doesn't regard this as an error condition. As far as I can tell, the only errors that can happen to cause the didFail... callback are an incorrect certificate/app permissions issue (a development problem), or the user declined permission (though I am only sporadically able to reproduce this by changing the date and turning the phone off).
线索就在那里:当失败时,你会得到一个 NSError 实例。因此,要找出错误的原因,请检查
NSError
并查看它告诉您什么。您可能至少应该在您的应用程序中处理这种情况,例如显示一条错误消息“无法注册应用程序,请重试...”。
The clue is right there: you get a
NSError
instance when it fails. So to figure out why it errors, inspect theNSError
and see what its telling you.You should probably at least handle that case in your app as well, such as showing an error message along the lines of "Failed to register application, please try again...".
另一件事:如果您的手机已root/越狱并用于其他网络等,您将会遇到问题。我的手机被root了,没有一个代表接到电话。我让一位同事把它放在他们的 iPad 上,效果很好,所以我知道代码没问题。
因此,我在手机上重新安装了操作系统,它开始工作了。
One more thing: If you phone is rooted/jail broken for use with another network, etc., you will have problems. My phone was rooted and none of the delegates were called. I had a coworker put it on their iPad, and it worked fine, so I know the code was OK.
So, I reinstalled the OS on my phone, and it started working.
您必须在配置文件中启用推送通知,并使用应用程序 ID 在开发人员门户中进行设置。那么它应该可以工作。
You have to enable the Push Notifications in the provisioning profile, set it up in the Developer Portal with the App Id. Then it should work.