簡體   English   中英

在NotificationService中從didReceive禁用接收推送

[英]disable receive push from didReceive in NotificationService

我想在特殊情況下禁用向用戶發送推送通知。 我將第一個Push保存在userDefault中,然后在第二個中檢查是否相同。 當我調試它進入返回而不是繼續,但仍然我得到一個推送通知。 從Firebase API發送的推送

 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

    if let dict = request.content.userInfo["aps"] as? Dictionary<AnyHashable, AnyHashable> {
        if let did = dict["did"] as? String {
            if let message = dict["message"] as? String {
                let currentPush = did + "_" + message

                if let lastPush = UserDefaults.standard.string(forKey: "lastPush"), currentPush == lastPush {
                    print("enter")
                    return
                }
                else {
                    UserDefaults.standard.setValue(currentPush, forKey: "lastPush")
                }
            }
        }
    }}

與Android(開發人員可以控制是否顯示通知)不同,iOS不提供該功能。 發送的通知將始終顯示。 因此,您的return將無效。

您可以采取的一種解決方法是發送一個靜默推送通知 ,處理數據,如果數據符合您的邏輯,則創建一個本地通知並顯示該通知。

暫無
暫無

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

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