如何在Android中将React本机导航与AWS Amplify推送通知集成?

时间:2019-01-31 02:45:20

标签: react-native-navigation aws-amplify wix-react-native-navigation react-native-navigation-v2

我正在尝试通过react-native-notification库和aws-amplify在我的Android React Native项目中使用FCM推送通知。我在PushNotification中遇到问题.onRegister方法在android中没有被调用来获取设备令牌以发送远程通知。如何在我的react native项目中使这两个库一起工作?

我遵循了aws-amplify文档https://aws-amplify.github.io/docs/js/start?ref=amplify-rn-btn&platform=react-native 在响应本机项目中包含放大库。 然后,我添加了根据https://aws-amplify.github.io/docs/js/push-notifications设置的推送通知 我已经包括了react-native-navigation库来支持我的react-native应用程序中的导航 https://wix.github.io/react-native-navigation/#/docs/Installing 安装后,本机反应式导航库pushNotification.onRegister方法不会被调用以接收设备令牌,否则,我将无法在android中发送推送通知。

我在App.js中配置了如下推送通知

 import PushNotification from '@aws-amplify/pushnotification';
 import Analytics from '@aws-amplify/analytics';
 import Auth from '@aws-amplify/auth';
 // retrieve temporary AWS credentials and sign requests
 Auth.configure(awsconfig);
// send analytics events to Amazon Pinpoint
Analytics.configure(awsconfig);
console.log(PushNotification);
PushNotification.configure(awsconfig);

然后在App.js的构造函数中,我正在注册PushNotification事件。

 Navigation.events().registerAppLaunchedListener(() => {
    PushNotification.onNotification((notification) => {
      // Note that the notification object structure is different    from Android and IOS
      console.log('in app notification', notification);

      // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    });

    // get the registration token
    PushNotification.onRegister((token) => {
      Alert.alert(token);
   this.setState({token:token});
      console.log('in app registration', token);
    });

    // get the notification data when notification is opened
    PushNotification.onNotificationOpened((notification) => {
        console.log('the notification is opened', notification);
    });
  });

此设置工作正常,如果我不包括react-native-navigation库,则调用PushNotification.onRegister事件并打印设备令牌。 在iOS中,我可以使用react-native-navigation库获取设备令牌,但是在android中,它无法正常工作。任何对此问题的建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

参见此处:https://github.com/aws-amplify/amplify-js/issues/2541#issuecomment-455245033

  • 当应用/设备是全新的时,Android应用仅调用一次.onRegister()。之后的调用似乎是从缓存加载的。