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

UNUserNotificationCenter iOS10本地通知不触发问题排查求助

Why Custom Local Notifications Fail on iOS 10 (But Work on iOS 11)

Hey there, let's dig into why your custom local notifications with custom content and action categories aren't firing on iOS 10—this is a super common gotcha I've debugged before. Even though Apple docs say the feature supports iOS 10, there are subtle behavioral differences between iOS 10 and 11+ that trip up many developers. Here are the most likely culprits and fixes:

1. Category Registration Timing & Format Issues

iOS 10 introduced the UserNotifications framework, but it’s pickier about when and how you register custom categories compared to iOS 11+.

  • Critical order: On iOS 10, you must register your UNNotificationCategory and associated actions before requesting notification permissions. If you request permissions first, the system won’t recognize your custom actions when the notification triggers.
  • Action parameter strictness: For actions like UNTextInputNotificationAction, iOS 10 requires explicit values for all parameters (like textInputButtonTitle and textInputPlaceholder), whereas iOS 11+ allows some defaults. Skipping these will cause iOS 10 to reject the category silently.

2. Custom UNNotificationContent Validation Rules

If you’re using a custom subclass of UNNotificationContent, iOS 10 enforces stricter validation rules than iOS 11.

  • Missing mandatory properties (like title or body) in your custom subclass will cause iOS 10 to drop the notification entirely, even if you set those values dynamically. iOS 11 is more forgiving here, so the notification still fires.
  • Avoid using any iOS 11+ only properties or initializers in your custom content class—these will break compatibility with iOS 10.

3. Mixed API Usage

If you’re accidentally mixing deprecated UILocalNotification APIs with the new UNUserNotificationCenter APIs, iOS 10 will get confused.

  • Stick entirely to the UserNotifications framework for iOS 10+ scheduling. Don’t fall back to UILocalNotification when working with custom categories or actions.
  • Always check the completion handler when adding a notification request—iOS 10 will throw errors that iOS 11 might ignore, which can help you spot issues you’d otherwise miss.

4. Permission & Category Registration Flow (Corrected Example)

Here’s a fixed AppDelegate setup that works reliably on both iOS 10 and 11:

import UserNotifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Step 1: Define custom actions (iOS 10-compatible)
    let primaryAction = UNNotificationAction(
        identifier: "primaryAction",
        title: "Take Action",
        options: []
    )
    let textReplyAction = UNTextInputNotificationAction(
        identifier: "textReplyAction",
        title: "Reply",
        options: [],
        textInputButtonTitle: "Send",
        textInputPlaceholder: "Type your response..."
    )

    // Step 2: Create and register the category BEFORE requesting permissions
    let customCategory = UNNotificationCategory(
        identifier: "customNotificationCategory",
        actions: [primaryAction, textReplyAction],
        intentIdentifiers: [],
        options: []
    )
    UNUserNotificationCenter.current().setNotificationCategories([customCategory])

    // Step 3: Request notification permissions
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        if let error = error {
            print("Permission request failed: \(error.localizedDescription)")
        }
        if granted {
            DispatchQueue.main.async {
                application.registerForRemoteNotifications() // Only if you need remote notifications
            }
        }
    }

    // Rest of your app setup...
    return true
}

When scheduling the notification, double-check you’re setting the correct categoryIdentifier:

let content = UNMutableNotificationContent()
content.title = "Custom Notification"
content.body = "This should fire on iOS 10 and 11!"
content.categoryIdentifier = "customNotificationCategory" // Must match registered category ID

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(
    identifier: "uniqueNotificationID",
    content: content,
    trigger: trigger
)

UNUserNotificationCenter.current().add(request) { error in
    if let error = error {
        print("Scheduling failed: \(error.localizedDescription)")
    }
}

Bonus: Custom Content Extension Check

If you’re using a UNNotificationContentExtension, ensure its Info.plist has the UNNotificationExtensionCategory key set to match your category identifier—iOS 10 is far stricter about this mapping than iOS 11.

Hope this helps you track down the issue and get your notifications firing consistently across iOS versions!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:24:58