0

I have an app implementing push notifications with parse.

I am having some issues clearing the badge icon when a user opens up the app.

I have the following line of code in my appDelegate didFinishLaunchingWithOptions and in the app didBecomeActive:

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

This works temporarily in that if I have a badge number of 18 and then I take my app out of the background via launching it, I then press the home button and I can see the app badge number was cleared and there is no longer a red icon a number in it for my app, as expected.

But then if I receive a new notification, the app badge does not read 1 as expected but instead 1 is added to the previous badge number that had been cleared and is now 19.

How can I handle this?

My app sends push notifications with the following data:

var data = [ "title": "xxx",
             "alert": message, 
             "badge" : "Increment",
             "sound" : "default"]
Wain
  • 118,658
  • 15
  • 128
  • 151
user2363025
  • 6,365
  • 19
  • 48
  • 89

1 Answers1

3

Your issue is that the you are not clearing the badge number saved in Parse's PFInstallation.

Update the badge number using PFInstallation rather than directly modifying it using UIApplication.sharedApplication().applicationIconBadgeNumber = 0

PFInstallation will automatically take care of clearing the badge icon as well as updating the badge number in Parse's backend.

Add the following to the app delegate's didFinishLaunchingWithOptions and also to applicationDidBecomeActive

if (PFInstallation.currentInstallation().badge != 0) {
    PFInstallation.currentInstallation().badge = 0
    PFInstallation.currentInstallation().saveInBackground()
}

You may also want to add the following to applicationDidBecomeActive

// Clears out all notifications from Notification Center
UIApplication.sharedApplication().cancelAllLocalNotifications()
Russell
  • 3,099
  • 2
  • 14
  • 18