简体   繁体   English

在NotificationService中从didReceive禁用接收推送

[英]disable receive push from didReceive in NotificationService

I want to disable send push notification to user under special conditions. 我想在特殊情况下禁用向用户发送推送通知。 I saved my first Push in userDefault and in the second I check if it's the same. 我将第一个Push保存在userDefault中,然后在第二个中检查是否相同。 when i debug it enter to return and not continue, but still i get a push notification. 当我调试它进入返回而不是继续,但仍然我得到一个推送通知。 the push sent from firebase api 从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")
                }
            }
        }
    }}

Unlike Android where the developer has some control whether to show the notification or not, iOS does not provide that facility. 与Android(开发人员可以控制是否显示通知)不同,iOS不提供该功能。 A notification sent will always be shown. 发送的通知将始终显示。 So, your return won't work. 因此,您的return将无效。

One work around that you can do is send a silent push notification , process the data and if it fits your logic, then create a local notification and show that. 您可以采取的一种解决方法是发送一个静默推送通知 ,处理数据,如果数据符合您的逻辑,则创建一个本地通知并显示该通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 未调用推送通知服务扩展 (didReceive) - iOS Push notification service extension (didReceive) is not called 如何从sup(2.1.3)接收推送通知 - How to receive Push notification from sup(2.1.3) 即使用户拒绝了应用程序接收推送通知,应用程序也可以从推送服务器接收令牌吗? - Can an app receive a token from a push server even if the user has declined the app to receive push notifications? 无法从 WKUserContentController didReceive WKScriptMessage 获取字符串数据 - Can't get string data from WKUserContentController didReceive WKScriptMessage 如何启用/禁用应用程序的推送通知? - How to enable/disable push notification from the app? 无法从Firebase 3.2.0主题接收iOS上的推送通知 - Cannot receive push notification on iOS from Firebase 3.2.0 topics 需要打开以接收来自APNS的推送通知的端口号 - Ports number needed to open to receive push notification from APNS 你能收到来自 iOS 模拟器的 FCM 推送通知吗? - Can you receive FCM push notification from iOS simulator? 禁用UISlider滑动,但仍允许它接收UIControlEventTouchDown - Disable UISlider from sliding, but still allow it to receive UIControlEventTouchDown iOS无法在Cordova项目(2018)上收到来自FCM的推送通知 - iOS cannot receive push notification from FCM on Cordova project (2018)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM