I am using @react-native-firebase for handling push notification. Problem is that It is working when the App is in the background. If the App is in the foreground or open state, Push notification not working. Also, I am using notifee to create a custom notification. It also not working.
here are the version details.
"@react-native-firebase/app": "^8.3.1",
"@react-native-firebase/messaging": "^7.7.2",
"@notifee/react-native": "^0.12.3",
Implemented Code
componentDidMount() {
console.log('componentDidMount App ');
// AppState.addEventListener('change', this._handleAppStateChange);
messaging()
.getInitialNotification()
.then(remoteMessage => {
console.log('Notification caused app to open from quit state:');
if (remoteMessage) {
console.log(remoteMessage.notification);
}
});
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!');
console.log(remoteMessage);
});
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!');
console.log(remoteMessage);
});
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('Notification caused app to open from background state:');
console.log(remoteMessage);
Actions.AddMeeting({
actionType: 'add',
});
// navigation.navigate(remoteMessage.data.type);
// });
messaging().registerDeviceForRemoteMessages();
notifee.registerForegroundService(() => {
return new Promise(resolve => {
// Long running task...
});
});
const channelId = notifee.createChannel({
id: 'default',
name: 'Default Channel',
});
async function onMessageReceived(message) {
// Do something
// notifee.displayNotification(message.data.msg);
console.log('onMessageReceived');
console.log(JSON.stringify(message));
notifee.displayNotification({
title: 'Foreground service',
body: 'This notification will exist for the lifetime of the service runner',
android: {
channelId,
asForegroundService: true,
color: '#dc193c',
colorized: true,
},
});
}
messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);
}