iPhone registerForRemoteNotificationTypes 不会生成错误,但不会触发提供设备令牌的委托
我正在开发一个需要推送通知的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据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.
如果您使用 iOS8+ SDK(例如 xcode 6+)进行编译,则已弃用的 iOS7 API 方法将被静默忽略,并且您不会收到任何错误。
如果你想支持 iOS7 和 8,你需要这样的东西:
如果你的配置文件中缺少
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:
If you are having issues with
aps-environment
missing from your profile, you'll at least get a helpful error callback withdidFailToRegisterForRemoteNotificationsWithError
我在 iOS 11.3 设备中遇到了同样的问题,没有调用任何委托方法。因此,根据 Apple 文档:-
https://developer.apple。 com/library/content/technotes/tn2265/_index.html
进一步来说,
我尝试连接到其他网络并再次开始执行注册通知操作,瞧!,它就像一个魅力。所以,我猜想注册通知时存在问题,它没有调用任何委托方法。
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
Further on,
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.
通常,如果从未调用
didRegisterForRemoteNotificationsWithDeviceToken
,这是因为您使用的配置文件未经过APS 授权
。如果您的应用程序是生产应用程序,则必须使用分发配置文件对其进行签名才能使推送正常工作。如果它是开发应用程序,您必须使用开发配置文件对其进行签名。编辑:还值得注意的是,
配置文件
必须使用显式捆绑包 ID。发送到使用通配符配置文件签名的任何内容的推送将不会被传递。Usually if
didRegisterForRemoteNotificationsWithDeviceToken
is never called, it is because you are using a provisioning profile that is notAPS-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.我有同样的问题。
对我来说,这是应用程序的通知设置将所有内容都关闭了。
如果在 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]
查看 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.
检查您的防火墙设置。
来自 Scott K 的回答:
Check your firewall settings.
From Scott K's Answer: