Search code examples
iosswiftswift3uilocalnotification

Trying to fire UILocalNotification not working


I'm using the code below to create a notification, but somehow it's not firing. I really just want it to trigger as I open one certain view controller, but nothing is firing so far. What is the problem with my code?

func notify() {
    let notification = UILocalNotification()
    notification.fireDate = Date()
    notification.repeatInterval = .minute
    notification.alertBody = "The alert body"
    notification.alertAction = "enter text here"
    UIApplication.shared.scheduleLocalNotification(notification)
}

I also have this line in the app delegate:

application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))

Solution

  • One reason I believe this would fail is because notification.fireDate = Date() will set the fireDate to the current time, which will be in the past by the time the (separate) process managing UserNotifications is alerted that you want to fire a notification. Consider moving to UNNotifications