我正在尝试使用新的 UNNotificationContentExtension 来显示本地通知的自定义用户界面,但只显示默认通知警报。
我使用 Xcode 模板创建了扩展,并UNNotificationExtensionCategory
在 Info.plist 中指定了扩展。
在此之后,我正在注册通知并设置UNNotificationCategory
inapplication(_:didFinishLaunchingWithOptions:)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
let action = UNNotificationAction(identifier: "Delete", title: "Delete", options: [])
let category = UNNotificationCategory(identifier: "notify-test", actions: [action], minimalActions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
我正在安排通知在应用程序确实使用以下代码进入后台五秒钟后触发:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "notify-test"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "notify-test", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
通知按预期触发,但在显示 iOS 默认样式时,没有显示在默认 Storyboard 文件中定义的自定义 UI。也许有人以前遇到过同样的问题,可以在这里帮助我。