I created a custom calendar for iOS , and I am trying using badge number to show number of the Day , but numbers did not change after one day passed , I mean they should update lively here is my code : I need something like weather live application , this application does not send any push notification !
int a = [DateComponents showDay];
[UIApplication sharedApplication].applicationIconBadgeNumber = a ;
It’s not easy to do this currently in iOS. You can get close, but not update the badge reliably every day unless the user opens the app occasionally.
You’ll need to use
UILocalNotification. You can create and schedule a “silent” notification that updates the app badge without alerting the user or playing an alert sound like this:where
dayTomorrowis an int of tomorrow’s day of the month. andsecondsis some time interval – this could be something like the interval to midnight. When this notification is delivered, there will be no alert or sound for the user, but the app badge should be updated to the new value. You could make this notification repeat every day using therepeatIntervalproperty, but the problem here though is that the next day you need to changedayTomorrowto be a different value, and again the day after that. But a repeated alert will always have the same value inapplicationIconBadgeNumber.So I think the only way to achieve anything close to what you want is to schedule multiple of those local notifications – you can schedule up to 64 in advance – each one a day apart, and each one with that day’s value set as the
applicationIconBadgeNumber. These need to be non-repeating, so make sure you setrepeatIntervalto nil (or don’t set it, as nil is the default value). Assuming all of these notifications are delivered reliably by iOS, you would be able to update the badge number silently and without bothering the user for up to 64 days. After that, the badge would stop updating… unless you manage to schedule more notifications in the meantime – i.e. when the user opens up the app. What you could try doing is cancelling all existing scheduled notifications at app launch:and then loop through 64 days in advance, scheduling a notification for midnight each day with the correct day of the month for the
applicationIconBadgeNumberproperty. As long as the user opened up your app at least once every 64 days, you would keep the badge icon up to date. If you envisage your app users opening the app frequently (e.g. multiple times per day), then an optimisation might be to check the number of existing notifications scheduled before cancelling them and creating 64 new ones, like this:A couple of other points to note:
application:didReceiveLocalNotification:is called but the badge icon will not be updated. So in order to cater for this case, I would suggest updating the badge icon to the current day every time the app is launched (or returns to the foreground from the background).EDIT:
Here’s a code snippet to cancel any existing local notifications and schedule 64 in the future for midnight and with the correct day of the month specified as the badge number:
What’s going on here is:
[NSDate date], and then break it into the components we want to keep (year, month, day), and then set the time components (hour, minute, second) to be midnight. This gives usmidnightToday.midnightTodayand adding on the number of seconds per day multiplied by how many days in the future we want the date to be. We use this date to schedule the local notification.NSDateComponentsto break the future date into the components we are interested in – which in this case is only the day so we specifyNSDayCalendarUnit. We can then set theapplicationIconBadgeNumberto becomponents.dayNote that this will probably only work for users that use the Gregorian (western style) calendar – depending on your market you might need to consider the other calendar types in use around the world and support those too.