Background work for iOS7, but not for iOS8

I have an app that uses background push, everything works fine when the app is running on an iOS 7 device, but the push is not delivered to the app if it is in the background if launched on an iOS 8 device.

I have this as registration code:

- (void) registerForRemoteNotification
{
    // Check for the presense of iOS8 notification API
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        NSLog(@"iOS8 detected");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        // iOS version < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

      

I've also tried this option:

- (void) registerForRemoteNotification
{
    // Check for the presense of iOS8 notification API
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        NSLog(@"iOS8 detected");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
    else
    {
        // iOS version < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

      

This is the content of the push payload:

aps =     {
    "content-available" = 1;
    somePayload = "The payload";
    sound = "";
};

      

This is what happens:

1) On iOS7 when app is in the foreground -> push is delivered to the app as expected
2) On iOS7 when app is in the background -> push is delivered to the app as expected
3) On iOS8 when app is in the foreground -> push is delivered to the app as expected
4) On iOS8 when app is in the background -> push is NOT delivered to the app
5) On iOS8 when app is in the foreground and running via Xcode -> push is delivered to the app as expected
6) On iOS8 when app is in the background and running via Xcode -> push is delivered to the app as expected

      

(In each case, didRegisterForRemoteNotificationsWithDeviceToken: gets invoked and didFailToRegisterForRemoteNotificationsWithError: doesn't.)

I don't know how important this is, but look at the difference between 4) and 6). I can see the difference between whether the application is executed through Xcode or not.

My question is, why is push not being delivered to the app for case 4)? It works for iOS7 and AFAICT - the code for registering remote notifications when running on iOS8 is correct (and indeed it works if the app is running through Xcode, hence it should be correct) hence why it doesn't work when the app is running independent of Xcode on iOS8?

** UPDATE. After posting a bounty for this, I went to the Apple Developers Forum. Many people seem to have similar problems. For some, Wi-Fi has to be turned on to get background taps, for me, however, it doesn't work - I have to charge my phone to get them. If it doesn't charge (even if the battery is full), they won't be delivered. Apple says what they did in regards to background pushing for iOS 8 is deliberate and not a mistake, but my conclusion is that they either screwed up or did something so illogical as to be considered wine. So I hope they understand what they did and change it in a future release.

If you want the background to go to the appropriate developer forum and read some of the relevant threads.

+3


source to share


4 answers


I figured out what it takes to get remote notifications in the background in iOS8. Apple has changed the methods required to do this. First, they are deprecated method registerForRemoteNotificationTypes

and now you should use registerForRemoteNotifications

. But that's not all that changes - you still have to specify the types of notifications allowed. Here, from the documentation ( https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/registerForRemoteNotifications ):

If you want your apps to push notifications to display alerts, play sounds, or perform other actions to the user, you must call the registerUserNotificationSettings: method to request the types of notifications you want to use. If you don't call this method, the system automatically feeds all push notifications to your app. Since the registration process takes into account the user's preferred notification settings, requesting access to the types of notifications addressed to the user also does not guarantee that they will be delivered. To find out what notification settings are available, use the currentUserNotificationSettings method.

Try calling currentUserNotificationSettings

now and you will see that no types of notifications are allowed, so it should come as no surprise that they have not been received.



Try the following:

[application registerForRemoteNotifications];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];

      

He does it!

0


source


The following works in my iOS7 and iOS8 apps:

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

      



Hope this helps!

0


source


Try this, I found I had a problem with Apples documentation, I am using Parse and it has a better section for reference

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {
    // Register for Push Notifications before iOS 8
    [Parse setApplicationId:@"asdasd" clientKey:@"asdasd"];
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                     UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}

      

0


source


I think you need to set Push Notification Access Keys.

MTA environment, Receive push notifications in iOS

Please refer to this link Notification Access Keys

-1


source







All Articles