Aaron Bratcher
Aaron Bratcher

Reputation: 6471

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?

Upvotes: 5

Views: 1442

Answers (1)

Tom
Tom

Reputation: 3861

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.

Upvotes: 3

Related Questions