This function serves as my listener for handling firebase push notifications. The desired behavior is to intercept push notifications received in the background state and display them as local notifications instead of showing them on the device. I have successfully implemented this functionality for foreground state notifications, but I am facing an issue with setBackgroundMessageHandler
where two notifications are displayed and the app is reopened, which is not the desired outcome.
I am specifically working with topics notifications.
export const NotificationListener = navigation => {
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state:',
remoteMessage?.notification,
);
});
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage?.notification,
);
}
});
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Handling message in the background!', remoteMessage);
// let title = remoteMessage?.notification?.title;
// let body = remoteMessage?.notification?.body;
// PushNotification.localNotification({
// channelId: 'burque_channel_id',
// title: title,
// message: body,
// });
});
messaging().onMessage(async remoteMessage => {
console.log('notifications on foreground state..', remoteMessage);
let title = remoteMessage?.notification?.title;
let body = remoteMessage?.notification?.body;
PushNotification.localNotification({
channelId: 'burque_channel_id',
title: title,
message: body,
});
});
};