【发布时间】: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