iPhone registerForRemoteNotificationTypes不会产生错误,但不会触发授予设备令牌的委托

我正在开发一个需要推送通知的iPhone应用程序。 我遵循了创buildauthentication和修改应用程序ID的说明。 我不完全肯定我做了这个正确的,但我确实按照方向。 任何想法如何我可以检查,看看这是否可以吗?

当我在模拟器中运行时,我收到一条错误消息,说模拟器不支持推送通知。 这是有点期待。

顺便说一句:我在这里似乎有这个问题几次。 它似乎总是与一个监狱破碎的电话。 我的手机没有被监禁。

但是当我在iPhone上debugging时,didRegisterForRemoteNotificationsWithDeviceToken方法永远不会被触发。 我真的很感激一些帮助。 我的代码如下。

-(void)applicationDidFinishLaunching:(UIApplication *)application { rootController.delegate = self; [window addSubview:rootController.view]; [window makeKeyAndVisible]; [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *str = [NSString stringWithFormat:@"%@",deviceToken]; NSLog(str); } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@", err]; NSLog(str); } 

通常如果didRegisterForRemoteNotificationsWithDeviceToken永远不会被调用,那是因为您正在使用不是APS-entitledconfiguration文件。 如果您的应用程序是生产应用程序,则必须使用分发configuration文件进行签名才能推送。 如果是开发应用程序,则必须使用开发configuration文件进行签名。

编辑:值得注意的是, provisioning profile必须使用明确的包ID。 发送到任何使用通配符configuration文件签名的内容将不会被发送。

我遇到过同样的问题。

对于我来说,应用程序的通知设置已closures。

如果在iOS设置应用程序通知被禁用,该方法将不会被调用。

一旦我打开通知types,它开始工作。

Settings App->Notification Settings->[My App]

查看TN2265 ,它提示debugging推送通知问题。 我今天遇到了这个问题,事实certificate,在iPod Touch上closures了无线networking。 既然你说过你是从一个可能不是iPhone的iPhone开始工作的,但是值得看一下这个技术笔记是否有其他的可能性。

检查你的防火墙设置。

从斯科特K的答案 :

如果您的iOS设备能够使用蜂窝数据networking,请检查它是否具有有效的蜂窝数据规划。 例如,在“设置”中closuresWi-Fi,然后查看是否仍然可以使用Safari浏览网页。 另一方面,如果推送服务正在使用Wi-Fi,则设备或计算机与Internet之间的任何防火墙都必须允许来往于端口5223的TCP通信。

根据Apple的文档“推送通知疑难解答”“如果既没有委托callback应用程序:didRegisterForRemoteNotificationsWithDeviceToken:也没有应用程序:didFailToRegisterForRemoteNotificationsWithError:被调用,这意味着此连接尚未build立。”…“系统可能没有Internet连接。 .aiplane模式“。

我也看到了这种行为,但不是因为缺乏互联网连接,而是因为,正如IrFan指出的那样,通知已被禁用,只要启用通知,该方法就会触发。

解决scheme1)不要依赖这些委托方法中的任何一个被调用。 2)如果您收到代表回电,请按照其执行。 3)缺乏networking连接,推送通知被禁用的应用程序,APS未启用,或? 会导致代表不被调用。

如果您使用iOS8 + SDK(例如xcode 6+)进行编译,那么不推荐使用的iOS7 API方法将被忽略 ,您将不会看到任何错误。

如果你想支持iOS7和8,你需要这样的东西:

 // Have to explicitly use the iOS8 api as the deprecated API is silently ignored on iOS8, surprisingly. if ([UIDevice currentDevice].systemVersion.intValue >= 8) { // iOS8+ API. UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { // iOS7. [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; } 

如果您的configuration文件中缺lessaps-environment问题,则至less可以通过didFailToRegisterForRemoteNotificationsWithError获取有用的错误callback