TyForHelpDude
TyForHelpDude

Reputation: 5002

RN FireBase push notification, cant access notification title & body when app is closed (getInitialNotification)

Code below works fine except, when I close the running and click next incoming notification there is not title and body.

I share code below, the line with

async createNotificationListeners() {
    /*
    * Triggered when a particular notification has been received in foreground
    * */
    this.notificationListener = firebase.notifications().onNotification((notification) => {
      const { title, body } = notification;
      console.log('onNotification:');

        const localNotification = new firebase.notifications.Notification({
          sound: 'sampleaudio',
          show_in_foreground: true,
        })
    ...

    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
      const { title, body } = notificationOpen.notification;
      console.log('onNotificationOpened:');
      Alert.alert(title, body)
    });


    const notificationOpen = await firebase.notifications().getInitialNotification();

    if (notificationOpen) {
      debugger
      const { title, body } = notificationOpen.notification; // title and body undefined
      const { data } = notificationOpen.notification;// and data has only google.delivered_priority ..
      console.log('getInitialNotification:');
      Alert.alert(title, body)
    }

  }

how can i get the notification content by opening app via clicking incoming notification,

my app package json like;

"dependencies": {
    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-firebase": "^5.5.6"
  },

Upvotes: 0

Views: 1327

Answers (2)

Mahmonir Bakhtiyari
Mahmonir Bakhtiyari

Reputation: 605

There is new Url for @abhikumar22 answer:

https://v5.rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4)-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background

Upvotes: 0

abhikumar22
abhikumar22

Reputation: 1814

When the app is closed, your listener will not work in react native rather than it will only work when the app is in foreground and background. So for the state when app is closed you have to add a background task in your react native application.

Now you have to use HeadlessJS funtionality which is available in react native.

You can refer here for Firebase Official documentation :- https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4)-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background

Hope this helps....Thanks :)

Upvotes: 1

Related Questions