I\'ve used react-native-fcm
for remote notification in android and iPhone.
react-native-fcm
In Android foreground I\'m not be able to getting re
As per the library issues listed here you can try two things:
just pass show_in_foreground
in your data
property in remote notification
android shows notification only when app state is killed or background. To display notifications in app foreground, you need to show local notification
.
Sample code:
FCM.on(FCMEvent.Notification, notif => {
if (!notif.opened_from_tray) {
showLocalNotification();
}
});
showLocalNotification() {
FCM.presentLocalNotification({
id: new Date().valueOf().toString(), // (optional for instant notification)
title: "Test Notification with action", // as FCM payload
body: "Force touch to reply", // as FCM payload (required)
show_in_foreground: true // notification when app is in foreground (local & remote)
});
}
Full code is here
According to the official Github of react-native-fcm, this library is depreciated. You can use the react-native-firebase for generating notification. I was able to get the notifications working in about 2 hours for android. If you want the code I can share it. good luck.
Update - Sorry I couldn't answer earlier because of my office account.
This is my code for showing android foreground notifications.
firebase.messaging()
.subscribeToTopic(this.state.user.user_name)
.then(response => console.log('response from FCM TOPIC' + response))
.catch(error => console.log('error from FCM TOPIC'+ error));
this.notificationListener = firebase.notifications().onNotification(notification => {
let notificationMessage = notification._android._notification._data.action;
let recordId = notification._android._notification._data.recordID;
let { title, body } = notification;
// console.log('ttttt', notification)
// notification.android.setAutoCancel(false)
console.log(title, body, notificationMessage, recordId);
const channelId = new firebase.notifications.Android.Channel(
'Default',
'Default',
firebase.notifications.Android.Importance.High
);
firebase.notifications().android.createChannel(channelId);
let notification_to_be_displayed = new firebase.notifications.Notification({
data: notification._android._notification._data,
sound: 'default',
show_in_foreground: true,
title: notification.title,
body: notification.body,
});
if (Platform.OS == 'android') {
notification_to_be_displayed.android
.setPriority(firebase.notifications.Android.Priority.High)
.android.setChannelId('Default')
.android.setVibrate(1000);
}
console.log('FOREGROUND NOTIFICATION LISTENER: \n', notification_to_be_displayed);
firebase.notifications().displayNotification(notification_to_be_displayed);
});
Which API level you are testing on ? Android API 26 and above requires channels to be created in order to receive notifications in foreground. please read this for more information.
react-native-fcm
is also updated to include channels too, refer this
though the library should not be used anymore as the library is not maintained anymore, a good alternative is react-native-firebase.