Shalva Avanashvili
Shalva Avanashvili

Reputation: 899

Prevent UILocalNotifications when the app is killed

I have developed IBeacon App. When the app is in background mode and I enter the beacon region the UILocalNotification is triggered. But the thing is that if I kill the app the notification still triggers. I use locationMgr.RequestAlwaysAuthorization (); to catch the location change when the app is in background. I have written background-safe task to enlarge my background time on iPhone.

I have tried to use UIApplication.SharedApplication.ApplicationState but it returns true when the app is in background only, not terminated.

I use locationMgr.RequestAlwaysAuthorization (); since DidEnterRegion is called only with location set to ALWAYS.

In a word I am searching for the solution how to monitor EnterRegion&ExitRegion only in background and active mode. NOT WHEN THE APP IS KILLED

Upvotes: 1

Views: 418

Answers (2)

jcaron
jcaron

Reputation: 17710

There are several reasons why an app would be terminated (not running):

  • because the user actively terminated it (double-tap on home button, swipe app up)

  • because the OS needed the memory, and terminated it

  • because the OS updated the app, and terminated it

  • because the device was rebooted, and the app was not launched yet

Do you want to avoid notifications in all cases, or only in the first one?

If you want to avoid notifications in all cases, you can try removing the local notification in your AppDelegate's applicationWillTerminate: method (or an observer for the UIApplicationWillTerminateNotification notification):

- (void)applicationWillTerminate:(UIApplication *)application
{
    [application cancelLocalNotification:<insert here the notification you saved>];
}

(you may alternatively use cancelAllLocalNotifications, or search for the notification using scheduledLocalNotifications).

If you only want to avoid those notifications in the first case, I'm afraid I'm not aware of any way to detect the user having actively terminated the app.

Upvotes: 2

davidgyoung
davidgyoung

Reputation: 64941

Before iOS 7.1 this was the default behavior of iOS, and everybody complained about it. Since iOS 7.1, beacon monitoring will launch apps even if they are killed.

What you need is a way to detect if an app has previously been killed when it is launched again so you can suppress notifications. Unfortunately, I am unaware of any way to do this.

Upvotes: 2

Related Questions