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

Posted

技术标签:

【中文标题】如何增加/减少 UILocalnotification 的 application.badge 编号【英文标题】:How to increment/decrement application.badge number for UILocalnotification 【发布时间】:2013-05-09 15:53:59 【问题描述】:

当多个通知被触发时我有 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]);


【问题讨论】:

在这里.. http://***.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification 【参考方案1】:

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

-(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];

【讨论】:

numberOfBadges -=1;这是用来递减applicationiconbadgenumber的吧? 我应该在我的 MainViewController 中使用此方法还是应该在 appdelegate 中也存在此方法 我会让你知道 tomo ...顺便说一句,当有多个通知时,徽章编号将自动增加 你可以根据需要设置数字,[[UIApplication sharedApplication] setApplicationIconBadgeNumber:4];或任何数字 如果我设置 4 ...并说 3 个通知被触发 ..它是否显示 3 applicationbadgenumber ? ..is setApplicationBadgeNumber : 4 表示最多显示 4 个?

以上是关于如何增加/减少 UILocalnotification 的 application.badge 编号的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Pan Gesture 增加/减少字体大小?

如何在C中找到堆栈正在增加或减少?

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

如何在 Common Lisp 中增加或减少数字?

如何在 Android 中以编程方式增加和减少音量

如何减少仓库,增加存货点