我已经阅读了几个帖子(比如这一个
https://forums.developer.apple.com/thread/7598)那个设置
application.applicationIconBadgeNumber = -1
在applicationDidBecomeActive或applicationWillEnterForeground上,我可以清除徽章编号,而无需从通知中心删除通知.当我将application.applicationIconBadgeNumber设置为负数(如-1)时,它会清除通知中心的所有通知.是否有其他方法可以在不删除通知的情况下清除徽章编号?
解决方法
使用空的本地通知间接将应用程序徽章设置为-1,但是在清除应用程序徽章计数时,它会在托盘中保留通知.
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版本
if badgeCount == 0 { //set application badge indirectly with UILocalNotification var ln = UILocalNotification() ln.applicationIconBadgeNumber = -1 UIApplication.sharedApplication().presentLocalNotificationNow(ln) } else { UIApplication.sharedApplication().applicationIconBadgeNumber = badgeCount }
在iOS 9和10上测试的方法.