Tom Shen
Tom Shen

Reputation: 2028

macOS Notification Service Extension not being used

I have a notification service extension for my macOS app.

Here is the code for that extension:

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        print("Extension received notification!")

        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        bestAttemptContent?.title = "Title modified!"
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

My payload is also pretty simple:

{"aps": {
        "alert":{"title":"Test1", "subtitle":"Test2", "body":"Test3"},
        "sound":"default",
        "mutable-content":1,
        "category":"news"
    }
}

However, upon receiving the notification, the title is not modified. I also tried the Attach to process by PID or name menu, I can not attach to this extension, which means it is not being ran.

Many other question were asking about iOS, I tried the solutions for those, but unfortunately they don't work.

Any ideas?

Upvotes: 6

Views: 1755

Answers (4)

Serg Basin
Serg Basin

Reputation: 267

Is your notification target is supported mac? I had this trouble

Notification settings

Looks like notification dosn't run and your push notifications handled by system as is.

Upvotes: 0

janh
janh

Reputation: 242

Check that you don't have multiple versions of the app installed. After I deleted the release version of the app (same bundle ID), it all started to work.

pluginkit and this SO thread pushed me into the right direction.

Upvotes: 1

momo
momo

Reputation: 54

1- An other possibility to not have an app extension run (or seems like it was not run) is that some thing happened and the extension did fail/crash and the OS delivered the Push notification without changes. (try to debug your extension using Xcode.)

2- Try also clean your target and derived data in Xcode.

3- Ensure that your extensions' entitlement has all supported Mac (Mac Catalyst) and not only suported iOS keys (if using for Mac Catalyst).

4- (Mac Catalyst) Try to run the extension on iOS (if it did but not on the Mac it means that some configuration/code crash should not be in your Mac target) Again debugging should help

Upvotes: 0

momo
momo

Reputation: 54

I had the same issue with Mac Catalyst (the iOS Notification Services extension is working fine but Not for MAC OS). (Xcode 11.1) For Mac OS X (Mac Catalyst) (if you already have the "App Sandbox" capability please skip to step 5)

  1. Select your extension's target.
  2. then on top select the tab "Signing and Capabilities"
  3. than click on Capability +
  4. Add the "App Sandbox" capability
  5. Ensure that Incoming Connection (Server) is checked (enabled).

Please see attached image. Xcode Notification Services Extension target setting

Upvotes: -1

Related Questions