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?
[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];
}
// ...
}
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With