iOS本地通知设置角标主线程警告及数值异常问题咨询
iOS本地角标更新问题解决方案
问题1:消除主线程访问警告
UIApplication相关的所有UI属性都属于UIKit接口,苹果要求必须在主线程访问,在后台线程读取applicationIconBadgeNumber是触发警告的直接原因。
正确实现方式是将角标的读取、计算逻辑全部放到主队列执行,修正后代码如下:
DispatchQueue.main.async { // 所有对applicationIconBadgeNumber的读写都放在主队列 let currentBadge = UIApplication.shared.applicationIconBadgeNumber let notificationContent = UNMutableNotificationContent() notificationContent.title = "Test Title" notificationContent.subtitle = "Test Subtitle" notificationContent.body = "Test Body" notificationContent.sound = content.sound notificationContent.badge = NSNumber(value: currentBadge + 1) // 同步更新当前角标 UIApplication.shared.applicationIconBadgeNumber = currentBadge + 1 // 后续添加通知到通知中心的逻辑 let request = UNNotificationRequest(identifier: "your_notification_id", content: notificationContent, trigger: yourTrigger) UNUserNotificationCenter.current().add(request) }
调整后即可消除主线程访问警告。
问题2:本地通知场景下角标数值准确性保障方案
原实现逻辑提前把角标数值写死在通知内容里,一旦出现通知取消、调度顺序变更的情况,预先计算的数值就会和实际情况不符,属于设计逻辑问题,可通过以下方案解决:
- 单独维护全局角标数据源
不要依赖系统返回的applicationIconBadgeNumber做累加计算,单独在UserDefaults里持久化存储一个自定义的未读计数变量,所有角标的计算都基于这个变量,避免多线程读取、系统状态变更带来的数值不准。 - 动态更新待触发通知的角标数值
每次新增/取消本地通知时,先获取所有待发送的通知列表,重新计算每个通知对应的角标值,逐个更新:
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in var badgeCount = self.getCurrentGlobalBadgeCount() // 读取自定义维护的全局未读计数 DispatchQueue.main.async { UIApplication.shared.applicationIconBadgeNumber = badgeCount } // 按触发时间倒序遍历待触发通知,保证早触发的通知角标值更小 let sortedRequests = requests.sorted { req1, req2 in guard let trigger1 = req1.trigger as? UNCalendarNotificationTrigger, let trigger2 = req2.trigger as? UNCalendarNotificationTrigger, let date1 = trigger1.nextTriggerDate(), let date2 = trigger2.nextTriggerDate() else { return false } return date1 > date2 } for req in sortedRequests { badgeCount += 1 guard let newContent = req.content.mutableCopy() as? UNMutableNotificationContent else { continue } newContent.badge = NSNumber(value: badgeCount) let newReq = UNNotificationRequest(identifier: req.identifier, content: newContent, trigger: req.trigger) UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [req.identifier]) UNUserNotificationCenter.current().add(newReq) } }
- 兜底校验逻辑
每次App进入前台、用户点击通知、用户清空通知时,都重新计算全局未读计数,一次性刷新系统角标数值,覆盖之前可能出现的计算误差。
内容的提问来源于stack exchange,提问作者user14341201
相关产品推荐
相关产品推荐

