【问题标题】:How to prevent first route to show on notification click in react-native如何防止第一条路线显示在react-native中的通知点击
【发布时间】:2018-11-21 08:30:52
【问题描述】:

我已经使用react-native-push-nofication 实现了推送通知,这是我的推送通知配置。

const configure = () => {

 var _token

 PushNotification.configure({

   onRegister: function(token) {
     //process token

     //alert(JSON.stringify(token));
     Clipboard.setString(JSON.stringify(token))
   },

   onNotification: function(notification) {
     // process the notification
     // required on iOS only
     navigator.navigate(notification.data.url);
    // notification.finish(PushNotificationIOS.FetchResult.NoData);
   },

   senderID: Config.GCMSENDERKEY,

   permissions: {
     alert: true,
     badge: true,
     sound: true
   },

   popInitialNotification: true,
   requestPermissions: true,

 });
};

此代码成功导航到期望路线,但是当应用程序处于后台时,当用户单击通知时,它会在导航到期望路线之前显示应用程序的根路线(启动画面)。我根本不想出现闪屏。

【问题讨论】:

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


    【解决方案1】:

    我刚刚查看了这个库,我不得不说,它的设计真的很糟糕。你应该转到react-native-firebase

    但如果你想坚持下去:

    我们首先要弄清楚,我们如何才能获得打开应用程序的通知?

    我们不能使用库中的onNotification,因为我们不知道回调何时会被调用。

    库有一个选项popInitialNotification,它将抛出打开应用程序的初始通知。如果你在库的源代码中搜索这个变量你会find the following:

        if ( this.hasPoppedInitialNotification === false &&
                ( options.popInitialNotification === undefined || options.popInitialNotification === true ) ) {
            this.popInitialNotification(function(firstNotification) {
                if ( firstNotification !== null ) {
                    this._onNotification(firstNotification, true);
                }
            }.bind(this));
            this.hasPoppedInitialNotification = true;
        }
    

    如您所见,它将调用一个名为popInitialNotification(callback)function,并带有一个带有初始通知的回调函数。

    您现在可以使用该功能获取初始通知。

    PushNotification.popInitialNotification(function(notification) {
    
    });
    

    现在您可以直接访问初始通知,而无需等待onNotification

    从这里开始,您可以使用 react-navigation 中的 SwitchNavigator,如下例所示:https://reactnavigation.org/docs/en/auth-flow.html

    【讨论】:

    • PushNotification.configure 或我的代码中的其他任何地方写下这段代码?
    • 加上你提到的库react-native-firebase我可以用它来获取IOS和android的推送通知吗?
    • PushNotification.configure 之外。将它放在显示初始屏幕的位置。
    • 是的,react-native-firebase 可以同时使用这两个平台。
    • 不幸的是它向我展示了相同的行为..我的问题是没有得到通知对象..我的问题是它在导航之前显示堆栈的根屏幕我不希望我想直接导航到愿望屏幕
    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多