简体   繁体   中英

How do I create a UNNotificationContentExtension without a Storyboard?

Adding content extension to my app's notification handling. I have it working, but I want to do it without a storyboard directly specified. The reason is that I want the ability to pass this on to a 3rd party framework (that I'm developing) to handle presenting the notification and managing the user response.

I tried removing the NSExtensionMainStoryboard entry and adding an NSExtensionPrincipalClass entry so I can load the view in code. However, my class isn't being instantiated. Here's the class definition:

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

    func didReceive(_ notification: UNNotification) {
        print("notification received")
    }
}

Here's my NSExtension entry:

<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>

Has anyone done this for content extensions?

Per this answer , I think you are missing a module prefix in your NSExtensionPrincipalClass :

The value should be the namespace of your extension and the class of the main ViewController. For example, if your extension is called Pretty Notification and the class is PrettyNotificationViewController , you would enter Pretty_Notification.PrettyNotificationViewController .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM