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

发布于 2024-09-08 16:36:51 字数 1112 浏览 1 评论 0原文

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

当我在模拟器中运行时,我确实收到一条错误消息,指出模拟器不支持推送通知。这在某种程度上是预料之中的。

顺便说一句:我已经多次看到这个问题了。似乎总是用越狱的手机。我的手机没有越狱。

但是当我在 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);    
}

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?

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.

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

-(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);    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别忘他 2024-09-15 16:36:52

根据Apple的文档“推送通知故障排除”,“如果既没有调用委托回调应用程序:didRegisterForRemoteNotificationsWithDeviceToken:也没有调用应用程序:didFailToRegisterForRemoteNotificationsWithError:,则意味着此连接尚未建立。”...“系统可能没有互联网连接。 .飞机模式。”

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

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

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."

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.

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.

↘人皮目录ツ 2024-09-15 16:36:52

如果您使用 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];
}

如果你的配置文件中缺少 aps-environment ,你至少会得到一个有用的错误回调 didFailToRegisterForRemoteNotificationsWithError

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

You need something like this if you want to support iOS7 and 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-environment missing from your profile, you'll at least get a helpful error callback with didFailToRegisterForRemoteNotificationsWithError

对你而言 2024-09-15 16:36:52

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

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

安装第一个具有推送功能的应用程序时,iOS 或 OS X 会尝试建立与推送服务的持久网络连接,该连接将由系统上所有具有推送功能的应用程序共享。如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 都没有被调用,则意味着该连接尚未建立。

进一步来说,

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

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

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

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.

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.

I tried connecting to other network and 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.

往昔成烟 2024-09-15 16:36:51

通常,如果从未调用 didRegisterForRemoteNotificationsWithDeviceToken,这是因为您使用的配置文件未经过 APS 授权。如果您的应用程序是生产应用程序,则必须使用分发配置文件对其进行签名才能使推送正常工作。如果它是开发应用程序,您必须使用开发配置文件对其进行签名。

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

Usually if didRegisterForRemoteNotificationsWithDeviceToken is 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.

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

秋心╮凉 2024-09-15 16:36:51

我有同样的问题。

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

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

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

设置应用->通知设置->[我的应用]

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.

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

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

撩发小公举 2024-09-15 16:36:51

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

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.

傲鸠 2024-09-15 16:36:51

检查您的防火墙设置。

来自 Scott K 的回答

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

Check your firewall settings.

From Scott K's Answer:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文