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

iOS-通过本地推送通知触发通知服务扩展?,ios,swift,push-notification,extension-methods,local,Ios,Swift,Push Notification,Extension Methods,Local,是否可以通过本地推送通知触发通知服务扩展? 我的代码片段 UNUserNotificationCenter.current().removeAllPendingNotificationRequests() let content = UNMutableNotificationContent() // Adding title,subtitle,body & badge content.title = "title" content.body = "body" content.sound

是否可以通过本地推送通知触发通知服务扩展?

我的代码片段

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)
请在发帖前先阅读。第一行说明:

UNNotificationServiceExtension对象为Notification Service应用程序扩展提供入口点,通过该入口点,您可以在将远程通知交付给用户之前自定义该通知的内容


否,无法通过推送通知触发通知服务扩展

原因:
服务扩展允许您在将远程通知发送给用户之前自定义其内容。e、 g解密邮件或下载附件

但我个人认为,在service extension中下载通知内容附件没有任何意义,因为您可以在计划之前下载附件。e、 g

    // 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)
    }

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

您是否测试过此功能以确定其是否可行,或者您只是在这里先询问?是的,测试了此功能以及远程推送功能,它仅适用于远程推送功能。