Clear applicationIconBadgeNumber without removing notifications not working

后端 未结 2 1278
借酒劲吻你
借酒劲吻你 2021-01-12 01:15

I\'ve read in a few posts (like this one https://forums.developer.apple.com/thread/7598) that setting

    application.applicationIconBadgeNumber = -1
         


        
2条回答
  •  抹茶落季
    2021-01-12 01:55

    Setting application badge to -1 indirectly with a empty local notification worked for me, but its a hack to persist notifications in tray while clearing application badge count.

    if(badgeCount == 0) {
       //set application badge indirectly with UILocalNotification
       UILocalNotification *ln = [[UILocalNotification alloc]init];
       ln.applicationIconBadgeNumber = -1;
       [[UIApplication sharedApplication] presentLocalNotificationNow:ln];
    }
    else {
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeCount];
    }
    

    Swift Version

    if badgeCount == 0 {
        //set application badge indirectly with UILocalNotification
        var ln = UILocalNotification()
        ln.applicationIconBadgeNumber = -1
        UIApplication.sharedApplication().presentLocalNotificationNow(ln)
    }
    else {
        UIApplication.sharedApplication().applicationIconBadgeNumber = badgeCount
    }
    

    Approach tested on iOS 9 and 10.

提交回复
热议问题