0

I've been trying to schedule a UILocalNotification on IOS at a specific timezone irrespective of where the user is currently location. The three scenarios I need to cover are

  1. being able to show a one-off notification at the the specified time and timezone
  2. show a daily notification at the specified time and timezone
  3. show a weekly notification on the specified day of the week (i.e. Sunday, Monday, etc) at the specified time and timezone

I tried tackling the first scenario but scheduling a notification to appear two minutes later with the timezone set to Pacific/Auckland. For background I'm based in Sydney and Auckland is two hours ahead. So suppose the current date and time in Sydney is 4th February 2020 8:00PM, I would parse a string using NSDateFormatter that was "2020-04-02T22:02" and specify the timezone for the formatter as Pacific/Auckland too. That date is then passed to the UILocalNotification's fireDate property and the timeZone property is also specified. I can see that it is the correct time in UTC. However, I'm finding that the notification doesn't fire two minutes later. Am I missing something or is there a bug/limitation with regards to what can be done using the old UILocalNotification APIs? I find that if I schedule a notification that is one minute into the pass but in Auckland time that it fires immediately.

MaikuB
  • 56
  • 2
  • 1
    you have done it right, there may be something else wrong – Shahzaib Qureshi Feb 04 '20 at 09:15
  • Thanks but I realised I forgot to mention that I have set the notification's `timeZone`. Supposedly this changes the behaviour as per the docs (https://developer.apple.com/documentation/uikit/uilocalnotification/1616659-timezone?language=objc). If i leave the `timeZone` as nil then it behaves as expected though I suspect I need to specify the timezone as I need to be able to handle recurring notifications. I've been having trouble find examples around setting it to a timezone that isn't the local or default timezone meets what I need – MaikuB Feb 04 '20 at 10:04

1 Answers1

0

try this one for Schedule Local Notifications at Specific times in the day Schedule Local Notifications at Specific times in the day

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)

        let localNotification1 = UILocalNotification()
        localNotification1.alertBody = "Your message"
        localNotification1.timeZone = NSTimeZone.defaultTimeZone()
        localNotification1.fireDate = self.getEightAMDate()
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)

        let localNotification2 = UILocalNotification()
        localNotification2.alertBody = "Your message"
        localNotification2.timeZone = NSTimeZone.defaultTimeZone()
        localNotification2.fireDate = self.getSevenPMDate()
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)
        return true
    }

    func getEightAMDate() -> NSDate? {
        let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        let now: NSDate! = NSDate()

        let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
        return date10h
    }

    func getSevenPMDate() -> NSDate? {
        let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        let now: NSDate! = NSDate()

        let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
        return date19h
    }
Chetan Hedamba
  • 293
  • 2
  • 9
  • I'm looking to schedule it a specific timezone, which could be different than the default timezone like you have in your code – MaikuB Feb 04 '20 at 09:55