React Native 如何在单击通知时重定向页面 [英] React Native How to redirect on page when click on notification
问题描述
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"
});
推荐答案
当任何通知被打开或收到时,回调 onNotification
被调用并传递一个带有通知数据的对象.
When any notification is opened or received the callback onNotification
is called passing an object with the notification data.
通知对象示例:
{
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 被触发时,您可以获得数据对象,并根据其值编写重定向逻辑.
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
}
});
这篇关于React Native 如何在单击通知时重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!