How to Schedule Local Notification for the Following Scenario

How can I schedule local notification for the following scenario?

You can simply schedule all (or many) notifications at one time. You don't need to wait for the user to View your app to schedule the next notification.

From the docs on UILocalNotification,

An application can have only a limited number of scheduled
notifications; the system keeps the soonest-firing 64 notifications
(with automatically rescheduled notifications counting as a single
notification) and discards the rest

So, if you have 3 notifications per day, you can pre-schedule 3 weeks of notifications at once. I suppose you would still have a problem if the user doesn't open your app for a month, but do you need to worry about that scenario?

Anyway, I just wanted to make sure it's clear that you don't need to schedule these notifications one at a time.

Example:

UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 60];
n1.alertBody = @"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 90];
n2.alertBody = @"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];

So, even if the user chooses Close when the first notification appears, the second one will still be delivered.

By the way, the didFinishLaunchingWithOptions method gets called right after your application starts, not right before it closes. That said, you can schedule new notifications whenever you want.

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 alert message 111"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getEightAMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)

let localNotification2 = UILocalNotification()
localNotification2.alertBody = "Your alert message22"
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
}

Schedule Local Notification in iOS / irregular time interval

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];

How to schedule a same local notification in swift

UILocalNotification has a repeatInterval property that lets you specify how often the notification should repeat. Note that this is an NSCalendarUnit, not just an arbitrary number, so you can only make a notification repeat once per calendar unit (second, minute, hour, day, week, month, etc.). See the NSCalendarUnit documentation for more options.

For example, you can make a notification repeat every month with:

notification.repeatInterval = .CalendarUnitMonth

When do I schedule local notification

Do it all in applicationWillEnterForeground. Remove any old notifications that don't matter now the user it engaged with the app and install any new notifications for after this use session. If the notification fires when the user is still using the app then you don't need to display anything (and the system won't display anything either).

Fire local notification every day on different times

According to Apple documentation:

An application can have only a limited number of scheduled notifications; the system keeps the soonest firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest

I solved that problem by setting a "queue" of notifications. For example, in my app I have three different types of notifications, let's just say type A, B and C.

I schedule the A, B and C notifications for the next month, everytime the user opens the app, I checked how many notifications left. If, for example, the are no more A notifications, the app schedules more A notifications and so on.

How I achieve this?

Everytime I schedule a notification, I use the userInfo property. I set a dictionary with a key called type and a value.

In my app delegate I check all the pending notifications and count how many left for each type. The code looks like this:

NSArray *scheduledNotifications = [UIApplication scheduledLocalNotifications];

NSUInteger AType, BType, CType;

for (UILocalNotification *notif in scheduledNotifications) {
//Classify notifications by type
NSUInteger notifType = [[notif.userInfo objectForKey:@"type"]integerValue];
if (notifType == 0) {
AType++;
}else if(notifType == 1){
BType++;
}else{
CType++;
}

}

If the count of any type is zero the app schedules more notifications.

Finally, if the notifications are show for example, everyday at the same hour you can use the repeatInterval property BUT you can't create your own repeating intervals, you can only use the repeating intervals defined in NSCalendarUnit.

Hope it helps.

Schedule number of Local Notifications

I've created app with local notifications and in this case user was notified every 20/40/60 minutes in given time interval (eg. 8:00-20:00). The solution was to generate up to 64 notification through whole day and set repeatInterval of UILocalNotification to NSCalendarUnitDay. Of course if you need to use more sophisticated notification pattern, this is not the way to go.

Also It's worth to mention, that since iOS7 I had to, due to client requirements, implement rescheduling of notifications in background app refresh.



Related Topics



Leave a reply



Submit