清除applicationIconBadgeNumber而不删除通知不起作用

我读过几篇文章(比如https://forums.developer.apple.com/thread/7598)这个设置

    application.applicationIconBadgeNumber = -1

applicationDidBecomeActiveapplicationWillEnterForeground将允许我清除徽章号码而无需从通知中心删除通知。 当我将application.applicationIconBadgeNumber设置为负数(如-1)时,它会清除Notification Center所有通知。 是否有另一种方法来清除徽章号码而不删除notifications


通过一个空的本地通知间接地将应用程序标记设置为-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上测试的方法。

链接地址: http://www.djcxy.com/p/92269.html

上一篇: Clear applicationIconBadgeNumber without removing notifications not working

下一篇: Laravel : How to store json format data in database?