iOS 10不会调用Notification Service Extension

jus*_*ust 14 ios swift ios-extensions ios10

我试图实现新的Notification Service Extension,但我遇到了问题.

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

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

也许我误解了这个扩展是如何工作的?

小智 27

检查服务扩展上的部署目标.

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

另一个问题可能是调试..为了使其工作您需要附加服务扩展流程.在Xcode菜单中,Debug> Attach To Process>扩展名

  • 对此的进一步说明:看起来即使您的基本应用程序针对旧版本,Apple 也会将新扩展的部署目标默认为最新的 iOS 版本。 (3认同)

Sud*_*pta 15

  1. 而不是运行应用程序.您必须将服务扩展名作为目标运行.然后它将询问您运行服务扩展的应用程序,然后选择您的应用程序,它将发送通知.

  2. 确保部署目标小于服务扩展中的设备OS.

  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"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 不要在aps中添加"Content-available"标志,如果需要添加make it 0."Content-available":0,

  • 我不认为省略“内容可用”的意义。我们发送两个标志并且它工作正常。 (4认同)
  • 将扩展作为目标运行是关键!我花了一段时间才弄清楚这一点。谢谢你指出! (3认同)
  • 2.救我。谢谢 :) (2认同)

cro*_*dor 10

我快疯了。最后,我意识到,我有deployment targetNotification Service Extension10.3(我的手机也是如此)。我改变了10.2,它完美地工作


cho*_*lot 6

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

  • 先生,值得我点赞,谢谢!显然,有必要终止先前使用错误的部署目标安装的服务扩展。 (2认同)

Raj*_*ram 4

您的推送通知有效负载应包含“mutable-content”:1 个键值对

\n\n
\n

Remote notification\xe2\x80\x99s aps 字典包含 mutable-content 键,其值设置为 1。

\n
\n\n

推送通知负载 JSON 的示例:

\n\n
{\n  "aps":{\n          "alert": {\n                    "body": "My Push Notification", \n                    "title" : "Notification title"},\n          "mutable-content" : 1,\n          "badge":0},\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

另请注意:

\n\n
\n

您无法修改无声通知或仅播放声音或标记 app\xe2\x80\x99s 图标的通知。

\n
\n\n

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

\n

  • 在有效负载中,我看到可变内容:1。我也在警报对象和 apns 对象中尝试了这个值,但它们都不适合我 (2认同)