Lee
Lee

Reputation: 373

Send a push notification with external link (React Native)

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

Upvotes: 1

Views: 2494

Answers (2)

Vinit Bhavsar
Vinit Bhavsar

Reputation: 254

"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

Upvotes: 0

sibabrat swain
sibabrat swain

Reputation: 1368

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);
}

}

Upvotes: 1

Related Questions