iPhone registerForRemoteNotificationTypes does not generate an error but does not fire delegate that gives device token
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]
开发者_StackOverflow中文版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);
}
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.
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]
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.
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.
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.
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
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.
精彩评论