Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset iOS app badge

Hi I'm currently developing an app which uses push notification. I have successfully got it to work with Parse and my application is receiving the notifications. My question is not how to reset the badge when i open the application because i already got that working with this code.

- (void)applicationDidBecomeActive:(UIApplication *)application
{

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Store the deviceToken in the current installation and save it to   Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}

That code removes the badge from the application but when i send another notification the number is now 2 instead of 1. How can i fix this?

like image 240
Johan Enstam Avatar asked Jun 14 '14 11:06

Johan Enstam


2 Answers

[UIApplication sharedApplication].applicationIconBadgeNumber = 0; Not helps to clear badge in parse. I just read the Parse Push notification Guide Documentation and the Documentation said.

badge: The current value of the icon badge for iOS apps. Changing this value on the PFInstallation will update the badge value on the app icon. Changes should be saved to the server so that they will be used for future badge-increment push notifications.

badge: (iOS only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.

Clearing the Badge You need to do Code like:

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  if (currentInstallation.badge != 0) {
    currentInstallation.badge = 0;
    [currentInstallation saveEventually];
  }
  // ...
}
like image 142
Nitin Gohel Avatar answered Nov 13 '22 03:11

Nitin Gohel


For anyone looking for how to reset the badge in swift, here's the swift version @nitin's answer which was spot on.

func applicationDidBecomeActive(application: UIApplication) {
    var current: PFInstallation = PFInstallation.currentInstallation()
    if (current.badge != 0) {
        current.badge = 0
        current.saveEventually()
    }
}
like image 4
dstefanis Avatar answered Nov 13 '22 04:11

dstefanis