iOS 10 don't call Notification Service Extensi

2020-07-02 08:29发布

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?

7条回答
Ridiculous、
2楼-- · 2020-07-02 08:45

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

查看更多
三岁会撩人
3楼-- · 2020-07-02 08:45

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)

查看更多
一纸荒年 Trace。
4楼-- · 2020-07-02 08:47

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.

查看更多
神经病院院长
5楼-- · 2020-07-02 08:49

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

查看更多
smile是对你的礼貌
6楼-- · 2020-07-02 08:50
  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 content-available flag in aps or if you've added then make sure it's set to 0.
查看更多
Evening l夕情丶
7楼-- · 2020-07-02 08:52

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

查看更多
登录 后发表回答