如何在没有 Storyboard 的情况下创建 UNNotificationContentExtension?

Aar*_*her 5 push-notification ios ios-app-extension

将内容扩展添加到我的应用程序的通知处理中。我可以正常工作,但我想在不直接指定故事板的情况下完成它。原因是我希望能够将其传递给第三方框架(我正在开发)来处理呈现通知和管理用户响应。

我尝试删除该NSExtensionMainStoryboard条目并添加一个NSExtensionPrincipalClass条目,以便可以在代码中加载视图。但是,我的课程没有被实例化。这是类定义:

class NotificationViewController: NSObject, UNNotificationContentExtension {
    override init() {
        super.init()
        print("extension instantiated")
    }

    func didReceive(_ notification: UNNotification) {
        print("notification received")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 NSExtension 条目:

<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>UNNotificationExtensionDefaultContentHidden</key>
        <true/>
        <key>UNNotificationExtensionCategory</key>
        <string>Messaging</string>
        <key>UNNotificationExtensionInitialContentSizeRatio</key>
        <real>1</real>
    </dict>
    <key>NSExtensionPrincipalClass</key>
    <string>NotificationViewController</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.usernotifications.content-extension</string>
</dict>
Run Code Online (Sandbox Code Playgroud)

有人为内容扩展做过这个吗?

Tom*_*Tom 3

根据这个答案,我认为您在以下内容中缺少模块前缀NSExtensionPrincipalClass

该值应该是扩展的命名空间和主 ViewController 的类。例如,如果您的扩展名为 Pretty Notification并且类为PrettyNotificationViewController,则您将输入Pretty_Notification.PrettyNotificationViewController