react-native-firebase/lib/modules
Dariusz Luksza 17f7f39dac Implement handling of Android actions in background
There are some cases when local notification action should be handled in
background eg. snoozing the reminder. In case of it launching app UI is
not necessary and would be confusing for the end user.

Therefore there should be a way to handle local notification action in
background.

For this reason new property 'runInBackground' was added to the
AndroidAction class and TypeScript type.

Also new broadcast receiver and service were implemented to handle
properly background actions.

In order to run particular action in background API consumer need to set its
'runInBackground' property to 'true', eg:

  ...
  .android.addAction(new firebase.notifications.Android.Action("snooze",
  "ic_snooze", "Snooze").setRunInBackground(true))
  ...

Then, there are two cases that API consumer needs to handle.

First when app is in the foreground, standard notification and
notification action code path will be executed. This mean, that:
 * onNotification() listener will be called (which should call
 displayNotification(), in order to show it to the user),
 * onNotificationOpen() listener will be called after the action is
 tapped by the user

Secondly, when application is in background or it is not running new
'RNFirebaseBackgroundNotificationAction' handler will be called. To
properly handle this case API consumer should create a background
asynchronous handler:

  const handleAsyncTask = async (notificationOpen: NotifficationOpen) => {
    if (notificationOpen && notificationOpen.notification) {
      const action = notificationOpen.action;
      const notificationId = notificationOpen.notification.notificationId;
      if (action === "snooze") {
        console.log("Reschedule notification for later time", notificationId);
      } else {
        console.log("unsupported action", action);
      }
      // hide the notification
      firebase.notifications().removeDeliveredNotification(notificationId);
    }
  }

Next hander should be registered to headless handler:

  AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => handleAsyncTask);

Finally AndroidManifest.xml file must be modified, to include receiver
and service definition:

  <receiver
      android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver"
      android:exported="true">
    <intent-filter>
      <action android:name="io.invertase.firebase.notifications.BackgroundAction"/>
    </intent-filter>
  </receiver>
  <service android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionsService"/>

Now when ever 'Snooze' action is pressed it will launch
'handleAsyncTask' function in the background or onNotificationOpen()
when app is in foreground. And reschedule the notification
for the later time.
2018-05-16 08:20:24 +02:00
..
admob Implement multiple database shard support 2018-03-21 12:51:02 -07:00
analytics Merge pull request #881 from pocketgems/multiple_databases_3.3.0_upstream 2018-03-23 08:35:01 +00:00
auth [js][auth] add signInWithEmailLink method + tweak isSignInWithEmailLink 2018-05-13 00:09:00 +01:00
config Implement multiple database shard support 2018-03-21 12:51:02 -07:00
core [core] add functions module to core + types 2018-05-05 19:25:52 +01:00
crash [crashlytics] Move to top level crashlytics namespace 2018-04-03 16:51:05 +01:00
crashlytics [crashlytics] Move to top level crashlytics namespace 2018-04-03 16:51:05 +01:00
database [database][js] fixed a regression where snapshot.child() would no longer work on array values 2018-04-22 00:29:33 +01:00
firestore [firestore][android] Add support for firestore v16 2018-05-08 15:36:49 +01:00
functions [js][functions] move errorOrResult fn outside class 2018-05-07 19:32:20 +01:00
iid [iid] Rename instanceid to iid 2018-03-23 14:59:00 +00:00
invites [invites] Add jsInitialised method to improve getInitialInvitation 2018-03-30 10:07:23 +01:00
links [links] Correct platform OS name 2018-03-30 09:52:37 +01:00
messaging feat(flow): prefer string over String 2018-04-19 12:01:40 -03:00
notifications Implement handling of Android actions in background 2018-05-16 08:20:24 +02:00
perf Implement multiple database shard support 2018-03-21 12:51:02 -07:00
storage Implement multiple database shard support 2018-03-21 12:51:02 -07:00
utils Implement multiple database shard support 2018-03-21 12:51:02 -07:00