React Native 如何在单击通知时重定向页面 [英] React Native How to redirect on page when click on notification

查看:103
本文介绍了React Native 如何在单击通知时重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有 计时器的页面上使用 react-native-push-notification一>.如果应用程序在 5 分钟后在后台,我会收到时间已过的通知.当我单击通知时,它会转到此页面.但是当我完全关闭应用程序并在 5 分钟后单击通知时,它会转到起始页面.现在的问题是如何让它进入这个页面?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆