Clarifying UNNotification in time zones
I cannot have iOS 10 local notification (UNNotification) correctly in time zones.
In +10 hours timezone, I want it to fire at 17:00:00 and schedule it.
Later, when I see what notifications are scheduled, I get the next layoff date, still in the +10 hours timezone, it will say 17:00:00.
But when I change the timezone to +8 hours on the device and see what notifications are scheduled, I get the next date he was fired, and he still says 17:00:00.I would expect him to fire two hours earlier, at 15: 00:00.
How can I make it work like this? I tried to change the time zone of a calendar used to create the date components that were shipped to UNCalendarNotificationTrigger (tried things: TimeZone.init(identifier: "Somewhere")
, TimeZone.autoupdatingCurrent
and possibly others). But that just won't change, AFAIK I always get the same fire date, even if I change the time zones.
source to share
UNTimeIntervalNotificationTrigger
can solve this problem.
You can calculate the difference between a set date and today's 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
}
source to share