2

当我发送带有推送负载的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) 
     } 
    } 
} 

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

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

EDIT1:这个问题似乎特定设备上运行的10.2.1,我检查了10.2上运行的其他设备,它触发了通知服务扩展。

+0

你如何发送包含“mutable-content:1”的Json?如果它与FCM通过“_”更改“ - ” – SeikoTheWiz

+0

Hi @SeikoTheWiz i我没有使用FCM – princerk

+0

Hi @princerk我也在尝试实现通知服务扩展,但它并不修改我的内容,即使我使用的是默认的苹果代码 我是否需要为应用标识启用推送通知我为通知服务扩展? 请帮助我完成工作所需的步骤。 –

回答

9

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

+0

谢谢。适用于我。奇怪的错误。 –