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

mha*_*ddl 5 uikit ios swift ios10 unnotificationrequest

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

我使用Xcode模板创建了扩展,并UNNotificationExtensionCategory在Info.plist中指定了该扩展. 在此输入图像描述

在此之后,我注册了通知和设置UNNotificationCategoryapplication(_: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])
Run Code Online (Sandbox Code Playgroud)

我安排通知在应用程序使用此代码进入后台后五秒触发:

    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)
Run Code Online (Sandbox Code Playgroud)

通知按预期触发,但在显示iOS默认样式时,未显示默认Storyboard文件中定义的自定义UI.也许有人之前遇到过同样的问题,可以帮助我.

bla*_*ckp 5

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

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


maq*_*ene -1

你的代码没有问题。

\n\n

据我所知\xef\xbc\x8cUNNotificationContentExtension是唯一支持3DTouch的设备(测试环境iOS10 beta1)。与 UNTextInputNotificationAction 相同。

\n\n

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

\n