开发者

Clearing the badge when received Push Notification

How can I clear the badge which appears on appl开发者_JS百科ication 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜