didRegisterForRemoteNotificationsWithDeviceToken不在ios8中调用,但didRegister …设置是

我跟着这个线程 ,但方法didRegisterForRemoteNotificationsWithDeviceToken仍然没有被调用:

该文件说:

在调用UIApplication对象的registerForRemoteNotifications方法后,应用程序在设备注册成功完成时调用此方法

didRegisterUser似乎很好,但did register notif

这是我在AppDelegate中的代码(应用程序版本是8.1):

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //register notif UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; return YES; } - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //register to receive notifications [application registerForRemoteNotifications]; NSLog(@"didRegisterUser"); } -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"error here : %@", error);//not called } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { /* // Store the deviceToken in the current installation and save it to Parse. PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; currentInstallation.channels = @[ @"global" ]; [currentInstallation saveInBackground]; */ NSLog(@"did register notif");//not called } 

我也有背景模式 – > info.plist中的远程通知。

你的代码似乎是正确的。 作为一个小改进,你可以像下面这样编写你的didRegisterUserNotificationSettings方法:

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { if (notificationSettings.types != UIUserNotificationTypeNone) { NSLog(@"didRegisterUser"); [application registerForRemoteNotifications]; } } 

可能有configuration问题导致APN注册失败。

  1. 确保您的供应configuration文件包含aps环境条目

  2. 确保您在configuration文件中设置了唯一的应用程序标识符(没有任何“*”的string)。 你也应该在Info.plist中使用这个确切的标识符作为“Bundle identifier”

  3. 也许您在初始安装后拒绝了推送function – 在这种情况下,您将再也看不到应用内推送警报,并且必须再次启用设置应用中的推送。

  4. 尝试另一个设备。

经过漫长的挖掘,我发现在2016年7月19日,由于苹果公司的一些错误或更新,即使所有条件如互联网连接,设备和使用的方法都是完美的,didRegisterForRemoteNotificationsWithDeviceToken方法也不会被调用。

请参阅此链接进行确认https://forums.developer.apple.com/thread/52224

要validation,请看看你的其他应用程序。 我浪费了几个小时,但希望它能帮助别人。 谢谢。

2016年7月19日: –

根据苹果开发人员的forms ,有一个关于Sandbox APNS的问题…所以可能有问题从苹果方面,这就是为什么代表application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError:不会被调用..

检查目前的状态APNS沙箱这个链接 …现在根据状态APNS沙箱工作正常,它的正常…所以可能是从苹果方面有一些其他的错误

所以,只要你的方法是完美的,证书是有效的,就不用担心。 这只是苹果方面的一个问题..只要问题解决了,你的方法就可以很好地工作(如果你的方面没有问题的话)。

请注意,生产工作正常..所以问题只关于沙盒APNS。

我遇到了这个问题,最后在Apple Developer网站上find了这个提示并解决了这个问题:

注册,调度和处理用户通知

iOS注意:“注册远程通知:

iOS注意:如果蜂窝或Wi-Fi连接不可用,则应用程序:didRegisterForRemoteNotificationsWithDeviceToken:方法和应用程序:didFailToRegisterForRemoteNotificationsWithError:方法都不会被调用。 对于Wi-Fi连接,当设备无法通过端口5223与APN连接时,有时会发生这种情况。如果发生这种情况,用户可以移动到另一个不阻止该端口的Wi-Finetworking,或者在iPhone或iPad上等待直到蜂窝数据服务变得可用。 在任何一种情况下,设备都应该能够build立连接,然后调用其中一个委托方法。

我的iPhone只与Wifi连接,重启iPhone并重新连接到WiFi AP解决了这个问题。

在我的情况下,我相信在iOS 9.3之后。 如果我使用Localytics,则不会调用didRegisterForRemoteNotificationsWithDeviceToken。 我必须从我的代码中删除(ApplicationDidFinishLaunchingWithOption)[Localytics autoIntegrate:@“xxxxx”launchOptions:launchOptions];

请添加下面的代码,它为我工作,

  #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //register to receive notifications [application registerForRemoteNotifications]; } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler { //handle the actions if ([identifier isEqualToString:@"declineAction"]){ } else if ([identifier isEqualToString:@"answerAction"]){ } } #endif 

您将在下面的方法中获得设备令牌,

  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

详情请参阅详细答案

希望这是对某个人的帮助。

XCode 8.3.1起

在尝试完所有上述选项之后,如果委托方法仍未被调用,请确保在项目中,

 under 'Capabilities', (next to General), - Push Notifications option is turned ON` - and under Background Modes, Remote Notifications is turned ON