【问题标题】:React Native How to redirect on page when click on notificationReact Native单击通知时如何在页面上重定向
【发布时间】:2019-02-26 06:08:43
【问题描述】:

我在带有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"
});

【问题讨论】:

  • 当您在这里提出问题时,您必须提供一些代码的 sn-ps。其他人也可以得到想法并给出解决方案。

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


【解决方案1】:

当打开或接收到任何通知时,回调 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
   }
});

【讨论】:

  • 这对我不起作用...我在主屏幕的 componentDidMount 方法中尝试了这个,但控制台中没有出现任何内容。
  • 你能帮忙吗? @Firu
  • 没有任何代码,对不起,我帮不上忙,而且 onNotification 是由库本身调用的,所以如果它没有发生,请检查他们的 github 问题页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多