简体   繁体   中英

Clearing the badge when received Push Notification

How can I clear the badge which appears on application icon when I receive Push Notification? I want to clear it once user has either tapped on "View" of Push notification alert or has tapped on the app icon.

I suspect you are talking about the SpringBoard's badge:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

Badge count set Zero

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

Cancel all local notifications with this code:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Cancel one local notification with this line of code:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

here theNotification is a UILocalNotification object, so in order to cancel a specific notification, you need to hold on to it's UILocalNotification.

Check this .

For Mac OS X Lion, it's:

    [NSApp dockTile].badgeLabel = @"";

(Lion supports badge-type push notifications.)

From Apple's documentation, set the application.applicationIconBadgeNumber to the number you want displayed on the badge. If you set it to 0, it will be cleared.

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
        [viewController displayItem:itemName];  // custom method
        application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
    }

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

Reference - Scroll down to the Handling Local and Remote Notifications section just above listing 2.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM