0
votes

I am using iOS 8 and I am scheduling a UILocalNotification as work-around to the fact that I cannot execute background code unless for a finite amount of time or that the app belongs to certain categories (E.g. communicates with external BLE accessories, etc.. but even so it would be limited to that code):

Background execution documentation here):

If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time.

What I want to achieve is to check if there are some conditions that are met at the time in which the UILocalNotification is fired (using local notification seems the only way to get the app to execute something whilst in background..). Unless there is another way..

Unfortunately I seem to be able to intercept the call and execute code only when the app enters the foreground (users tap the notification badge and the ** didReceiveLocalNotification** gets called.

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif

This corresponds to the behaviour described by Apple here.

The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.

Any idea on how I can intercept the UILocalNotification whilst the app is in background and execute some code without having the user to tap on the notification?

1

1 Answers

1
votes

You cannot use the firing of a local notification in itself to launch your app in the background. As you have noted, your app only executes if the user taps the notification.

You can use a remote notification with a content-available key to launch your application in the background.