使用UNNotificationContentExtension显示自定义UI以进行本地通知

时间:2016-06-24 08:52:15

标签: ios swift uikit ios10 unnotificationrequest

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

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

在此之后,我正在注册通知并设置UNNotificationCategory中的application(_: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])

我正在安排通知在App使用此代码进入后台后五秒钟触发:

    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。 也许有人之前遇到过同样的问题,可以帮助我。

4 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:2)

据我所知,UNNotificationContentExtension还支持没有3DTouch的设备,对于那些设备,系统会在锁定屏幕上为通知添加向左滑动行为,在翻转后你可以看到名为{{1}的按钮然后,如果你点击它,你可以看到内容扩展

答案 2 :(得分:0)

您的代码没有问题。

据我所知,UNNotificationContentExtension只是支持3DTouch的支持设备(测试环境iOS10 beta1)。与UNTextInputNotificationAction相同。

然后按或下拉菜单可以显示自定义UIViewcontroller。

答案 3 :(得分:0)

刚刚体验过我的UNNotificationContentExtension没有在运行iOS 10.2.1的iPhone 7上显示。

奇怪的是,内容扩展程序可以很好地显示在iOS模拟器和运行iOS 10.3.1的iPhone 6S上。

修复:将iPhone 7更新到最新版本的iOS(10.3.1)修复了问题,所有内容都按现在显示。看起来一定是iOS本身的一个错误。

相关问题