React Native How to redirect on page when click on notification

自古美人都是妖i 提交于 2021-01-28 13:47:55

问题


I’m using react-native-push-notification on a page with a timer. If the application is in the background after 5 minutes, I’m notified that time has passed. When I click on the notification, it goes to this page. But when I close the application completely and after 5 minutes I click on the notification, it goes to the start page. Now the question is how to make it go to this page?

//
let remainingTime = (this.state.minute * 60 + this.state.seconds) * 1000;

let date = new Date(Date.now() + remainingTime);
PushNotification.localNotificationSchedule({
    message: "Message",
    date,
    soundName: "rush"
});

回答1:


When any notification is opened or received the callback onNotification is called passing an object with the notification data.

Notification object example:

{
    foreground: false, // BOOLEAN: If the notification was received in foreground or not
    userInteraction: false, // BOOLEAN: If the notification was opened by the user from the notification area or not
    message: 'My Notification Message', // STRING: The notification message
    data: {}, // OBJECT: The push data
}

So when onNotification is triggered you can get data object and based on its value you can write your logic of redirection.

To be more clear you can have this code on your start screen or main file

var PushNotification = require('react-native-push-notification');

PushNotification.configure({

    // (optional) Called when Token is generated (iOS and Android)
    onRegister: function(token) {
        console.log( 'TOKEN:', token );
    },

    // (required) Called when a remote or local notification is opened or received
    onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );

        // process the notification
   }
});


来源:https://stackoverflow.com/questions/54879364/react-native-how-to-redirect-on-page-when-click-on-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!