Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I schedule local notification for the following scenario?

Experts, I have a scenario wherein I need to notify user three times a day (morning, afternoon, evening). And the timings for these notifications will be different for each day, based on database values for each date.

These three notifications are configurable. Meaning user might just choose to set afternoon and evening while turning off morning notification under settings.

As per my understanding I can achieve this using local notifications.

I can do the following: - before the application exits, inside didFinishLaunchingWithOptions, I can check what is the next notification due, is it set (on/off). If it is set I schedule it. If not I move on to the next notification type and do the same thing. If all the notifications are turned off, obviously, no notifications will be scheduled.

Now when the notification shows up i get to see alert with two buttons "Close" and "View". If user selects "View" My app is back to active and before user exits next notification is scheduled.

So far so good.

Now if user choose to select "Close" What do I do? It wont launch my app and consequently next notification wont be scheduled?

How do I achieve this? Is there any better way to do this?

Help! Help! Help!

like image 679
Haris Farooqui Avatar asked Dec 08 '12 00:12

Haris Farooqui


People also ask

What is a local notification?

Local notifications are scheduled by an app and delivered on the same device. They are suited for apps with time-based behaviors, such as calendar events. When you run your app on a device with Android OS 8.0 or above, Kony uses default channels that are mentioned in the localnotificationconfig.

How many local notifications can be scheduled iOS?

As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.


2 Answers

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.

like image 83
Nate Avatar answered Oct 04 '22 04:10

Nate


You can also use the repeatInterval property so that they reschedule themselves indefinitely. However, you are limited to the units in NSCalendarUnit. See the docs for more info

like image 33
Nick Yap Avatar answered Oct 04 '22 02:10

Nick Yap