React-native-firebase: unable to receive push notification in foreground react native

Created on 5 Jul 2019  路  12Comments  路  Source: invertase/react-native-firebase

I'm trying to enable google firebase push notification with react-native fire-base. I'm able to receive push notification when an app is in the background but foreground notifications are not working. Also, how can I make push notification get displayed like other notifications such as Whatsapp notification which shows dropdown alert to users.

I tried using this Push notification not showing in Android foreground but still unable to set up notifications in foreground.

MainFest.xml

package="com.pragyaam.grid.mobile">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:allowBackup="false"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

App.js code for notifications

this.notificationListener = firebase.notifications().onNotification((notification) => {
let notificationMessage = notification._android._notification._data.action;
let recordId = notification._android._notification._data.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);
const {
    title,
    body
} = notification;
this.showAlert(title, body, );
 });
 const notificationOpen = await firebase.notifications().getInitialNotification();
 if (notificationOpen) {
const {
    title,
    body
} = notificationOpen.notification;
this.showAlert(title, body);
 }
this.notificationOpenedListener =
firebase.notifications().onNotificationOpened((notificationOpen) => {
    const {
        title,
        body
    } = notificationOpen.notification;
    this.showAlert(title, body);
});
 this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
 });
this.handleConnectivityChange = isConnected => {
this.setState({
    isConnected
});
 }
}
Stale

Most helpful comment

Did you manage to get notifications when the app is in the foreground? it does not work with IOS and android. In IOS i do import this dependency:

import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

but notifications appear just when the app is closed or in the background.

All 12 comments

https://medium.com/@anum.amin/react-native-integrating-push-notifications-using-fcm-349fff071591
hello, this tutorial works in this issue, especially in

import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

the RNFirebaseNotificationsPackage, we have encountered the same issue and have fixed it by adding the package :)

I have a problem not being able to get the data body of a notification when the app is closed.

Can you help me, @calz10 ?

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.

Did you manage to get notifications when the app is in the foreground? it does not work with IOS and android. In IOS i do import this dependency:

import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

but notifications appear just when the app is closed or in the background.

same issue(Not getting the notifications in the foreground when using the firebase). Anyone have any update so please help ?

I have the same issue

Same issue for me too

same issue

Any update on this.

This issue is closed. Notifications don't exist in RNFBv6+. Upgrade to RNFB-current, use data-only silent firebase cloud messages, and re-test. If it fails, open a new issue with details populated in the template.

Same issue does any alternative way?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abdullahizzuddiin picture abdullahizzuddiin  路  80Comments

tranty9597 picture tranty9597  路  79Comments

GH974 picture GH974  路  47Comments

kevinEsherick picture kevinEsherick  路  57Comments

gilbert picture gilbert  路  65Comments