Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 don't call Notification Service Extension

Tags:

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?

like image 829
just Avatar asked Sep 23 '16 14:09

just


People also ask

What is notification service extension IOS?

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.

What is service extension?

Extension of service means the provision of service through any physical or operational infrastructure arrangement other than consolidation.

What is notification content extension?

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.


2 Answers

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

like image 129
Kamilton Avatar answered Sep 21 '22 15:09

Kamilton


  1. 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.

  2. Make sure the deployment target of the service extension is less that your physical device's OS version.

  3. 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" } 
  1. Don't add the content-available flag in aps or if you've added then make sure it's set to 0.

---------------------------------------- if nothing works, Restart your device --------------------------------------

like image 22
Sudhanshu Gupta Avatar answered Sep 23 '22 15:09

Sudhanshu Gupta