Pergunta

So, I have an exercise app where we have 10/15 Second repetitive timers through which we show the progress done by the User. For making it work on background as well, I have used local notifications. However, as stated by Apple, we can only have ONLY 64 local notifications at any given instance of our app.

So, what happens now is when app is in background and these 64 notifications have finished, I have no control over the app. I can re-add the 64 notifications if the User opens the app. But if User doesn't open the app at all and these 64 notifications are finished then what should I do?

Is there any event which occurs inside the app when any local notification arrives? Is there any way to exceed the number of notifications?

I think there should be a solution as there are many Alarm Apps which work on the same methodology.

Please help!

Foi útil?

Solução

You can use the repeatInterval property of UILocalNotification to make your notification automatically repeat every second, minute, hour, day, or other unit (see NSCalendarUnit documentation):

notification.repeatInterval = NSMinuteCalendarUnit;

For your case, this means that you can create 5 notifications with fireDates 10 seconds apart, each with a repeatInterval of NSMinuteCalendarUnit. This will result in the user receiving a notification every 10 seconds indefinitely, and you will have only scheduled 5 notifications, which is well below the limit of 64.

The only downside of this technique is that there will be no way to stop the notifications until the user opens the app, as you will need to cancel all the notifications to stop them, and you can only do that while the app is running.

Outras dicas

There is no way to do this.

No delegate will be called if a notification comes when your app is in killed state or in background.

Note:

Only 64 local notification can be scheduled at a time. Possibly the alarm apps are using the repeatInterval property of UILocalNotification

Eg:

notification.repeatInterval = NSDayCalendarUnit;

It'll repeat each day.

I have an exercise app that does the same thing and I had the same problem. My solution was to put all the timers into a "pending" queue and then schedule the local notifications from that queue.

On first run - when the user presses Go - while the (number of notifications < 64) && (pending queue count > 0), remove each timer from the pending queue and add it to the notifications queue.

Each time the app launches, in applicationDidFinishLaunchingWithOptions in the appDelegate check whether ([notificationsQueue count] < 64 && [pendingQueue count > 0]) and top up the notifications queue. If there are more than 64 in the pending queue, they remain there until the app is launched and there is space in the notifications queue.

The queue system works just perfectly.

It doesn't get round the problem of the user not opening the app within 64 notifications, however, in my testing users didn't ever get close to creating 64 exercise timers and rarely made it past 30. Also, it's quite common for the user to open the app to pause a timer, at which point the queue logic deals with topping up the notifications.

I've never had a single problem with this method. Then again, if your timers are identical (10/15seconds) a repeatInterval will do the job as PartiallyFinite wrote... while I was writing this : )

The end.

Steve

This method is called in AppDelegate when a local notification is received. You can do what ever you want here.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"recieved notification %@", notification);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top