Clear applicationIconBadgeNumber without removing notifications not working

纵然是瞬间 提交于 2019-12-01 16:08:49

问题


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?


回答1:


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.




回答2:


UIApplication.sharedApplication.applicationIconBadgeNumber = -1;

I have tested it in iOS 12.2 and it's working fine for me with remote notification.

It is clearing badge count by keeping received remote notification in notification centre.

Code snippet for reference:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    UIApplication.sharedApplication.applicationIconBadgeNumber = -1;
}


来源:https://stackoverflow.com/questions/37789581/clear-applicationiconbadgenumber-without-removing-notifications-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!