Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RNFirebase v6 Push Notifications are not coming both iOS&Android

I am trying to send notifications from firebase console to my react-native app

I followed the poor documentation here as much as I understand: https://invertase.io/oss/react-native-firebase/v6/messaging/quick-start

I installed @react-native-firebase/app and /messaging and here is my code in component:

  componentDidMount() {

    this.reqNotifications()
    this.checkNotificationPermission()

  }


  reqNotifications() {

    requestNotifications(['alert', 'badge', 'sound']).then(({status, settings}) => {

      console.log('NOTIFICATION STATUS' + status)

    });

  }

  async checkNotificationPermission() {
    const enabled =  await messaging().hasPermission();
    if (enabled) {
      console.log('APPROVED');
      await messaging().registerForRemoteNotifications()
      messaging().getToken().then(token => console.log('token: >> ' + token))



    } else {
      console.log('NOT APPROVED');
    }
  }
  • I am requesting permission via react-native-permissions and permission request is working.
  • My Apple APNs are OK on Apple and Firebase console
  • And I am getting my token by getToken() method on the code succesfully.

But I cant send anything to device from firebase; nothing happening on neither foreground nor background . I tried with-token test and also tried normal but no, nothing happens.

I added this code to componentDidMount:

messaging().onMessage(async remoteMessage => {
  console.log('FCM Message Data:', remoteMessage.data);
});

As I understand this subscribes for cloud messages and when I send some cloud message notification from firebase-console, I should get console output; but nothing happens.

I dont know what am I missing but I think there is a big update on this package and most of docs are for previous version and I really stuck here thanks for assist

like image 417
ugrdursun Avatar asked Dec 10 '25 22:12

ugrdursun


1 Answers

for rnfirebase.io V6
 componentDidMount = async () => {
    this.checkNotificationPermission();
    await messaging().requestPermission({provisional: true});
    await messaging().registerDeviceForRemoteMessages();

    await this.getFCMToken();
    if (Platform.OS === 'android') {
      this.createAndroidNotificationChannel();
    }

    this.backgroundState();
    this.foregroundState();
  };   
checkNotificationPermission = () => {
    firebase
      .messaging()
      .hasPermission()
      .then(enabled => {
        if (!enabled) {
          this.promptForNotificationPermission();
        }
      });
  };

  promptForNotificationPermission = () => {
    firebase
      .messaging()
      .requestPermission({provisional: true})
      .then(() => {
        console.log('Permission granted.');
      })
      .catch(() => {
        console.log('Permission rejected.');
      });
  };

  createAndroidNotificationChannel() {
    const channel = new firebase.notifications.Android.Channel(
      'channelId',
      'Push Notification',
      firebase.notifications.Android.Importance.Max,
    ).setDescription('Turn on to receive push notification');

    firebase.notifications().android.createChannel(channel);
  }
 foregroundState = () => {
    const unsubscribe = messaging().onMessage(async notification => {
      console.log('Message handled in the foregroundState!', notification);
    });

    return unsubscribe;
  };

  // Register background handler
  backgroundState = () => {
    messaging().setBackgroundMessageHandler(async notification => {
      console.log('Message handled in the background!', notification);
    });
  };
like image 116
Atul Raj Avatar answered Dec 13 '25 15:12

Atul Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!