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>扩展名
Sud*_*pta 15
而不是运行应用程序.您必须将服务扩展名作为目标运行.然后它将询问您运行服务扩展的应用程序,然后选择您的应用程序,它将发送通知.
确保部署目标小于服务扩展中的设备OS.
请确保有效载荷应包含"可变内容":1.
Run Code Online (Sandbox Code Playgroud){"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" }
不要在aps中添加"Content-available"标志,如果需要添加make it 0."Content-available":0,
cro*_*dor 10
我快疯了。最后,我意识到,我有deployment target的Notification Service Extension是10.3(我的手机也是如此)。我改变了10.2,它完美地工作
如果一切都失败了,请重新启动您的设备。我只花了 2 个小时就解决了这个问题,只需重新启动手机即可解决。
您的推送通知有效负载应包含“mutable-content”:1 个键值对。
\n\n\n\n\nRemote notification\xe2\x80\x99s aps 字典包含 mutable-content 键,其值设置为 1。
\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}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n另请注意:
\n\n\n\n\n\n您无法修改无声通知或仅播放声音或标记 app\xe2\x80\x99s 图标的通知。
\n
| 归档时间: |
|
| 查看次数: |
9459 次 |
| 最近记录: |