1

如果用户将警报样式设置为横幅。他们可以收到超过 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];

        }
    }
}
4

3 回答 3

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
}
于 2013-02-26T04:42:33.867 回答
1

你可以加

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

给您的应用委托。这将被调用,您可以在那里使用

[[UIApplication sharedApplication] cancelLocalNotification:notification]; 

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

于 2013-02-26T03:03:27.220 回答
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 /不是远程推送/并且仅当应用程序在接收时刻处于活动状态时)

于 2013-08-22T21:12:22.677 回答