gpt4 book ai didi

iOS - 通过本地推送通知触发通知服务扩展?

转载 作者:行者123 更新时间:2023-11-29 13:52:00 30 4
gpt4 key购买 nike

Is it possible to trigger Notification Service Extension through Local Push Notification?

我的代码片段

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()

// Adding title,subtitle,body & badge
content.title = "title"
content.body = "body"
content.sound = UNNotificationSound.default

let aps = ["mutable-content": 1]
let dict = ["aps": aps, "some-key": "some-value"] as [AnyHashable : Any]
content.userInfo = dict

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content as UNNotificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

最佳答案

不可以,无法通过推送通知触发通知服务扩展。

原因: 服务扩展允许您在将远程通知传送给用户之前自定义其内容。例如解密消息或下载附件

但我个人认为在服务扩展中下载通知内容附件是没有意义的,因为你总是可以在安排之前下载附件。例如

    // Download attachment asynchronously 
downloadAttachments(data: data) { attachments in
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
// Add downloaded attachment here
content.attachments = attachments

// Adding title,subtitle,body & badge
content.title = "title"
content.body = "body"
content.sound = UNNotificationSound.default

// No need of adding mutable content here
let dict = ["some-key": "some-value"] as [AnyHashable : Any]
content.userInfo = dict

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content as UNNotificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

如果您有不同的用例,请告诉我。

关于iOS - 通过本地推送通知触发通知服务扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211578/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com