Raghav
Raghav

Reputation: 71

How to integrate React native navigation with AWS Amplify push notifications in android?

I am trying to get FCM push notifications working in my react native project for android with react-native-notification library and aws-amplify. I have an issue with PushNotification.onRegister method is not called in android to get device token to send remote notifications.How can I make these two library working together in my react native project?

I followed aws-amplify documentation https://aws-amplify.github.io/docs/js/start?ref=amplify-rn-btn&platform=react-native to include amplify library in react native project. I then added push notifications set up as per https://aws-amplify.github.io/docs/js/push-notifications I have included react-native-navigation library to support navigation in my react-native app as per https://wix.github.io/react-native-navigation/#/docs/Installing After the installation react-native navigation library pushNotification.onRegister method is not getting called to receive device token without which I am unable to send push notifications in android.

I have push notifications configured in App.js as per below

 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);

Then in the constructor of App.js I am registering for PushNotification events.

 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);
    });
  });

This set up works fine and PushNotification.onRegister event is called and device token is getting printed if I don't include react-native-navigation library. In iOS I am able to get device token with react-native-navigation library but in android it is not working. Any pointers on this issue would be greatly appreciated!

Upvotes: 3

Views: 1566

Answers (1)

MattDameon
MattDameon

Reputation: 31

See here: https://github.com/aws-amplify/amplify-js/issues/2541#issuecomment-455245033

  • the Android app calls .onRegister() once only, when the app/device is completely new. The calls afterwards seems to load from cache.

Upvotes: 1

Related Questions