2011-04-28 84 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