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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With