iOS 13 local notification not sending

Viewed 662

I have a reoccurring local notification set up every day at 8am. This has worked completely fine in every iOS except for when I updated to iOS 13+, it no longer sends my local notification. There has been no code changes on that part. I have seen people having issues with push notifications on iOS 13, but this is a local one so I have no idea why it suddenly stopped working on iOS 13. I have tried using xcode 10 & 11.

If anyone needs code posted I can get that, but I'm curious if anyone else has had this issue with local notifications and iOS 13.

1 Answers

I think I figured it out!

At least for me, setting the trigger with UNCalendarNotificationTrigger didn't work. However, if I used UNTimeIntervalNotificationTrigger instead, it works great!

Here's my code:

let now = Date(timeIntervalSinceNow: 0)

// If event has already occured, silently fail.
if (timelineEvent.event.time < now) {
    return
}

// Otherwise, let's calculate the time until the next event
let interval = timelineEvent.event.time.timeIntervalSince(Date(timeIntervalSinceNow: 0))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)

If you're strictly targeting iOS 13 and newer, the date comparison can be simplified slightly:

let now = Date(timeIntervalSinceNow: 0)

// If event has already occured, silently fail.
if (timelineEvent.event.time < now) {
    return
}

// Otherwise, let's calculate the time until the next event
let interval = now.distance(to: timelineEvent.event.time)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)

Strangely, Date().distance(to: Date) appears to be iOS 13+ only, however the documentation says it's available since iOS 7.0.

Related