react-native-firebase/lib/modules/notifications
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
..
AndroidAction.js Implement handling of Android actions in background 2018-05-16 08:20:24 +02:00
AndroidChannel.js [notifications] Add enableLights and enableVibration to AndroidChannel 2018-03-31 15:42:14 +01:00
AndroidChannelGroup.js [notifications] Add support for android actions 2018-03-09 11:09:28 +00:00
AndroidNotification.js [notifications] Add support for Android BigPictureStyle and BigTextStyle 2018-03-27 16:15:31 +01:00
AndroidNotifications.js Add Android API to delete channel and channel group 2018-05-14 09:24:44 +02:00
AndroidRemoteInput.js fix allowedDataTypes in fromNativeAndroidRemoteInput 2018-05-01 23:45:55 +08:00
IOSNotification.js [notifications][ios] Properly define attachment options 2018-03-01 08:39:24 +00:00
Notification.js [notifications] Make title and body optional fields 2018-04-11 18:15:38 +01:00
index.js [notifications] Add jsInitialised method to improve getInitialNotification 2018-03-30 10:31:06 +01:00
types.js Implement handling of Android actions in background 2018-05-16 08:20:24 +02:00