'iOS 10 don't call Notification Service Extension

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?



Solution 1:[1]

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

Solution 2:[2]

  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 than 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 --------------------------------------

Solution 3:[3]

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

Solution 4:[4]

If all else fails, restart your device. I just spent 2 hours on this and simply restarting my phone fixed it.

Solution 5:[5]

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: Push Notification

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.

Solution 6:[6]

I have the same issue but my problem was notification extension is "app". It should be appex

Solution 7:[7]

From docs on UNNotificationServiceExtension class:

  • The remote notification is configured to display an alert.

  • The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.

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:

  • content-available: 1

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)

Solution 8:[8]

you need to add "mutable-content": 1 to your payload

Solution 9:[9]

This is a sample if you implement with Swift and Firebase cloud functions (Node.js) to send notifications has attachments with Notification Service Extension. Below i have mentioned some important points only.

Payload sample

const payload = {
    notification: {
        title: name,
        body: messageText,
        badge: "1",
        mutable_content: "true"
    },
    data: {
        type: "MESSAGE",
        fromUserId: name,
        attachmentUrl: imageUrl
    }};

mutable_content: "true" this part is the important i spend so much of time to find the exact naming most of the documentations are invalid now.

The next main thing is the way we have to run the App

Select the Notification Service Extension with your device enter image description here

After when you run you will get a popup to Select your main project

This way it will call the NotificationService didReceive function (Which is inside the Notification Service Extension) when you receive a notification.

Solution 10:[10]

If you trying to send push from Firebase console it's not possible to send "mutable_content": true. So you should send it from server or postman or any type of terminal. And your payload should be like this: For firebase

{
    "to" : "your device token/ firebase topic",
    "notification" : {
      "body" : "This is an FCM notification that displays an image.!",
      "title" : "FCM Notification"
     },
    "mutable_content": true
  }

If not from FCM then your payload should be:

{"aps" : {
    "alert" : {
        "title" : "Your title",
        "subtitle" : "Your subtitle",
        "body" : "Your body"
    },
    "sound" : "default",
    "category" : "message",
    "badge" : 1,
    "mutable-content": 1
    },
    "attachment-url": "https://yourattachment-url.com"
}

Note: And make sure the deployment target of the service extension is less that your physical device's OS version. Like if your development target is 10.3, your service extension version should be 10.2.