Search code examples
iospush-notificationnotificationsuilocalnotificationnsnotificationcenter

Schedule Local Notification in iOS / irregular time interval


I am scheduling a local notification. My problem is that Recurring notifications can only be scheduled on an interval of every hour, every day, every week, every month, or every year. You do not have an option of “every 2 hours”.

Now, I want to schedule it for every 2 or 3 hours of time interval. Also as there is a limitation of iOS to schedule only 64 notification at a time.

Please note that I can not use push notification.

How can I achieve this ?


Solution

  • 2 Hours = 60 secs * 60 min *2 = 7200 secs. Will this work? if you use 7200 in TimeInterval in following code which is for five second (from Apple website)

     let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
    content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
    content.sound = UNNotificationSound.default() // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request)
    

    EDIT: Please see similar code for using UILocalNotification

    UILocalNotification* localNotification = [[UILocalNotificationalloc] init]; 
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7200];
    localNotification.alertBody = @"Your alert message";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];