0

I need to have a logic in our app, which allows to define recurring events (e.g. every tuesday, oder every 1st day of a month) which lead then to a specific action in the app.

I thought UILocalNotification would be a good idea, but with this class I send a notification also to the user and I want to process the event only in the app (If the app is not online, then may be the next time the app is up)

Another idea was to set up a list with the event and check every time the app is up, whether an event is due - but this seems quite old fashioned - hope there is something better.

Thanks a lot for any suggestion

Red
  • 1,425
  • 1
  • 10
  • 15

1 Answers1

0

You definitely want to use UILocalNotification in your situation, it will completely fulfill the needs that you described.

Since you said, the event should only fire whenever the app is active (in foreground) you will have to add some custom logic to make this happen, but this is not a very difficult task and you have multiple options here.

What I would suggest is that you use the app lifecycle methods of your AppDelegate, to schedule and remove your notifications when it's appropriate.

The UIApplicationDelegate protocol contains two relevant methods for your case (actually a bit more, but these two will do the job for you...). First we have applicationWillEnterBackground:, that's where you should remove all currently scheduled notifications. In the method applicationDidBecomeActive: you can then reschedule the notifications, as this one is called every time your app is coming to the foreground again.

Let me know if you have further questions :)

nburk
  • 22,409
  • 18
  • 87
  • 132
  • Thanks a lot for your answer. Did I understand right, that it would mean, that whenever the app become active, I reschedule all existing notifications (there will be about 20 to 50 different schedules which were inputed once) and delete them when the app becomes not active anymore?( As I very much appreciate your answer, I somehow had the hope of an easier solution) – Red Nov 10 '14 at 08:54
  • well, ideally you encapsulate the logic to (re)schedule notifications into a dedicated class (e.g. `NotificationManager`). that way, you only have to implement the logic once, and then just reuse the instance of that class in your `AppDelegate`. this should only be one method call in both `applicationWillEnterBackground:` and `applicationDidBecomeActive:`, something like `[notificationManager scheduleNotifications]` and `[notificationManager removeNotifications]` – nburk Nov 10 '14 at 08:57
  • `NotificationManager` is the class that always knows how many notifications to schedule, and when to schedule them, your `AppDelegate` only calls its methods – nburk Nov 10 '14 at 08:58
  • And one more addition, since you have said it'll be between 20 and 50 notifications to be scheduled, you might want to do it in a background thread using `performSelector:withObject:`, this could look like this: `[notificationManager performSelector:@selector(scheduleNotifications) withObject:nil]` – nburk Nov 10 '14 at 09:00
  • Hi, yes I got it, thanks again, will give it a try and let you know. Have a good day – Red Nov 10 '14 at 10:01
  • Great, if it's clear, could you please mark the question as answered :) – nburk Nov 10 '14 at 10:48