2011-04-28 82 views
8

当我收到推送通知时,如何清除出现在应用程序图标上的徽章?一旦用户点击了“查看”推送通知提醒或点击了应用程序图标,我想清除它。收到推送通知时清除徽章

回答

22

我怀疑你是在谈论跳板的徽章:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0] 
0

从苹果公司的文件,设置application.applicationIconBadgeNumber到要显示的徽章的数量。如果将它设置为0,它将被清除。

- (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

1

对于Mac OS X Lion中,它是:

[NSApp dockTile].badgeLabel = @""; 

(狮子支持徽章式推送通知。)

4

徽章计数设为零

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0] 

取消所有本地通知与此代码:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

取消一个本地通知,这行代码:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification]; 

这里所述通知是UILocalNotification对象,所以为了消除特定的通知,需要坚持它的UILocalNotification。

检查this