在React Native中从推送通知打开应用程序时如何重定向到屏幕

时间:2019-05-14 04:12:19

标签: react-native push-notification react-navigation aws-amplify

我正在使用aws-amplify库https://aws-amplify.github.io/docs/js/push-notifications开发一个react native应用。单击推送通知时,我的应用应重定向到通知屏幕。当我从以下位置打开应用时,我没有导航道具通知,因此我可以在没有道具https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html的情况下使用它,但是仍然出现此错误“未定义不是对象(正在评估't.dispatch')”。我在我的应用中使用了react-navigation

 import PushNotification from '@aws-amplify/pushnotification';
 import NotificationList from './NotificationList';
 import NavigationService from './NavigationService';

   Analytics.configure(awsmobile);

  PushNotification.configure(awsmobile);


if(Platform.OS === 'android'){


PushNotification.onNotification((notification) => {

    // console.log('in app notification : Outside App', notification);

    Auth.currentAuthenticatedUser({
      bypassCache: false  // Optional, By default is false. If set to true, this call will send a request to Cognito to get the latest user data
    }).then(user => {

    console.log('Current user : Outside App', user.attributes.sub)

    const currentDate = new Date();

    const todayDate = moment(currentDate).format("YYYY-MM-DD")

    var todayTime = moment().format('HH:mm:ss')

    const notificationDetails = {
        userId: user.attributes.sub,
        userTitle: notification.title,
        userBody: notification.body,
        userDate: todayDate,
        userTime: todayTime,
    };

    console.log("notificationDetails outside", notificationDetails)

    API.graphql(graphqlOperation(createUserNotification, { input: notificationDetails }))
        .then(response => {

            console.log(JSON.stringify(response, null, 2));
        })
        .catch(err => {
            console.log('Error Saving Details...', err);
            this.setState({ showActivityIndicator: false });
    });

  });

});

  PushNotification.onNotificationOpened((notification) => {

        const b = new Home();

        console.log('onNotificationOpened 1');

        b._handleNotificationOpen(notification)


  });

}

 class Home extends React.Component {


 constructor(props) {
 super(props);

  global.ShowRecordVar = "12"

  this._handleNotificationOpen =  
  this._handleNotificationOpen.bind(this);



  this.state = {
    apiResponse: 0,
    loading: true,

  }
 }

_handleNotificationOpen = (notification) => {

 if (notification["pinpoint.deeplink"]) {

    NavigationService.navigate('NotificationList',{notification});

 }
}

//Some More Codes

}

0 个答案:

没有答案