Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have UNNotification trigger across timezones

I can't have a iOS 10 local notification (UNNotification) fire properly across time zones.

In time zone +10 hours, I want it to fire at 17:00:00, and schedule it.

Later when I look at what notifications are scheduled, I get the next firing date of it, still in time zone +10 hours, it will say 17:00:00.

But when I change time zone to +8 hours on the device, and look at what notifications are scheduled, I get the next firing date of it, and it still says 17:00:00. I would have expected it to fire two hours earlier, at 15:00:00.

How can I make it work like this? I have tried changing the timezone of the calendar used for creating the date components fed to the UNCalendarNotificationTrigger (tried things are: TimeZone.init(identifier: "Somewhere"), TimeZone.autoupdatingCurrent, possibly others). But it just won't change, AFAIK I always get the exact same fire date even if I change time zones.

like image 254
Jonny Avatar asked Oct 30 '22 08:10

Jonny


1 Answers

UNTimeIntervalNotificationTrigger can solve this problem.

You could calculate the difference between the date you set and current date, then schedule a time interval trigger.

func timeIntervalTrigger(from date: Date, repeats: Bool) -> UNTimeIntervalNotificationTrigger {
    let timeInterval = date.timeIntervalSinceNow
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: repeats)
    return trigger
}
like image 151
Justin Jia Avatar answered Nov 09 '22 08:11

Justin Jia