Lizzan
Lizzan

Reputation: 1062

iOS: How to expire a local notification with a region trigger?

I have a local geofence notification, set up with the following code:

func startMonitoring(annotation:MKAnnotation) {

    let region = CLCircularRegion(center: annotation.coordinate, radius: 10.0, identifier: "randomID")
    region.notifyOnExit = false

    let notification = UILocalNotification()
    notification.region = region
    notification.alertBody = "You got it!"
    notification.category = self.notificationCategory
    notification.soundName = "my_sound.wav"
    notification.userInfo = ["ID": "randomID"]

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

This works great, but the user has to get to the area within a certain time limit for the action of the notification to be available. How can I make the notification NOT fire if the time limit has passed?

I currently only have "WhenInUse" authorization for the user's location and would prefer to keep it that way.

Upvotes: 3

Views: 1325

Answers (2)

eagleonhill
eagleonhill

Reputation: 121

You can setup background fetch with setminimumbackgroundfetchinterval , and cancel the scheduled notification if it's going to expire.

It's not 100% reliable because you cannot control exact time, but it's better than nothing.

Upvotes: 1

chedabob
chedabob

Reputation: 5881

You'd be better off monitoring the CLCircularRegion manually using CLLocationManager, and when your app is notified that the user has entered the region, check whether it's in the time limit and post your UILocalNotification manually.

See the section Using Regions to Monitor Boundary Crossings here: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html

Upvotes: 1

Related Questions