【问题标题】:iOS - Trigger Notification Service Extension through Local Push Notification?iOS - 通过本地推送通知触发通知服务扩展?
【发布时间】:2019-12-06 10:43:41
【问题描述】:

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

我的代码sn-p

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)

【问题讨论】:

  • 您是否对此进行了测试以查看是否可行,或者您只是先在这里询问?
  • 是的,测试了这个以及远程推送,它只适用于远程推送。

标签: ios swift push-notification extension-methods local


【解决方案1】:

不,不能通过推送通知触发通知服务扩展。

原因: Service extension lets you customize the content of a remote notification before it is delivered to the user. e.g decrypt the message or download the attachments

但我个人认为在服务扩展中下载通知内容附件没有意义,因为您始终可以在调度之前下载附件。例如

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

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

【讨论】:

    【解决方案2】:

    请在发帖前阅读documentation。第一行声明:

    UNNotificationServiceExtension 对象提供 Notification Service 应用扩展的入口点,让您可以在将远程通知的内容传递给用户之前对其进行自定义。

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多