I have an app with react-native-firebase notification, the message is sucessfully received and the notification is successfully created and displayed on Android Nougat or older, on Android Oreo or later I have adding channelId it works well when using react-native run-android, but when using bundle (cd android && ./gradlew installRelease) my notification is not displayed only in foreground on Android Oreo or later, on background works. Didn't know the problem is on message listener or when creating notification. Here is my code...
I call function below after having permissions, to listening message when the app is in foreground
export const listening_message = () => {
return firebase.notifications().onNotification((notification) => {
display_notification({
notification_param: {
sound: 'default',
show_in_foreground: true,
},
title: notification.title,
subtitle: notification.subtitle,
body: notification.body,
data: notification.data,
});
});
}
then this is the code to display notification
async function display_notification(parameter) {
let realmUser = await Realm.open(RealmData.User);
if(realmUser.objects(RealmDataName.User).length > 0) {
let realmNotification = await Realm.open(RealmData.NotificationID);
if(realmNotification.objects(RealmDataName.NotificationID).length == 0) {
realmNotification.write(() => {
realmNotification.create(RealmDataName.NotificationID, {
id: 0,
})
});
}
if(realmNotification.objects(RealmDataName.NotificationID).length == 0) {
realmNotification.write(() => realmNotification.create(RealmDataName.NotificationID, {id: 0}));
}
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Channel Name',
firebase.notifications.Android.Importance.High
).setDescription('Description of the channel');
firebase.notifications().android.createChannel(channel);
let new_notification = new firebase.notifications.Notification(parameter.notification_param)
.setNotificationId(realmNotification.objects(RealmDataName.NotificationID)[0].id.toString())
.setTitle(parameter.title)
.setSubtitle(parameter.subtitle)
.setBody(parameter.body)
.setData({
id: realmNotification.objects(RealmDataName.NotificationID)[0].id.toString(),
...parameter.data,
});
if(Platform.OS == "android") {
new_notification.android.setVibrate(1000)
.android.setSmallIcon('ic_launcher')
.android.setColor('#000000');
if(Platform.Version <= 25) {
new_notification.android.setPriority(firebase.notifications.Android.Priority.High);
} else {
new_notification.android.setChannelId("channelId");
}
} else if(Platform.OS == "ios") {
new_notification.ios.setBadge(1);
}
firebase.notifications().displayNotification(new_notification);
realmNotification.write(() => realmNotification.objects(RealmDataName.NotificationID)[0].id++);
}
}