Search code examples
iosbackgroundcore-locationibeacon

Disabling UILocalNotifications after closing app


I am using beacons in my app (app set to background) and I am setting series of UILocalNotifications on beacon:DidExitRegion: (kind of alarm when beacon is lost).

Edit: When app is in the background, user exits region, then the notifications are set properly. When user wants to close the app while it's still being in background with the notifications set, I want to disable them all. /edit

The problem is, that I want to disable all those notifications in AppWillTerminate. According to documetation when app is in suspended state (waiting in background for getting signal from the beacon - CoreLocation), the appWillTerminate is not called. Users will often close the app while it is in background and alarms-notifications will not be removed.

What is the possible way to get rid of those notifications?

I've got 1 suggestion: perform "artificial" finite-length tasks in background (like here) and actually run the app after receiving beacon:didExitRegion: -> it doesn't get suspended, appWillTerminate will be called. It will, however work only for max 10 minutes and I don't think it's elegant thing to do.

Maybe other ways to do it? Thanks.


Solution

  • You can't perform any action when the user swipes up your app in the task switcher. There are no callbacks, appWillTerminate or otherwise. This is by design.

    You might be able to rework your delayed notifications so they are sent immediately by your app after a timed delay under programmatic control. That way, they are only sent if the app is still running when the timer goes off. The trick is that your app may be suspended for other reasons (user locks the phone, switches to another app) so you probably need a background task to do this. The limit in iOS 7+, by the way, is now 3 minutes not 10 minutes, so that is the longest you could delay notifications reliably.