Search code examples
iosunusernotificationcenterunnotificationtrigger

Local Notification not working in iOS 10.3.1


I have an app which will send a local notification for specific location change when the app is in background or not running. Im using region monitoring to get location changes and create notification request if needed. My problem is the notification is not working in iOS 10 where as it is working fine in iOS 11 & 12. Below is the code to create a notification request.

func getRequest() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        if granted {
            DispatchQueue.main.async {
                self.scheduleNotification()
            }
        }
    }
}

func scheduleNotification() {
    let timeTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 15.0, repeats: false)
    let center = UNUserNotificationCenter.current()

    let content = UNMutableNotificationContent.init()
    content.title = "Notification works!"
    content.sound = UNNotificationSound.default

    let request = UNNotificationRequest.init(identifier: "LocalNotification", content: content, trigger: timeTrigger)

    center.add(request) { (error) in
        print(error?.localizedDescription ?? "No Error")
    }
}

Is there anything I miss here that must be included for iOS 10? Why is it not working in iOS 10 alone?


Solution

  • After spending some hours on stack over flow, thanks to this answer in another post. We should also mention the content.body. This should not be empty in iOS 10. For some reason the notification works without body in iOS 11 & 12.