2013-02-26 133 views
1

如果用户将警报样式设置为横幅。他们可以收到超过1个通知,而不会提示他们清除它。他们然后去使用他们的电话,他们说有3个存储。如果点击最新的一个&它打开应用程序,我只想清除只是这一个通知,我也需要去badgeCount--;如何清除iOS中的通知

我怎样才能实现它与下面的代码? (目前它被设置为清除所有我不想要的东西......)我也注意到有时它会更新徽章号码。但如果我切换回iOS主屏幕,并拉下通知菜单,它仍然存在!

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    if([[userInfo valueForKey:@"aps"] valueForKey:@"alert"] != nil) { 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     if(message != nil) { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Usage Alert" 
      message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
      [alertView show]; 
      [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; 
      [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

     } 
    } 
} 

回答

5
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 


UIApplication *app = [UIApplication sharedApplication]; 
NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number 
badgeNumber--; // decrement by one 
[app setApplicationIconBadgeNumber:badgeNumber]; // set ne badge number 
[app cancelLocalNotification:notification]; // cancel the received notification. It will clear the notification from banner alos 
} 
+0

http://stackoverflow.com/questions/17107957/how-to-clear-a-single-通知 - 从-A-列表中的通知上单击 – SWT 2013-06-14 13:10:14

1

您可以添加

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

到应用程序委托。这将被称为,在那里你可以使用

[[UIApplication sharedApplication] cancelLocalNotification:notification]; 

删除特定的通知并减少徽章计数。

1

我要警惕调用

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:bg_NUM] 

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

方法!

如果一些证件号码您安排本地通知,然后徽章将后置异步几毫秒进入到-didReceiveLocalNotification

样品:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
    // ^^^ maybe not reset badge to 0!! ^^^ 
} 

另一个代码:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    sleep(1); //waiting for system is set our scheduled badge 
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
    // ^^^ most chances for reset badge to 0 ^^^ 
} 

测试代码,屏幕触摸安排本地通知 和系统实际设置徽章之前计算延迟:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
    UILocalNotification *localNotif = [[[UILocalNotification alloc] init] autorelease]; 
    localNotif.applicationIconBadgeNumber = rand()%100+1; 
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 
[...] 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 
    while ([UIApplication sharedApplication].applicationIconBadgeNumber == 0) sleep(0); 
     NSLog(@"badge set: %d after %f sec.", [UIApplication sharedApplication].applicationIconBadgeNumber, CFAbsoluteTimeGetCurrent()-startTime); 
} 

输出:

badge set: 41 after 0.000839 sec. 
badge set: 9 after 0.000754 sec. 
badge set: 56 after 0.076026 sec. 
badge set: 17 after 0.069889 sec. 
badge set: 8 after 0.056245 sec. 
badge set: 71 after 0.120729 sec. 
badge set: 28 after 0.122720 sec. 
badge set: 17 after 0.000758 sec. 

该测试在IOS 4.2/4.3/5.0 /在不同的设备6.1

在-didReceiveLocalNotification消息中重置徽章号码时要小心! (这真仅对LocalNotification /不远程推/且仅当应用程序处于活动上 接收时刻)