0

I'm trying to get my task manager app to automatically badge the app icon with the number of tasks due today. I have tried two different solutions, but none are ideal or work exactly how I want.

Solution 1: I set up local notifications to send a notification at 12 in the morning when a task is due that day. The problems are, one, I want to badge the app AFTER the notification is sent but I can't figure out how to do that, and two, every time a new task is created or a due date on another is edited, I have to delete all scheduled local notifications and then reschedule them all. This does not seem efficient, especially because I have to sort through my core data objects to get the due dates.

Solution 2: I set up my app to refresh in the background so the icon badges update, then just updated the badge number anytime a new task or change of due date was made in app. The problems with this are that one, obviously it's kind of a waste of energy to refresh in the background when really I only need it to refresh at the beginning of the day, and two, I can't schedule when it actually refreshes.

Is there another way I should be going about this?

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
user3247022
  • 69
  • 1
  • 9
  • No, there is not. You really have to cancel all notifications, fetch due dates and schedule them fresh. And this also apply for your background refresh. Notifications and badges are not handled in your code, therefore during your rescheduling process you need to set correct badge count to each notification that you reschedule. The only time your code is called is when user interacts with the notification, but you cannot presume that, as you want a correct badge count even when user does nothing or blocks part of your notification. – Martin Koles Jun 30 '14 at 20:25
  • So if my best option is to use local notifications, how do I get it to badge the application on the day the task is due? I know how to send the notification and how to badge the app icon, but how do you specify that you want it to badge when the notification is sent? – user3247022 Jun 30 '14 at 23:29
  • You have to set the correct badge number on the notification when you schedule it. As I said, once you set notifications, their invoking and badge count appearance is out of your control when your code does not have a chance to run. So you must set the correct badge count when you reset the notification. And in case of any changes which happen in your code on the device in case of local notifications, you have to cancel all scheduled notifications, calculate new badge counts and reset all your notifications. This is based on my recent research and it actually works well. But it's not simple. – Martin Koles Jun 30 '14 at 23:50
  • So I can only actually change the badge count when my application runs? – user3247022 Jun 30 '14 at 23:56
  • You can set the badge count to whatever you need in code, but remember, when the notification arrives, the badge is set by the notification which is out of your control. So in order to get the right badge (assume you want the badge number to be the number of due notifications), you need to schedule the notifications with the right (increasing) count and this might be several days in your case ahead. When you app launches, enters background or foreground, you always have a chance to recalculate due notifications and set the app badge, but not when the not. fires. – Martin Koles Jul 01 '14 at 00:02
  • How do I do that? I've tried setting the badge number for the notification, but it ends up badging the application when the notification is created instead of when it's fired. – user3247022 Jul 01 '14 at 00:25
  • Looks like you are presenting the notification immediately, or that you are setting the badge directly. Instead you should set the property of the UILocalNotification and schedule it via UIApplication sharedApplication. I can post the code later or you can search it. It's plenty of it at SO. – Martin Koles Jul 01 '14 at 07:36
  • http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification – Martin Koles Jul 01 '14 at 07:46

0 Answers0