1

I built a simple react-native app using react-native-firebase. I succeed to send a push notification to my device, and then I want to implement if user touch(click) the notification from the device it will redirect to the webpage I attached to.

I installed fcm-node to send the push notification. Code is below

const FCM = require('fcm-node');

const message = {
    notification: {
        fcmTitle,
        body
    },
    to: token
};


 fcm.send(message, function (err, response) {
    console.log('response',response);
    console.log("error : "+err);
 })

So if user click the push notification, it shoud be opening web browser and redirect to page I attach from the code

Lee
  • 333
  • 3
  • 7
  • 19

2 Answers2

1

This is handled by Firebase SDK. Here when user taps on notification, your app receives payload associated with that notification.

Please have a look how to handel it

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

/*
 If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
 */
 this.notificationOpenedListener = 
firebase.notifications().onNotificationOpened((notificationOpen) => {
   const { title, body } = notificationOpen.notification;
    this.showAlert(title, body);
 });

 /*
 If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
 */
const notificationOpen = await 
firebase.notifications().getInitialNotification();
 if (notificationOpen) {
   const { title, body } = notificationOpen.notification;
   this.showAlert(title, body);
}

}
sibabrat swain
  • 1,277
  • 8
  • 20
  • I am using react-native-firebase v6. As their doc, It seems not supporting yet. https://invertase.io/oss/react-native-firebase/v6/notifications#gatsby-focus-wrapper – Lee Oct 24 '19 at 08:35
0
"message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

Notification Payload must be like this.

https://firebase.google.com/docs/cloud-messaging/concept-options

Vinit Bhavsar
  • 226
  • 3
  • 13