通知服务扩展不工作 [英] Notification Service Extension Not working
问题描述
当我发送带有推送负载的 mutable-content:1
时没有显示通知,它也没有到达通知服务扩展中的断点,虽然没有显示可变内容推送,但通知内容扩展工作正常.我没有修改通知服务扩展中的代码,它是 Xcode 生成的默认值.我在创建通知服务扩展时遗漏了什么,或者可能是设备设置的问题?几天前我在同一台设备上进行了测试,通知服务扩展工作正常.
Notification is not being displayed when I send mutable-content:1
with push payload neither it hits the breakpoint inside the Notification service extension, although without mutable-content push is being displayed, also Notification content extension is working fine. I did not modify the code inside the Notification service extension it's the default generated by the Xcode. Am I missing something while creating a notification service extension or it might be the issue with the device setting? I have tested on the same device a few days ago and the notification service extension was working fine.
下面是我的服务扩展代码
Below is my code for service extension
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:下面是我的推送负载
edit 1: below is my push payload
{
"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 上的其他设备,它触发了通知服务扩展.
edit1: The problem seems on that particular device running on 10.2.1, I checked with other device running on 10.2, it was triggering the Notification service extension.
推荐答案
最后我能够解决这个问题.我不确定是什么导致了问题,但经过一些实验,我意识到不仅我的服务扩展,而且所有其他已安装应用程序的服务扩展都不起作用,并且在发送带有可变内容:1"的推送有效负载时,未显示通知.重新启动设备后,它开始正常工作.我使用的是 iPhone 6s.
Finally I was able to resolve the issue. I am not sure what was causing the issue but after some experiments I realised that not only my service extension but all other installed app's service extension was not working and when sending the push payload with "mutable-content: 1, Notification was not displayed. After rebooting the device it started working fine. I was using iPhone 6s.
这篇关于通知服务扩展不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!