Clear applicationIconBadgeNumber without removing notifications not working

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

    application.applicationIconBadgeNumber = -1

on applicationDidBecomeActive or applicationWillEnterForeground would allow me to clear the badge number without removing the notifications from the notification center. When I set application.applicationIconBadgeNumber to a negative number (like -1) it clears all my notifications from the Notification Center . Is there another way to clear the badge number without removing the notifications ?


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.

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

上一篇: GitHub包含另一个存储库

下一篇: 清除applicationIconBadgeNumber而不删除通知不起作用