ios - 在 NotificationService 中禁用从 didReceive 接收推送

标签 ios firebase push-notification swift4

我想禁用在特殊情况下向用户发送推送通知。 我在 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 不同,开发者有 some control是否显示通知,iOS 不提供该功能。发送的通知将始终显示。因此,您的 return 将不起作用。

您可以做的一个解决方法是发送 silent push notification ,处理数据,如果它符合您的逻辑,则创建本地通知并显示该通知。

关于ios - 在 NotificationService 中禁用从 didReceive 接收推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100030/

相关文章:

javascript - 带时间戳的 Firestore 查询

java - 如何向特定用户发送推送通知 firebase

ios - 无法在 Iphone 应用程序的搜索按钮单击时隐藏键盘

javascript - 在 Firebase 函数 CLI 中模拟用户创建

ios - bundle 标识符不同于供应配置文件。

Angularfire2 显示 "Missing permissions"错误,而直接运行 firebase 没有问题

push-notification - Google 日历推送通知 - 发送到源本身的通知

ios - iOS 7 和 iOS 8 中的推送通知有什么区别?

ios - 防止页脚与 UITableView 中的 tableViewCell 重叠 - Swift

iphone - Objective-C : don´t lock iPhone while app is running