ios - 使用 UNNotificationContentExtension 为本地通知显示自定义 UI

标签 ios swift uikit ios10 unnotificationrequest

我正在尝试使用新的 UNNotificationContentExtension 来显示本地通知的自定义用户界面,但只显示默认通知警报。

我使用 Xcode 模板创建了扩展,并在 Info.plist 中指定了 UNNotificationExtensionCategoryenter image description here

在此之后,我将注册通知并在 application(_:didFinishLaunchingWithOptions:)

中设置 UNNotificationCategory
    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。 也许以前有人遇到过同样的问题,可以在这里帮助我。

最佳答案

无论设备是否具有 3D 触摸功能,都可以使用丰富的通知内容。

对于没有 3D 触摸的设备,可以下拉通知以获得丰富的界面。 (或者如上所述在通知中心向左滑动。)

关于ios - 使用 UNNotificationContentExtension 为本地通知显示自定义 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38009267/

相关文章:

ios - SwiftyJSON 响应中的 for 循环

ios - 如何检测与当前设备的 MCSession 的非编程断开连接?

objective-c - 像 iOS 邮件应用程序一样缩放 UIWebView

ios - 具有现有分配的变量上的 Swift EXC_BAD_ACCESS

ios - 用户开始输入时如何记住 UITextView 中的文本属性

ios - 从 UIImageView 生成图像时分辨率损失

ios - 自定义 UIFieldBehavior 中的意外加速

ios - clang : error: linker command failed with exit code 1 when archiving

ios - 确定 UILocalNotification 是否是从调度或用户交互中触发的

ios - 更改 NSLayoutConstraint 时的 SWIFT 动画