Skip to content

Badge Management for iOS

Today we’re excited to announce the release of automatic badge management in iOS. The heart of this feature is the
PFInstallation class, which lets you persist badge numbers to Parse for use in auto-badged push notifications.

Let’s take a walk through a typical example. Say you are building a group messaging app, and want to push a message from one sender to a set of receiving devices that will increment each device’s badge. The tricky thing is that the badge numbers could be different among the receiving devices, and the sender doesn’t know what those badge numbers are. Parse will take care of it for you, though, if the sending device creates a push like this:

NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
messageContent, @”alert”,
@”Increment”, @”badge”,
PFPush *push = [[PFPush alloc] init];
[push setChannel:chatRoomName];
[push setData:data];
[push sendPushInBackground];

The special "Increment" badge value will tell Parse that the badge
field for each matching Installation should be incremented and the
new value used in the push payload. If you were to specify a numerical
value for the badge, that value will be written in the badge field of
each matching Installation before the push is sent. The badges can
be left the same by simply omitting the badge key from your push data.

Of course, badges eventually need to be cleared, and a good time is
usually when your app is opened. Setting the badge property on the
current installation will update the application icon badge number
and ensure that the latest badge value will be persisted to the server
on the next save, so all you need to do is:

- (void)applicationDidBecomeActive:(UIApplication *)application {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
// …

The UIApplicationDelegate documentation contains more information on hooks into an app’s life cycle; the ones which are most relevant for resetting the badge count are applicationDidBecomeActive:, application:didFinishLaunchingWithOptions:, and application:didReceiveRemoteNotification:.

We hope you find this feature useful in managing badges for your app!