简体   繁体   English

有没有办法阻止用户在从Firebase发送后收到推送通知?

[英]Is there a way to prevent users from receiving push notifications after they have been sent from Firebase?

I was wondering if i can send multiple push notifications to users through Firebase, but then intercept them before the user sees them on the device and allow my app to selectively allow notifications. 我想知道我是否可以通过Firebase向用户发送多个推送通知,但在用户在设备上看到它们之前拦截它们并允许我的应用选择性地允许通知。

Is this possible? 这可能吗?

If so, which method do I need to use? 如果是这样,我需要使用哪种方法? (either from react-native-firebase or iOS ) (来自react-native-firebaseiOS

new

You can send silent notification to users and schedule your Actual notification as local notification 您可以向用户发送静默通知,并将实际通知安排为本地通知

Refer This 请参阅

old

(In Case of Firebase) Notifications are only manageable in the case when the app is in foreground. (在Firebase的情况下)通知仅在应用程序位于前台时才可管理。 If the app is in background it can't be handled as user will receive notification and app will not be notified until notification is tapped. 如果应用程序处于后台,则无法处理,因为用户将收到通知,并且在通知被点击之前不会通知应用程序。

You can try to use UNNotificationServiceExtension (available from iOS 10 ). 您可以尝试使用UNNotificationServiceExtension (可从iOS 10 )。 You need to add an extension to you project ( example how to add ). 您需要为项目添加扩展( 示例如何添加 )。

After implement method - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler; 实现方法之后- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler; in your extension. 在你的扩展名中。

It will be called before notification shows to user and you can try to do some action (for example: save some data to local). 它会在通知显示给用户之前调用,您可以尝试执行某些操作(例如:将一些数据保存到本地)。

Note that it works only with iOS 10 rich notifications ( how to implement it with Firebase ). 请注意 ,它仅适用于iOS 10 rich notifications如何使用Firebase实现它 )。

PS Hope it help for you! PS希望对你有所帮助!

EDIT : 编辑

In Apple documentation write: Apple文档中写道:

Override this method and use it to modify the UNNotificationContent object that was delivered with the notification. 重写此方法并使用它来修改随通知一起提供的UNNotificationContent对象。

UPDATE : 更新

You can try to send from server silent push notification, check it and create local notification to show to user if need it. 您可以尝试从服务器静默推送通知发送,检查并创建本地通知,以便在需要时向用户显示。

FireBase notifications can be handled when an application is in foreground state . 当应用程序处于前台状态时,可以处理FireBase通知。 Firstly you have change notifications to when in use from Always in both your controller and info.plist. 首先,您在控制器和info.plist中都有来自Always的更改通知。 Once you have set it you can control notifications in your App delegate or viewController. 设置后,您可以在App委托或viewController中控制通知。

You can send data notifications that will be received both in fore- and background. 您可以发送将在前台和后台接收的数据通知。 In iOS they are processed by the delegate method application(_:didReceiveRemoteNotification:fetchCompletionHandler:) , after processing you can decide there to create a push for the user or not. 在iOS中,它们由委托方法application(_:didReceiveRemoteNotification:fetchCompletionHandler:)处理,在处理之后,您可以决定是否为用户创建推送。

@update @Update

For receiving data message (not push) on iOS lower than 10 用于在低于10的iOS上接收数据消息(不推送)

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      handleNotification(data: userInfo)
      completionHandler(UIBackgroundFetchResult.newData)
}

on iOS10 在iOS10上

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
      handleNotification(data: remoteMessage.appData)
}

And then I just check if custom fields have data I need and use NotificationCenter to create local notification 然后我只检查自定义字段是否包含我需要的数据,并使用NotificationCenter创建本地通知

fileprivate func handleNotification(data: [AnyHashable : Any]) {
      guard let topic = data["topic"] as? String else { return }
      if data["receiver"] as? String == UserManager.shared.current(Profile.self)?.uuid && topic  == "coupons" {
        displayNotification(data: data)
      }
}

fileprivate func displayNotification(data: [AnyHashable : Any]) {
        if #available(iOS 10.0, *) {        
            let content = UNMutableNotificationContent()
            content.title = data["notification_title"] as! String
            content.body = data["notification_body"] as! String

            let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
            let request = UNNotificationRequest.init(identifier: data["topic"] as! String, content: content, trigger: trigger)

            UNUserNotificationCenter.current().add(request)
        } else {
            if currentNotification != nil {
                UIApplication.shared.cancelLocalNotification(currentNotification!)
            }

            let notification = UILocalNotification()
            notification.fireDate = Date()
            notification.category = data["topic"] as? String
            notification.alertBody = data["notification_body"] as? String
            if #available(iOS 8.2, *) {
                notification.alertTitle = data["notification_title"] as? String
            }
            currentNotification = notification
            UIApplication.shared.scheduleLocalNotification(currentNotification!)
        }
}

Just remember this works with data notification not push notification, but in Firebase you are able to send both types. 请记住,这适用于数据通知而非推送通知,但在Firebase中,您可以发送这两种类型。

Send a silent remote notification with "content-available" : 1 and decide if you want to show it to user or not. 发送带有"content-available" : 1的静音远程通知"content-available" : 1并决定是否要将其显示给用户。

{
    "aps" = {
        "content-available" : 1,
        "sound" : ""
    };
    // add custom key value for message

}

If you want to show it to user, fetch custom key-value pair for message and fire a local notification for user to be shown. 如果要将其显示给用户,请为消息提取自定义键值对并触发本地通知以供用户显示。 It will work in the background as well. 它也可以在后台运行。 UIBackgroundMode needs to enabled for Remote Notifications. 需要为远程通知启用UIBackgroundMode

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM