首页 > 解决方案 > 在 NotificationService 中禁用从 didReceive 接收推送

问题描述

我想在特殊情况下禁用向用户发送推送通知。我将我的第一个 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")
                }
            }
        }
    }}

标签: iosfirebasepush-notificationswift4

解决方案


与开发人员可以控制是否显示通知的 Android 不同,iOS 不提供该功能。将始终显示发送的通知。所以,你的return行不通。

您可以做的一种解决方法是发送静默推送通知,处理数据,如果它符合您的逻辑,然后创建一个本地通知并显示它。


推荐阅读