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.

Jonny
  • 15,955
  • 18
  • 111
  • 232
  • 1
    share some code ? – KKRocks Apr 27 '17 at 07:32
  • 1
    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`. – Kof May 29 '17 at 18:02
  • 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. – Kof May 29 '17 at 18:56
  • 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. – Jonny May 30 '17 at 13:48
  • Jonny, where you able to get a solution for this? I am having the exact same problem now. – Danny Bravo Aug 05 '17 at 14:55
  • No, I gave up... I haven't tried the answer below, so might be worth a shot. https://stackoverflow.com/a/45046066/129202 – Jonny Aug 07 '17 at 01:56
  • Start with a UTC date and add the time zone offset to the date, then convert to components. profit – SmileBot Aug 20 '21 at 16:09

1 Answers1

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
}
Justin Jia
  • 91
  • 7
  • 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. – Omid Ariyan Jul 12 '18 at 10:59