为什么不调用 didRegisterForRemoteNotificationsWithDeviceToken [英] why didRegisterForRemoteNotificationsWithDeviceToken is not called

查看:205
本文介绍了为什么不调用 didRegisterForRemoteNotificationsWithDeviceToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,我想在其中实现苹果推送通知服务.我正在按照 this 中给出的分步说明进行操作教程.

I am making an application in which I want to implement apple push notification service. I am following the step-by-step instructions given in this tutorial.

但是,这些方法仍然没有被调用.我不知道是什么导致了问题.有人可以帮我吗?

But still, the methods are not called. I don't know what is causing the problem. Can anyone help me?

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
        //NSString * token = [[NSString alloc] initWithData:deviceTokenencoding:NSUTF8StringEncoding];
        NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
        NSLog(@"Device Token:%@",str);

        //NSLog(@"Device token is called");
        //const void *devTokenBytes = [deviceToken bytes];
        //NSLog(@"Device Token");
    }

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
        NSString *str = [NSString stringWithFormat: @"Error: %@", err];
        NSLog(@"Error:%@",str);    
    }

推荐答案

我遇到了同样的问题:调用 registerForRemoteNotificationTypes: 既没有调用 application:didRegisterForRemoteNotificationsWithDeviceToken: 也没有调用 应用程序:didFailToRegisterForRemoteNotificationsWithError:

I had the same issue: calling registerForRemoteNotificationTypes: invoked neither application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError:

我最终在 Apple 的技术说明 TN2265 的帮助下解决了这个问题.

I eventually resolved this issue with the help of Apple's technical note TN2265.

这就是我所做的:

首先,我仔细检查了我确实注册正确用于推送通知,包括验证我的aps-environment"配置文件;密钥和 .app 文件本身的协同设计.我已经正确设置了所有内容.

First off, I double-checked that I am indeed registering correctly for Push Notifications, including verifying my provisioning profile for "aps-environment" key and the codesigning of the .app file itself. I had it all set up correctly.

然后我不得不在控制台中调试推送通知状态消息(您需要在您的设备上安装 PersistentConnectionLogging.mobileconfig 配置文件并重新启动它.参见 TN2265 在观察推送状态消息"下).我注意到 apns 进程启动了一个计时器并计算了一个最小触发日期,这让我怀疑此时通常呈现的 Push-Notification 注册确认消息被 APNS 抑制了,如 TN2265 所示:

I then had to debug Push Notification status messages in the console (you need to install PersistentConnectionLogging.mobileconfig provisioning profile on your device and reboot it. See TN2265 under "Observing Push Status Messages"). I noticed that apns process starts a timer and calculates a minimum fire date, which made me suspect that the Push-Notification registration confirmation message, which is normally presented at this point, is supressed by APNS, as indicated in TN2265:

在 iOS 上重置推送通知权限警报

启用推送的应用首次注册推送通知时,iOS 会询问用户是否希望接收该应用的通知.用户对此警报做出响应后,它不会再次显示,除非设备已恢复或应用已卸载至少一天.

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

如果您想模拟应用的首次运行,您可以将应用卸载一天.您可以通过将系统时钟提前一天或更长时间,完全关闭设备,然后重新打开设备来实现后者,而无需实际等待一天.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by setting the system clock forward a day or more, turning the device off completely, then turning the device back on.

因此,我从设备上删除了该应用程序,然后在设置"中手动更改了 iPhone 的日期,重新启动了设备,然后重新安装了该应用程序.

So, I removed the app from the device, then manually changed the iPhone's date in Settings, rebooted the device, and re-installed the app.

下次我的代码调用 registerForRemoteNotificationTypes 时,它按预期收到了回调.

The next time my code called registerForRemoteNotificationTypes, it received callbacks as expected.

这为我解决了问题.希望有帮助.

This resolved the issue for me. Hope it helps.

这篇关于为什么不调用 didRegisterForRemoteNotificationsWithDeviceToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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