Reputation: 2028
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
Reputation: 267
Is your notification target is supported mac? I had this trouble
Looks like notification dosn't run and your push notifications handled by system as is.
Upvotes: 0
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
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
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)
Please see attached image. Xcode Notification Services Extension target setting
Upvotes: -1