0

I start my app and schedule my local notifications. This is a simplified version of the code I'm using:

let content = UNMutableNotificationContent()
content.body = "Wild IBEACON appeared!"
let region = CLBeaconRegion(proximityUUID: uuid, identifier: "iBeacon region")
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let request = UNNotificationRequest(identifier: "iBeacon notification", content: content, trigger: trigger)
notificationCenter.add(request)

They trigger while my app is in the background. So far, so good.

Then I restart the device. I don't force-quit the app.
And now the notifications don't trigger anymore. I need to open the app again.

Is there a way to let my schedules survive the restart?

Mr.Yeah
  • 1,054
  • 2
  • 9
  • 21
  • 1
    No way to do this – Vyacheslav Jan 17 '17 at 11:24
  • By restarting the device you are force quitting the app. As soon as the app is closed then local notifications will no longer work. You need to use Push notifications for this scenario. – dlbuckley Jan 17 '17 at 11:37
  • you've forgotten to mention _which part of your app_ sets up the notification; and I assume your app __is not__ setting the local notification in case of your app's termination – __only when__ it goes to background. it would be great if you'd extend your OP and clarify that, please. – holex Jan 17 '17 at 11:57
  • @holex I run that code via a `UISwitch`. – Mr.Yeah Jan 17 '17 at 12:19
  • @dlbuckley How can I implement location-specific notifications with push notifications? – Mr.Yeah Jan 17 '17 at 12:22
  • @Mr.Yeah if you are using location services then you can use geofencing to wake up your app when the user enters a specific geographical location. In that case when your app is woken up you could send a local notification to the user. You can find more information about region monitoring here: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html – dlbuckley Jan 17 '17 at 12:35
  • Hi, I want to get notification when app is terminated and if app is not running then again I want notification every 72 hours. How can I achieve this ? Is it possible with local notification? Also, I am getting issue with local notification - I restart the device. I don't force-quit the app. And now the notifications don't trigger anymore. I need to open the app again. – Rajput Nancy Katal Jan 19 '23 at 17:41

1 Answers1

1

The UNLocationNotificationTrigger is a new helper classes added in iOS10 to make it easier to trigger notifications based on beacon or geofence detections. According to the documentation, it is designed to be used only when the app is in use:

Apps must request access to location services and must have when-in-use permissions to use this class. To request permission to use location services, call the requestWhenInUseAuthorization() method of CLLocationManager before scheduling any location-based triggers.

https://developer.apple.com/reference/usernotifications/unlocationnotificationtrigger

Based on the above permissions, the app will only trigger when in use. The documentation does not explicitly say that it won't work in the background, so you might try requesting always location permission with the requestAlwaysAuthorization() instead of requestWhenInUseAuthorization() (be sure you put the correct key in your plist if you do this), to see if this helps.

An alternative would be to not use this helper class and instead manually start up CoreLocation and beacon monitoring, then create your own UILocalNotification manually when you get the region entry callback:

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
  if let region = region as? CLBeaconRegion {
    let notificationMessage = "Wild IBEACON appeared!"
    let notification = UILocalNotification()
    notification.alertBody = notificationMessage
    notification.alertAction = "OK"
    UIApplication.shared.presentLocalNotificationNow(notification)
  }
}

The above approach is known to work across app restarts.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • WhenInUseAuthorization is sufficient because the notification trigger does not start the app. And my code already works after an _app_ restart, but not after a _device_ restart. In that case neither my nor your code works. – Mr.Yeah Feb 08 '17 at 17:58
  • Finally, it works, but it is unstable. Sometimes it takes several minutes and some Bluetooth dis-enable cycles until it triggers. – Mr.Yeah Feb 13 '17 at 14:11