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?
A notification service app extension doesn't present any UI of its own. Instead, it's launched on demand when the system delivers a notification of the appropriate type to the user's device. You use this extension to modify the notification's content or download content related to the extension.
Extension of service means the provision of service through any physical or operational infrastructure arrangement other than consolidation.
Overview. The UNNotificationContentExtension protocol provides the entry point for a notification content app extension, which displays a custom interface for your app's notifications. You adopt this protocol in the custom UIViewController subclass that you use to present your interface.
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" }
content-available
flag in aps
or if you've added then make sure it's set to 0
.---------------------------------------- if nothing works, Restart your device --------------------------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With