【问题标题】:iOS 10 How to set UNotificationContent threadIdentifier for remote notificationiOS 10 如何为远程通知设置 UNotificationContent threadIdentifier
【发布时间】:2017-01-06 10:52:13
【问题描述】:

TL;DR: 需要在 APNs 通知负载 JSON 中设置什么键来对应 UNNotificationContent 对象的 threadIdentifier 属性?例如"category" 键对应于 categoryIdentifier 属性。


iOS 10 引入了Notification Content Extension,允许我们在展开通知时呈现视图控制器。

我们提供的视图控制器符合UNNotificationContentExtension 协议,这需要我们实现didReceive(_:) 方法。

此方法的文档包括以下段落:

当您的视图控制器可见时,此方法可能会被多次调用。具体来说,当一个新的通知到达时,它会再次被调用,它的 threadIdentifier 值与已经显示的通知的线程标识符匹配。

threadIdentifier 属性可以在代码中为本地通知设置,但我不知道如何为从服务器发送到 APN 的远程通知设置它。

UNNotificationContent 文档在此处描述了该属性:http://developer.apple.com/reference/usernotifications/unnotificationcontent

以下 JSON 包含我尝试过的密钥("thread""thread-identifier"):

{
    "aps" : {
        "alert" : "Hello World!",
        "sound" : "default",
        "category" : "example-category",
        "thread" : "example-thread",
        "thread-identifier" : "example-thread-identifier"
    }
    "custom-field" : "some value",
}

我找不到任何来自 Apple 的关于如何设置的文档。有人可以帮忙吗?

【问题讨论】:

    标签: ios apple-push-notifications unnotificationrequest


    【解决方案1】:

    我从 Apple 的一位联系人那里发现,填充此属性的正确键是 "thread-id" 键。

    所以发送到 APNs 的 JSON 如下:

    {
        "aps" : {
            "alert" : "Hello World!",
            "sound" : "default",
            "category" : "example-category",
            "thread-id" : "my conversation blah blah"
        }
        "custom-field" : "some value",
    }
    

    这会填充UNNotificationContent 对象的threadIdentifier 属性,可通过notification.request.content.threadIdentifier 在通知内容扩展中访问。

    通过设置这个"thread-id"值,意味着你的内容扩展的didReceive(_:)方法会被多次调用。首先是在最初扩展通知时,然后在新通知到达时再次使用相同的"thread-id" 值。

    我假设(希望)这将在 iOS 10 正式发布后添加到官方文档中。

    【讨论】:

    • 是的,这篇文章是很久以前的了。 apns-collapse-id 不是 aps JSON 中的键。它必须在 HTTP Header 中发送
    • 它用于从 iOS 10 开始折叠通知。但我相信(不确定)“thread-id”键应该用于在 iOS 12 中对消息进行分组(新分组功能)
    • 它应该在邮递员中与fcm.googleapis.com/fcm/send 一起使用吗?我已经用字符串和数字设置了它,但它仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2016-11-28
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多