手机型号:iPhone 13 Pro iOS版本号:iOS 18.6.2 (22G100)
用户开启了应用的系统通知功能,在收到离线推送后应用右上角展示未读消息数。在APP启动或者从后台恢复的时候,应用会用如下方法清理应用桌面图标的未读数角标。但是在部分机型上,应用转为“后台模式”时仍然会出现一个未读角标,且每次都是一个固定值;如果直接kill进程就不会出现未读角标。请问如何能够【完全】清理消息未读数,确保不会在退后台的时候再次出现呢?
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (@available(iOS 16.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:nil];
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
}
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @(-1);
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"clearBadge"
content:content
trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
// Do nothing
}];