簡體   English   中英

iOS - 通過本地推送通知觸發通知服務擴展?

[英]iOS - 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)

否,無法通過推送通知觸發通知服務擴展。

原因: Service extension lets you customize the content of a remote notification before it is delivered to the user. eg decrypt the message or download the attachments Service extension lets you customize the content of a remote notification before it is delivered to the user. eg 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)
    }

如果您有不同的用例,請告訴我。

請在發布前閱讀文檔 第一行指出:

UNNotificationServiceExtension 對象為 Notification Service 應用程序擴展提供入口點,它允許您在將遠程通知傳送給用戶之前自定義它的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM