通知服务扩展无法正常工作 [英] Notificaton Service Extension Not working
问题描述
通知,它也没有到达Notification Service扩展内部的断点,尽管没有显示可变内容推送,但Notification content扩展也运行良好.我没有修改Notification服务扩展中的代码,它是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 modified the code inside Notification service extension its the default generated by the Xcode. Am I missing something while creating notification service extension or it might be the issue with the device setting. I have tested on the same device few days ago and 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)
}
}
}
以下是我的推送负载
{
"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上的其他设备,它正在触发Notification Service扩展.
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屋!