5

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++);
}
}
Reynald Prabha
  • 53
  • 1
  • 10
  • i am getting notification in android 8 and later in foreground and background with HeadUp. my problem is different...notfication came but i am not getting HeadUp notification in android 7 and lower in background. in foreground it's working properly in Android 5,6,7,8,9 with Headup Notification can you please help how to get HedUp Notification in Android 7 and lower?? – Yash Vaghela Apr 19 '19 at 06:54
  • 1
    Could it be related with this? https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted/50834600#50834600 Cleartext http traffic – IvanF. Aug 30 '19 at 07:45

0 Answers0