You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用处于后台时通知角标不更新的技术求助

iOS本地通知后台角标不更新问题

我的iOS应用会在特定时间生成本地通知,通知可正常生成并显示。应用处于前台时,通知角标会正确更新为显示1条通知,但应用处于后台状态时,角标无法更新。另外,后台处理通知的didReceive方法未执行,终端未打印"here"。

相关代码如下:

// 该代码在指定日期(action.date)前7天调度通知
private func scheduleNotificationIfRequired() {
    let sevenDaysBeforeActionDate = Calendar.current.date(byAdding: .day, value: -7, to: action.date)!
    var triggerDateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: sevenDaysBeforeActionDate)
    
    triggerDateComponents.hour = 21
    triggerDateComponents.minute = 51
    
    let content = UNMutableNotificationContent()
    content.title = "Upcoming Deadline"
    content.body = "Action Required by: \(formattedDate(from: action.date))"
    content.sound = UNNotificationSound.default

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDateComponents, repeats: false)
    
    let identif = action.action + action.name
    
    let request = UNNotificationRequest(identifier: identif, content: content, trigger: trigger)
    
    UNUserNotificationCenter.current().add(request) 
}

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var viewRouter: ViewRouter?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        return true
    }
    
    // 应用前台时处理通知
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // 更新应用图标角标
        let currentbadge = UIApplication.shared.applicationIconBadgeNumber
        UIApplication.shared.applicationIconBadgeNumber = currentbadge + 1
        
        completionHandler([.badge, .sound, .banner]) // 确保通知横幅显示
        
        // 前台点击通知时处理逻辑
        handleNotification(notification)
    }
    
    // 该方法未按预期工作,"here"从未打印到终端
    // 应用后台或终止时处理通知(仅用户点击通知时触发)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
       // 更新应用图标角标
       let currentbadge = UIApplication.shared.applicationIconBadgeNumber
       UIApplication.shared.applicationIconBadgeNumber = currentbadge + 1
       print(currentbadge)
       print("here")
       print(UIApplication.shared.applicationIconBadgeNumber)
       
       completionHandler()
   }
}

问题原因及修复方案

1. 未通过通知内容设置角标

应用后台时,willPresent方法不会触发,当前代码仅在前台方法中更新角标。正确做法是在调度通知时,直接给UNMutableNotificationContent设置badge属性,系统会自动更新角标,无需依赖前台或点击事件:

let content = UNMutableNotificationContent()
content.title = "Upcoming Deadline"
content.body = "Action Required by: \(formattedDate(from: action.date))"
content.sound = UNNotificationSound.default
// 根据当前未读数量设置角标值
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)

2. 明确didReceive方法的触发条件

该方法仅在用户点击通知时才会执行,并非通知送达后台就自动触发。如果需求是通知一送达就更新角标,无需依赖此方法,直接设置content.badge即可。

3. 确认后台通知权限

  • 在Info.plist中添加UIBackgroundModes键,包含remote-notification值(本地通知也需要此权限以在后台处理相关逻辑)
  • 确保已向用户申请并获得通知权限(包括角标权限)

4. 后台状态下避免直接修改角标

后台状态下直接修改UIApplication.shared.applicationIconBadgeNumber可能无法立即生效,通过通知内容设置角标是系统推荐的可靠方式。

内容的提问来源于stack exchange,提问作者Carlos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:38:10