To add to what @Donut has answered, things have changed a bit now.
AndroidManifest.xml (remains unchanged as per @Donut)
<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"/>
It is recommended that you add a channel-id here (i.e. app-local-notifications)
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
Your backgroundMessaging.js should now be
import firebase from 'react-native-firebase'
const channelId = 'app-local-notifications'
export const backgroundMessageListener = async (message) => {
const channel = new firebase.notifications.Android.Channel(
channelId,
'Local Interactive Notifications',
firebase.notifications.Android.Importance.Max
).setDescription('Local Interactive Notifications');
firebase.notifications().android.createChannel(channel);
const localNotification = new firebase.notifications.Notification({
sound: 'default' //important
})
.setNotificationId(message.messageId)
.setTitle(message.data.title)
.setBody(message.data.body)
.android.setChannelId(channelId) //important
.android.setSmallIcon('ic_launcher')
.android.setPriority(firebase.notifications.Android.Priority.High); //important
const action = new firebase.notifications.Android.Action('reply', 'ic_launcher', 'Reply')
action.setShowUserInterface(false)
const remoteInput = new firebase.notifications.Android.RemoteInput("inputText")
remoteInput.setLabel('Message')
action.addRemoteInput(remoteInput)
localNotification.android.addAction(action)
firebase.notifications().displayNotification(localNotification).catch(err => console.log(err)); //important
}
export const backgroundActionHandler = async (notificationOpen) => {
if (notificationOpen && notificationOpen.notification) {
const action = notificationOpen.action;
const notificationId = notificationOpen.notification.notificationId;
if (action === "reply") {
console.log(notificationOpen)
} else {
console.log("unsupported action", action);
}
// hide the notification instead of Promise.resolve()
firebase.notifications().removeDeliveredNotification(notificationId); //important
}
};
Things to note:
1. Notification sent via FCM should be data-only
notifications.
2. Notification priority should be "high".
3. Notification action must not take longer than 60s.
4. ChannelID is required.
5. Notification sound should be default.