2

Been stuck on this forever, but could not get the following methods to be called. I am able to get the phone to ask for permission but after that it gets stuck.

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

I have tried quite a few things including:

1) everything offered in the following stackoverflow post: why didRegisterForRemoteNotificationsWithDeviceToken is not called

2) Looking at this technical note from Apple: https://developer.apple.com/library/ios/technotes/tn2265/_index.html

3) This entire tutorial: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 (so hopefully my certificates are all good)

4) (And yes I have internet connection)

Does anyone have any possible solutions? I have been rattling my brains out for the last 2-3 days and have already had to factory reset my phone twice, as well as change the date on my phone N^e number of times in order to get the notification popup to appear to test over and over again.

Would love any help! Thanks!

Here is what I am using to call... (tried a few other versions):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //other stuff not related

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    #ifdef __IPHONE_8_0
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    #endif
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}
Community
  • 1
  • 1
John
  • 1,677
  • 4
  • 15
  • 26

1 Answers1

0

iOS 8 introduced a change in the flow of registration that requires explicitly asking to register remote notifications after getting permission from the user.

You are calling this code?

[[UIApplication sharedApplication] registerUserNotificationSettings: settings];

And implementing this in your Application Delegate?

- (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

That said, the old APIs are now deprecated but are still required if you plan to support iOS 7. To support both you can check to see if UIApplication responds to the new selector:

if ([[UIApplication sharedApplication] respondsToSelector:@SEL(registerUserNotificationSettings:)]) {
    // iOS 8
    [[UIApplication sharedApplication] registerUserNotificationSettings: settings];

    // [[UIApplication sharedApplication] registerForRemoteNotifications] 
    // will be called in your UIApplicationDelegate callback
} else {
    // iOS 7
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
  • In the iOS 8 portion of your if block, you need to remove the call to `[application registerForRemoteNotifications]`. Implement the `didRegisterUserNotificationSettings:` callback in your application delegate and make the `registerForRemoteNotifications` call in that method. The `registerForRemoteNotifications` call won't work until after the request for permission is complete. – James Blair Apr 01 '15 at 13:50
  • I've done that before like the following: - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { NSLog(@"IOS8 VERSION"); [application registerForRemoteNotifications]; } – John Apr 01 '15 at 19:00
  • It basically crashes my application though :( – John Apr 01 '15 at 19:00