I tried to implement the new Notification Service Extension, but I have a problem.
In my NotificationService.swift file I have this code:
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
print(bestAttemptContent.body)
contentHandler(bestAttemptContent)
}
}
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)
}
}
}
When I got a push notification the didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) method never called.
Maybe I misunderstood how this extension is working?
Check your deployment target on Service Extension.
I had deployment target set to 10.2 when testing on device with 10.1 and extension wasn't called until I changed it.
Another issue might be debugging.. In order to make it work You need to attach Service Extension Process. In Xcode menu Debug > Attach To Process > Name of your extension
Run service extension as the Target instead of the app. Then it will ask for which app you have run service extension, then select your app and it will send the notification.
Make sure the deployment target of the service extension is less that your physical device's OS version.
Ensure payload contains mutable-content: 1
{"aps" : {
"alert" : {
"title" : "Introduction To Notification",
"subtitle" : "Session 707",
"body" : "New Notification Look Amazing"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://api.buienradar.nl/Image/1.0/RadarMapNL"
}
- Don't add
content-available
flag in aps
or if you've added then make sure it's set to 0
.
I was getting crazy. Finally I realized that I had the deployment target
of Notification Service Extension
was 10.3
(my phone too). I changed to 10.2
and it works perfectly
Your push notification payload should contain the "mutable-content" : 1 key value pair.
The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.
Ex of push notification payload JSON:
{
"aps":{
"alert": {
"body": "My Push Notification",
"title" : "Notification title"},
"mutable-content" : 1,
"badge":0},
}
This works perfectly fine and i get the Push notification as follows:
Also Note That :
You cannot modify silent notifications or those that only play a sound or badge the app’s icon.
You can try Pusher or Houston for testing the Push Notifications.
I have the same issue but my problem was notification extension is "app". It should be appex
From docs on UNNotificationServiceExtension
class:
You cannot modify silent notifications or those that only play a sound or badge the app’s icon.
Basically
Must include:
mutable-content: 1
- an
alert
dictionary.
Must NOT include:
To summarize Apple is doing its best to not allow apps to mutate silent notifications. It wants to allow that only on user notifications (user facing notifications)
you need to add "mutable-content": 1
to your payload