【问题标题】:iOS 10 don't call Notification Service ExtensioniOS 10 不调用通知服务扩展
【发布时间】:2017-02-01 11:08:34
【问题描述】:

我尝试实现新的通知服务扩展,但我遇到了问题。

在我的 NotificationService.swift 文件中,我有以下代码:

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)
    }
}
}

当我收到推送通知时,从未调用过 didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) 方法。

也许我误解了这个扩展的工作原理?

【问题讨论】:

  • 嗨。你解决了吗?

标签: ios swift ios10 ios-extensions unnotificationserviceextension


【解决方案1】:

在服务扩展上检查您的部署目标。

在使用 10.1 的设备上进行测试时,我将部署目标设置为 10.2,并且在我更改它之前没有调用扩展。

另一个问题可能是调试。为了使它工作,您需要附加服务扩展进程。在 Xcode 菜单 Debug > Attach To Process > Name of your extension

【讨论】:

  • 关于此问题的进一步说明:看起来 Apple 会将新扩展的部署目标默认为最新的 iOS 版本,即使您的基础应用程序以旧版本为目标。
  • 这可能是大多数情况下的正确答案。无论我正在运行哪种方案,只有正确设置部署目标才能为我工作。并且不要忘记以正确的形式推送有效载荷。 +1
  • 优秀;为我节省了大量时间!
  • 你是我的英雄。
  • 刚刚浪费了我生命中的 2 个小时来调试具有 Firebase 的新通知服务扩展的 Flutter 应用程序。我什至无法更改通知标题!原来appex没有运行,因为我的设备不是最新的iOS版本......非常感谢Kamilton。
【解决方案2】:
  1. 运行服务扩展作为目标而不是应用。然后它会询问您运行服务扩展的应用程序,然后选择您的应用程序并发送通知。

  2. 确保服务扩展的部署目标低于您物理设备的操作系统版本

  3. 确保有效负载包含“可变内容: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. 不要在aps 中添加content-available 标志,或者如果您已添加,请确保将其设置为0

---------------------------------------- 如果没有任何效果,请重新启动您的设备--------------------------------------------------

【讨论】:

  • 运行扩展作为目标是关键!我花了一段时间才弄清楚这个。感谢您指出!
  • 2.救了我。谢谢:)
  • 我的通知 { "registration_ids": ["devicetoken"], "mutable_content": true, "data": { "post_details": { "pcm_message": "asdf", "pcm_img_url": " portalvh/15683.jpg";, } }, "notification" : { "title" : "demo push", "body" : "this is push body" } } 但仍然没有调用通知服务扩展哪里有问题或者遗漏了一些信息我已经在我的整个项目中设置了部署目标 10.0
  • 我看不出省略content-available 的意义。我们发送了两个标志,它工作正常。
  • 这终于为我解决了。我看过几篇关于在运行应用程序后附加到服务扩展的帖子,但这不起作用。这就像一个魅力!
【解决方案3】:

我快疯了。最后我意识到我有Notification Service Extensiondeployment target10.3(我的手机也是)。我改成10.2,效果很好

【讨论】:

    【解决方案4】:

    如果一切都失败了,请重新启动您的设备。我只花了 2 个小时就解决了这个问题,只需重新启动手机即可修复它。

    【讨论】:

    • 您先生,值得我点赞,谢谢!显然,有必要终止以前使用错误部署目标安装的服务扩展。
    • 标记我也花了 2 小时。
    【解决方案5】:

    您的推送通知负载should contain the "mutable-content" : 1 key value pair

    远程通知的 aps 字典包含值设置为 1 的 mutable-content 键。

    推送通知负载 JSON 示例:

    {
      "aps":{
              "alert": {
                        "body": "My Push Notification", 
                        "title" : "Notification title"},
              "mutable-content" : 1,
              "badge":0},
    }
    

    这工作得很好,我收到推送通知如下:

    还要注意:

    您无法修改静音通知或仅播放声音或标记应用图标的通知。

    您可以尝试PusherHouston 来测试推送通知。

    【讨论】:

    • 在有效负载中,我看到了 mutable-content:1。我在警报对象和 apns 对象中也尝试了这个值,但它们都不适合我
    • 漂亮的圣诞树!
    【解决方案6】:

    我有同样的问题,但我的问题是通知扩展是“应用程序”。应该是appex

    【讨论】:

    • 这个。我的项目有多个目标,我使用 .xcconfig 文件在所有类似目标之间共享构建设置。从模板创建服务扩展不会将 Wrapper Extension 设置为 appex,因此它会从项目中获取 app 扩展。
    【解决方案7】:

    来自docs on UNNotificationServiceExtension class

    • 远程通知配置为显示alert

    • 远程通知的 aps 字典包含 mutable-content 键,其值设置为 1

    您无法修改静音通知或仅播放声音或标记应用图标的通知。

    基本上

    必须包括:

    • mutable-content: 1
    • alert 字典。

    不得包括:

    • content-available: 1

    总而言之,Apple 正在尽最大努力不允许应用更改静默通知。它希望仅在用户通知(面向用户的通知)上允许这样做

    【讨论】:

      【解决方案8】:

      您需要将"mutable-content": 1 添加到您的有效负载中

      【讨论】:

        【解决方案9】:

        这是一个示例,如果您使用 SwiftFirebase cloud functions (Node.js) 来实现发送通知具有通知服务扩展的附件。 下面我只提到了一些重点。

        有效负载样本

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

        mutable_content: "true" 这部分很重要,我花了很多时间来找到确切的命名,现在大多数文档都无效了。

        下一个主要问题是我们必须运行应用程序的方式

        使用您的设备选择通知服务扩展

        运行后会弹出选择主项目的弹出窗口

        这样当您收到通知时,它会调用 NotificationService didReceive 函数(位于 Notification Service Extension 中)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-08-03
          • 1970-01-01
          • 1970-01-01
          • 2020-02-20
          • 2017-02-23
          • 1970-01-01
          • 2022-10-07
          相关资源
          最近更新 更多