3

我正在尝试使用 react-native-notification 库和 aws-amplify 让 FCM 推送通知在我的 Android 反应原生项目中工作。我有一个问题,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 安装后 react-native 导航库 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 中它不起作用。任何有关此问题的指示将不胜感激!

4

1 回答 1

1

见这里:https ://github.com/aws-amplify/amplify-js/issues/2541#issuecomment-455245033

  • .onRegister()当应用程序/设备是全新的时,Android 应用程序仅调用一次。之后的调用似乎是从缓存中加载的。
于 2019-02-22T16:00:20.990 回答