UILocalNotification应用程序徽章编号重复

时间:2013-01-09 18:19:43

标签: iphone ios objective-c cocoa-touch

我在我的应用中使用UILocalNotification s。有时我会安排重复通知,有时会安排非重复通知。每当通知触发时我想增加应用程序徽章编号。所以我做localNotification.applicationIconBadgeNumber = badgeNumber;这工作正常...除非通知是重复通知,因为如果我之前已经设置了带有徽章编号1的重复通知,那么我设置另一个带有徽章编号2的非重复通知。 / p>

流程就是这样:

Notification 1 fires (first time) - Badge number = 1 (OK!) 
Notification 2 fires              - Badge number = 2 (OK!) 
Notification 1 fires (repeating)  - Badge number = 1 (ERROR)

如您所知,当通知1再次触发时,我仍然希望应用程序徽章编号显示2(对于2个错过的通知)。我可以实现这种行为吗?还是有一些我错过的明显的东西?对我来说,重复通知的更合乎逻辑的徽章行为是它第一次设置徽章编号,以后随后不会更改徽章编号。

4 个答案:

答案 0 :(得分:1)

简单:对不起,戴夫。我恐怕不能那样做。

使用UILocalNotification时,无法让徽章计数保持在您想要的“最高”数。

徽章计数将始终设置为您在安排通知时设置的值(如您所知)。您无法假设用户每次收到通知时都会打开您的应用程序,因此在每次应用程序启动时更新已安排的通知(重复和非重复)的徽章计数将无法确定,也不会成为一种选择。

唯一可行的方法是使用推送通知,跟踪服务器上的所有“未读”通知,并在用户启动应用后立即将通知标记为“已读”。 / p>

答案 1 :(得分:1)

我认为处理重复通知的唯一方法是在第一次触发时增加应用程序徽章编号,以安排您的重复通知而不使用徽章编号:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = @"1";
localNotification.alertAction = @"Send";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSMinuteCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

然后安排一个通知,该通知仅在第一次触发重复通知的同时设置应用程序徽章编号:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
localNotification.hasAction = NO;
localNotification.alertBody = nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

答案 2 :(得分:0)

您可以使用if条件来执行此操作,例如:

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)localNotification
{
   if([UIApplication sharedApplication].applicationIconBadgeNumber < localNotification.applicationIconBadgeNumber)
   {
      [UIApplication sharedApplication].applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber;
   }
}

(未检查上述代码)。

答案 3 :(得分:0)

在我的应用中,我必须使用重复的本地通知添加徽章编号。每次用户打开应用程序时,都应重新设置徽章编号。因此,所有预定通知都应重新计算徽章编号。

func applicationDidEnterBackground(application: UIApplication) {
    addBadgeNumberToScheduledNotifications()
}

func addBadgeNumberToScheduledNotifications() {
    var badgeNumber: Int = 0
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    if let notifications = UIApplication.sharedApplication().scheduledLocalNotifications {
        UIApplication.sharedApplication().cancelAllLocalNotifications()
        for notification in notifications {
            notification.applicationIconBadgeNumber = notification.fireDate > NSDate() ? ++badgeNumber : 0
            UIApplication.sharedApplication().scheduleLocalNotification(notification)
        }
    }
}