ios iPhone registerForRemoteNotificationTypes 不会生成错误,但不会触发提供设备令牌的委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3224605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:24:58  来源:igfitidea点击:

iPhone registerForRemoteNotificationTypes does not generate an error but does not fire delegate that gives device token

iosiphoneapple-push-notificationsappdelegatedevicetoken

提问by Rick Leinecker

I am developing an iPhone app that needs push notification. I followed the instructions for creating the certifications and modifying the app ID. I am not totally sure I did this correctly, but I did follow the directions. Any idea how I can check to see if this is OK?

我正在开发需要推送通知的 iPhone 应用程序。我按照创建认证和修改应用程序 ID 的说明进行操作。我不完全确定我这样做是否正确,但我确实按照说明进行了操作。知道我如何检查这是否正常吗?

When I ran in the emulator I did get an error message saying that the emulator did not support push notifications. This was somewhat expected.

当我在模拟器中运行时,我确实收到一条错误消息,指出模拟器不支持推送通知。这有点在意料之中。

BTW: I have seem this question out there a few times. It always seems to be with a jail-broken phone. My phone is NOT jail-broken.

BTW:这个问题我已经问过好几次了。它似乎总是与越狱的手机有关。我的手机没有越狱。

But when I debug on the iPhone the didRegisterForRemoteNotificationsWithDeviceToken method is never fired off. I will really appreciate some help. My code follows.

但是当我在 iPhone 上调试时,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);    
}

回答by Patrick Goley

Usually if didRegisterForRemoteNotificationsWithDeviceTokenis never called, it is because you are using a provisioning profile that is not APS-entitled. If you app is a production app, it must be signed with a distribution profile for pushes to work. If it is a development app, you must sign it with a development profile.

通常 ifdidRegisterForRemoteNotificationsWithDeviceToken从未被调用,这是因为您使用的配置文件不是APS-entitled. 如果您的应用程序是生产应用程序,则必须使用分发配置文件对其进行签名才能推送工作。如果是开发应用程序,则必须使用开发配置文件对其进行签名。

EDIT:It is also worthy to note that the provisioning profilemust use an explicit bundle id. Pushes sent to anything signed with a wildcard profile will not be delivered.

编辑:还值得注意的是,provisioning profile必须使用显式包 ID。发送到使用通配符配置文件签名的任何内容的推送将不会被传递。

回答by Eric

I had the same issue.

我遇到过同样的问题。

For me, it was the notification setting for the App had everything turned off.

对我来说,这是应用程序的通知设置关闭了所有内容。

If in the iOS settings app notifications are disabled, that method will not get called.

如果在 iOS 设置中禁用了应用通知,则不会调用该方法。

Once I turned on a notification type it started to work.

一旦我打开通知类型,它就开始工作。

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

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

回答by Scott K.

Check out TN2265, which has hints for debugging push notification problems. I had exactly this problem today and it turned out that WiFi was turned off on iPod Touch. Since you said you were working from an iPhone. that's probably not it, but it's worth checking out the tech note for additional possibilities.

查看TN2265,它有调试推送通知问题的提示。我今天遇到了这个问题,结果是 iPod Touch 上的 WiFi 被关闭了。既然你说你是用 iPhone 工作的。可能不是这样,但值得查看技术说明以了解其他可能性。

回答by Tony Ashworth

Check your firewall settings.

检查您的防火墙设置。

From Scott K's Answer:

来自Scott K 的回答

If your iOS device is capable of using the cellular data network, check that it has an active cellular data plan. Turn off Wi-Fi in Settings and see if you can still browse the web with Safari, for example. On the other hand, if the push service is using Wi-Fi, any firewalls between your device or computer and the Internet must allow TCP traffic to and from port 5223.

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

回答by user3335999

According to Apple's documentation "Troubleshooting Push Notifications" "If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called, that means that this connection is not yet been establihed."..."The system may not have Internet connectivity..aiplane mode."

根据 Apple 的文档“推送通知疑难解答”,“如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 均未调用,则表示此连接尚未建立。”...“系统可能没有 Internet 连接。 .aiplane 模式。”

I'm seeing this behavior as well but NOT because of lack of internet connectivity but because, as IrFan pointed out, the notifications were disabled for the app, as soon as you enable the notifications, the method fires.

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

Solution 1) Do NOT rely on either of these delegate methods ever being called. 2) If, and when, you receive a delegate callback, act on it. 3) Lack of network connectivity, push notifications being disabled for the app, APS not enabled, or ? will cause the delegates to NOT get called.

解决方案 1) 不要依赖于被调用的这些委托方法中的任何一个。2) 如果以及何时收到委托回调,请对其采取行动。3) 缺乏网络连接、应用程序的推送通知被禁用、APS 未启用,或者?将导致代表不会被调用。

回答by Chris

If you're compiling using the iOS8+ SDK (eg xcode 6+), then the deprecated iOS7 API methods are silently ignoredand you'll get no error.

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

You need something like this if you want to support iOS7 and 8:

如果你想支持 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];
}

If you are having issues with aps-environmentmissing from your profile, you'll at least get a helpful error callback with didFailToRegisterForRemoteNotificationsWithError

如果您有问题,aps-environment从您的个人资料丢失,你至少得到一个有用的错误回调didFailToRegisterForRemoteNotificationsWithError

回答by Shankey

I faced the same issue in iOS 11.3 device where none of the delegate methods were getting called. So, according to Apple documentation :-

我在 iOS 11.3 设备中遇到了同样的问题,其中没有调用任何委托方法。因此,根据 Apple 文档:-

https://developer.apple.com/library/content/technotes/tn2265/_index.html

https://developer.apple.com/library/content/technotes/tn2265/_index.html

When the first push-capable app is installed, iOS or OS X attempts to establish a persistent network connection to the push service that will be shared by all push-capable apps on the system. If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called, that means that this connection has not yet been established.

安装第一个支持推送的应用程序后,iOS 或 OS X 会尝试与推送服务建立持久的网络连接,该连接将由系统上的所有支持推送的应用程序共享。如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 均未调用,则表示此连接尚未建立。

Further on,

进一步,

This is not necessarily an error condition. The system may not have Internet connectivity at all because it is out of range of any cell towers or Wi-Fi access points, or it may be in airplane mode. Instead of treating this as an error, your app should continue normally, disabling only that functionality that relies on push notifications.

这不一定是错误条件。该系统可能根本没有互联网连接,因为它超出了任何蜂窝塔或 Wi-Fi 接入点的范围,或者它可能处于飞行模式。您的应用程序不应将其视为错误,而是应继续正常运行,仅禁用依赖推送通知的功能。

I tried connecting to other networkand again start performing register notification operation and Voila!, it worked like a charm. So, I guess there was a problem in registering for notification which was calling neither of the delegate methods.

我尝试连接到其他网络并再次开始执行注册通知操作,瞧!,它就像一个魅力。所以,我猜在注册通知时存在问题,它没有调用任何委托方法。