4

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.

Is this possible?

If so, which method do I need to use? (either from react-native-firebase or iOS)

Michael Campsall
  • 4,325
  • 11
  • 37
  • 52
  • Another option would be creating a new "Audience" in Firebase, rather than selectively blocking the push notifications after they are sent. Then, when you want to send `this_push_notification` to a specific set of users, you could only send it to the group of users with `receive_this_push_notification = true`. That way you only deliver the notification to the users who should receive it in the first place (the filtering happens on the Firebase side). Audiences can be created based on specific custom events that the user has undergone in the app, and also on number of demographic options. – Iron John Bonney Jun 29 '17 at 15:49
  • Unfortunately, the number of possible audience combinations we would need is in the tens of thousands – Michael Campsall Jun 30 '17 at 08:01

5 Answers5

1

You can try to use UNNotificationServiceExtension (available from 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; 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).

P.S. Hope it help for you!

EDIT:

In Apple documentation write:

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

UPDATE:

You can try to send from server silent push notification, check it and create local notification to show to user if need it.

S. Matsepura
  • 1,673
  • 1
  • 17
  • 24
  • Do you know if it is possible to ignore notifications this way? – Michael Campsall Jun 27 '17 at 18:07
  • @MichaelCampsall, I edit my answer, just look for it. – S. Matsepura Jun 28 '17 at 08:11
  • "UNNotificationContent: The uneditable content of a local or remote notification." from the Apple documentation. Also, in the docs: "You may also modify the alert text as long as you do not remove it. If the content object does not contain any alert text, the system ignores your modifications and delivers the original notification content." – Michael Campsall Jun 29 '17 at 21:10
  • You can send from server silent push notification, check it and create local notification to show to user if need it. – S. Matsepura Jun 30 '17 at 08:28
0

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. 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.

  • I wouldn't recommend doing this - many things can prevent the delivery of silent notifications. For example, low battery mode, weak cell signal, and more. The documentation explains that these notifications are only processed when iOS decides it is a good idea to do so, for example, when plugged in, or when on a strong wifi or cellular signal. – Jordan Smith Jul 03 '17 at 02:02
0

FireBase notifications can be handled when an application is in foreground state . Firstly you have change notifications to when in use from Always in both your controller and info.plist. Once you have set it you can control notifications in your App delegate or viewController.

Fahad Malik
  • 51
  • 11
0

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.

@update

For receiving data message (not push) on iOS lower than 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 custom fields have data I need and use NotificationCenter to create 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 this works with data notification not push notification, but in Firebase you are able to send both types.

0

Send a silent remote notification with "content-available" : 1 and decide if you want to show it to user or not.

{
    "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.

Bilawal Baig
  • 332
  • 1
  • 6