React Native如何在单击通知时重定向页面

时间:2019-02-26 06:08:44

标签: react-native react-native-push-notification

我在带有timer的页面上使用react-native-push-notification。如果5分钟后该应用程序在后台运行,则会通知我时间已过去。当我单击通知时,它会转到此页面。但是,当我完全关闭该应用程序时,在5分钟后,我单击了通知,它将转到开始页面。现在的问题是如何使其转到该页面?

//
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 个答案:

答案 0 :(得分:2)

打开或接收到任何通知时,调用回调 onNotification 称为传递带有通知数据的对象。

通知对象示例:

{
    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
}

因此,在触发onNotification时,您可以获取数据对象,并根据其值可以编写重定向逻辑。

更清楚地说,您可以在开始屏幕或主文件中使用此代码

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