【问题标题】:Notification Service Extension Not working通知服务扩展不起作用
【发布时间】:2017-07-30 10:49:44
【问题描述】:

当我发送带有推送有效负载的mutable-content:1 时未显示通知,它也没有命中通知服务扩展内的断点,尽管没有显示可变内容推送,但通知内容扩展也工作正常。我没有修改通知服务扩展中的代码,它是 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 上的其他设备,它触发了通知服务扩展。

【问题讨论】:

  • 你如何发送包含 "mutable-content:1" 的 Json ?如果使用 FCM 将“-”更改为“_”
  • 嗨 @SeikoTheWiz 我没有使用 FCM
  • 嗨@princerk 我也在尝试实现通知服务扩展,但即使我使用默认的苹果代码,它也不会修改我的内容。我为通知服务扩展设置的 App Id 是否需要启用推送通知?请帮助我完成使其工作所需的步骤。
  • @Milan 无需为服务扩展启用推送通知。 xcode 生成的默认代码应该可以完成这项工作。确保您在推送有效负载中传递“可变内容”:1。尝试使用其他设备。
  • FWIW,您没有展示如何修改有效负载。

标签: ios notifications apple-push-notifications ios10 serviceextension


【解决方案1】:

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

【讨论】:

  • 最近我的一些客户遇到了同样的问题,重启后一切正常。你找到根本原因了吗?
【解决方案2】:

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

在我的例子中,我正在测试在 iOS 12.4 上运行的设备,而目标 我的 NotificationServiceExtension 版本是 14.1。我只是改变了我的 NotificationServiceExtension 的目标版本为 12.4 它有效。

【讨论】:

  • 天啊,这正是我遇到问题的原因。 ?
  • WEEKS 试图弄清楚为什么它在我们的 QA 流程上不起作用,这就是我的答案。谢谢!
猜你喜欢
  • 2017-02-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多