如何增加/减少UILocalnotification的application.badge数

时间:2013-05-09 15:54:00

标签: iphone ios objective-c uilocalnotification

当我发出多个通知时,我有UILocalnotification我希望申请号码获得增量,当看到通知时,我希望应用程序徽章号码减少,具体取决于取消/观看通知的通知数量

 - (UILocalNotification *)scheduleNotification :(int)remedyID
{
        NSString *descriptionBody;
        NSInteger frequency;

        UILocalNotification *notif = [[UILocalNotification alloc] init];


        descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"];
        frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue];

        NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];

        for (NSDate *fireDate in notificationFireDates)
        {
                notif.timeZone = [NSTimeZone defaultTimeZone];


                notif.repeatInterval = NSDayCalendarUnit;
                notif.alertBody = [NSString stringWithString:descriptionBody];
                notif.alertAction = @"Show me";
                notif.soundName = UILocalNotificationDefaultSoundName;

                notif.applicationIconBadgeNumber = 1;

                notif.fireDate = fireDate;


                NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody,                                         @"kRemindMeNotificationDataKey",  [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
                                          nil];

                notif.userInfo = userDict;

                [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            }

            return notif;

}

}

- (void)cancelNotification:(int)remedyId
{


    NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

    for (UILocalNotification *notification in notifications)
    {

        int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue];

        NSLog(@"remedyID  : %d",remedyId);
        NSLog(@"notifyId : %d",notifRemedyId);
        if (remedyId == notifRemedyId)
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }

    }

    NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

}

1 个答案:

答案 0 :(得分:7)

您可以简单地使用这两种方法,

-(void) incrementOneBadge{
    NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
    numberOfBadges +=1;

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}

-(void) decrementOneBdge{
    NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
    numberOfBadges -=1;

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}