Have UNNotification trigger across timezones
Asked Answered
B

1

6

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.

Bigamist answered 27/4, 2017 at 7:0 Comment(7)
share some code ?Girardo
code won't help here, since UNMutableNotificationContent and UNCalendarNotificationTrigger does not accept timezone. It receives date components such as hour and minute, then fires it at local time. This is a new behavior from previous iOS versions, which had a timeZone field on UILocalNotification.Scaffold
Date components received by the trigger can contain a timezone component Calendar.current.dateComponents([.day,.month,.year,.hour,.minute,.timeZone], from: fireDate), but the timeZone seems to be ignored by UserNotifications framework.Scaffold
Honestly I gave up on this. Maybe there is no solution... it felt like a waste of time searching for it. No it won't work... I have more other work to do :-P Not saying it is impossible though. Or maybe at least this should be a feature request on Radar.Bigamist
Jonny, where you able to get a solution for this? I am having the exact same problem now.Deniable
No, I gave up... I haven't tried the answer below, so might be worth a shot. https://mcmap.net/q/1784271/-have-unnotification-trigger-across-timezonesBigamist
Start with a UTC date and add the time zone offset to the date, then convert to components. profitDetriment
A
4

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
}
Albuminate answered 11/7, 2017 at 23:35 Comment(1)
This does indeed solve the problem! The UNCalendarNotificationTrigger is sadly very buggy and this is a great way to work around it. This should be the accepted answer in my opinion.Hole

© 2022 - 2024 — McMap. All rights reserved.