如果用户将警报样式设置为横幅。他们可以收到超过1个通知,而不会提示他们清除它。然后他们去使用他们的手机,他们说3存储。如果点击最新的一个&它打开了应用程序,我只想清除这一个通知,我还需要去badgeCount--;
如何使用下面的代码实现它? (目前它已经开始清理所有我不想要的东西......)我也注意到有时它会更新徽章编号。但如果我切换回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];
}
}
}
答案 0 :(得分: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
}
答案 1 :(得分:1)
您可以添加
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
到你的app代表。这将被调用,你可以使用
[[UIApplication sharedApplication] cancelLocalNotification:notification];
删除特定通知并减少徽章数。
答案 2 :(得分: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 /非远程推送/且仅在应用程序处于活动状态时才有效 接受片刻)