1

I have written the following code in Xcode 6:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}

The I set break point in the following two methods:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //Registration successful
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    //Registration failed
}

But these two methods are never triggered when I tested on a device running iOS 8. I tried with iOS 7 as well, still no success. So I thought it might be a problem with my provisioning profile. So, to confirm, I opened Developer account, in App IDs I confirmed that APNS is enabled, revoked the existing certificates, created a new development certificate for both Push Notification and Development. Deleted the old certificates from the key chain. Cleared all provisioning profiles from Xcode, added the newly created certificates into keychain, created a development profile using the newly created certificate, added it to Xcode and ran my application on both devices again using the newly created profile. Still no result. Then I opened the project in Xcode 5, changed the didFinishLaunching method as shown:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];

    return YES;
}

And ran the app on both devices using the same profile that I just created. It ran all fine and the break point in - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken got hit. The Remote notification registration was successful both on iOS 7 and iOS 8 using just the UIRemoteNotificationType when I compiled using Xcode 5!!!

So I archived and submitted the application to iTune connect's test flight. The first time I got this mail from iTunes Connect:

Missing Push Notification Entitlement - Your app appears to include API used to register with the Apple Push Notification service, but the app signature's entitlements do not include the "aps-environment" entitlement. If your app uses the Apple Push Notification service, make sure your App ID is enabled for Push Notification in the Provisioning Portal, and resubmit after signing your app with a Distribution provisioning profile that includes the "aps-environment" entitlement. See "Provisioning and Development" in the Local and Push Notification Programming Guide for more information. If your app does not use the Apple Push Notification service, no action is required. You may remove the API from future submissions to stop this warning. If you use a third-party framework, you may need to contact the developer for information on removing the API.

So I recreated the production APNS certificate, Production Certificate, added them to keychain after deleting the old ones, created a distribution profile for App Store using the newly created production certificate, added it to Xcode, archived and submitted to test flight again. This time the mail didn't come, but both the Remote Notification Delegate methods didn't trigger either(I had placed an AlertView to know when either of the methods is triggered).

Can anyone tell me the cause of this strange behavior? What am I doing wrong here? And how can I rectify it?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

1 Answers1

3

Ok. I found the solution myself finally. I had to add the following line in case of IOS 8:

[[UIApplication sharedApplication] registerForRemoteNotifications];

So the code for didFinishLaunchingWithOptions looks like below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}

Strange that I didn't change anything in the else condition, but now it works on both IOS 7 and IOS 8. Hope it helps someone. I had spent over two days on it and I hate it when I find a solution myself right after posting a question.

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • I think its a good idea to add that line to the didRegisterUserNotificationSettings callback: http://stackoverflow.com/questions/24216632/remote-notification-ios-8 – Johnny Z Aug 25 '15 at 11:11