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
Rather then running the app. You have to run service extension as the target. Then it will ask for which app you have run service extension, then select you app and it will send notification.
Make sure the deployment target is less than the device OS in service extension.
Please ensure payload should contain "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" }
Dont add "Content-available" flag in aps, if need to add make it 0. "Content-available" : 0,
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 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
I have the same issue but my problem was notification extension is "app". It should be appex
you need to add "mutable-content": 1
to your payload
来源:https://stackoverflow.com/questions/39663315/ios-10-dont-call-notification-service-extension