Is there a way to prevent users from receiving push notifications after they are sent from Firebase?

I was wondering if I could send multiple push notifications to users via Firebase and then intercept them before the user sees them on the device and allow my app to selectively allow notifications.

Is it possible?

If so, which method should I use? (either from react-native-firebase

or iOS

)

+3


source to share


5 answers


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 available if the app is in the foreground. If the app is in the background, it cannot be processed as the user will receive a notification and the app will not be notified until the notification is triggered.

0


source


You can try using UNNotificationServiceExtension

(available from iOS 10

). You need to add the extension to the project ( example of adding ).

After implementing the method - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;

in your extension.

It will be called before the notification is shown to the user and you can try to take some action (ex: save some data to local).

Note that it only works with iOS 10 rich notifications

( how to implement it with Firebase ).

PS Hope this helps you!



EDIT

In Apple Documentation, write:

Cancel this method and use it to modify the UNNotificationContent object that was delivered with the notification.

UPDATE

You can try to send silent push notifications from the server, test it, and create a local notification to show to the user if needed.

0


source


FireBase notifications can be processed when the app is in foreground state. First, you have change notifications when used with Always in your controller and in your info.plist file. After you install it, you can manage notifications in your delegate or viewController.

0


source


You can send data notifications to be received in both the foreground and background. On iOS, they are processed by the delegate method application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

, after processing, you can decide whether to create a push for the user there or not.

@update

For data message (don't click) on iOS below 10

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

      

on iOS10

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

      

And then I just check if the custom fields have the data I need and use the NotificationCenter to create a local notification

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 that this works with data notification, not push notification, but in Firebase you can send both types.

0


source


Send a silent remote notification with help "content-available" : 1

and decide if you want to show it to the user or not.

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

}

      

If you want to show it to the user, select a custom key / value pair for the message and trigger a local notification for the user. It will work in the background as well. UIBackgroundMode

must be enabled for remote notifications.

0


source







All Articles