Notificaton服务扩展无法正常工作

rah*_*mar 3 notifications apple-push-notifications ios ios10 serviceextension

当我发送mutable-content时,没有显示通知:1,推送有效负载既不会到达Notification服务扩展中的断点,但是如果没有显示可变内容推送,Notification内容扩展也正常工作.我没有修改Notification服务扩展中的代码,它是xcode生成的默认代码.在创建通知服务扩展时我是否遗漏了某些内容,或者可能是设备设置的问题.我几天前在同一台设备上进行了测试,通知服务扩展工作正常.

以下是我的服务扩展代码

import UserNotifications

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]"

           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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑1:下面是我的推送有效载荷

{  
   "aps":{  
      "sound":"default",
      "mutable-content": 1,
      "category": "myCategory",
      "alert":{  
         "body":"this is a custom push",
         "subtitle":"subtitle of the push",
         "title":"Push Test"
      }

   }
}
Run Code Online (Sandbox Code Playgroud)

edit1:问题出现在10.2.1上运行的特定设备上,我检查了运行在10.2上的其他设备,它触发了Notification服务扩展.

rah*_*mar 13

最后我能够解决这个问题.我不确定是什么导致了这个问题,但经过一些实验后,我意识到不仅我的服务扩展,而且所有其他安装的应用程序的服务扩展都无法正常工作,并且在发送带有"mutable-content:1"的推送有效负载时,未显示通知.重新启动设备后,它开始正常工作.我正在使用iPhone 6s.


小智 8

NotificationServiceExtension 的部署目标也可能是一个原因。

就我而言,我在 iOS 12.4 上运行的设备上进行测试,而我的 NotificationServiceExtension 的目标版本是 14.1。我刚刚将我的 NotificationServiceExtension 目标版本更改为 12.4 它有效。

  • 天啊,这正是我遇到问题的原因。 (2认同)