Search code examples
amazon-web-servicesreact-nativefirebase-cloud-messagingaws-cliaws-pinpoint

Push notification using aws pinpoint in react-native


I am working on sending PUSH NOTIFICATION from AWS Pinpoint to my react-native app. If I develop a new react-native app as mentioned on AWS guide online using aws amplify CLI and create whole new setup, it works fine. But problem is I already have amplify setup with Auth in my react-native app. Now I need to integrate PUSH NOTIFICATION to my existing react-native app. How I can add PUSH NOTIFICATION using AWS PINPOINT in my existing react-native app which already has AWS AUTH configured.

Below things I have tried.

  1. Created Firebase project and added the required Android dependencies and configuration in Android.
  2. Saved the API server key which is needed for AWS Pinpoint push notification.
  3. Created AWS Pinpoint project and added FCM channel in Push notification.
  4. Added below Analytics configuration in my react-native app.

Configuration in react-native:

Amplify.configure({
  Auth: {
      // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
      identityPoolId: 'ap-XXXXXX',

      // REQUIRED - Amazon Cognito Region
      region: 'ap-XXXXX',

      // OPTIONAL - Amazon Cognito Federated Identity Pool Region 
      // Required only if it's different from Amazon Cognito Region
      identityPoolRegion: 'ap-XXXX',

      // OPTIONAL - Amazon Cognito User Pool ID
      userPoolId: 'ap-XXXXX',

      // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
      userPoolWebClientId: 'XXXXXXX',
  },
  Analytics: {
    AWSPinpoint: {
        // OPTIONAL -  Amazon Pinpoint App Client ID
        appId: 'XXXXX',
        // OPTIONAL -  Amazon service region
        region: 'us-west-2',
        // OPTIONAL -  Customized endpoint
        //endpointId: 'XXXXXXXXXXXX',
        // OPTIONAL - Default Endpoint Information
        endpoint: {
           // address: 'xxxxxxx', // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
            attributes: {
                hobbies: ['piano', 'hiking'],
            },
            channelType: 'GCM', // The channel type. Valid values: APNS, GCM
            // Customized userId
            //userId: 'XXXXXXXXXXXX',
            // User attributes
            //userAttributes: {
              //  interests: ['football', 'basketball', 'AWS']
                // ...
            //}
        },
    }
  },
});

PushNotification.onNotification((notification) => {
  console.log('in app notification', notification);
});

// get the registration token
// This will only be triggered when the token is generated or updated.
PushNotification.onRegister((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);
});

Analytics.getPluggable('AWSPinpoint')._config.endpointId;
//above give me the endpoint.

My app is running fine. But when I try to send notification from AWS CLI console, I get error

AWS cli:
> aws pinpoint send-messages --cli-input-json file://test.json
An error occurred (NotFoundException) when calling the GetEndpoint operation: Resource not found

test.json:
{
  "ApplicationId": "pinpoint_app_client-id",
  "MessageRequest": {
    "Endpoints": {
      "endpoint_got_above": {}
    },
    "MessageConfiguration": {
      "DefaultPushNotificationMessage": {
        "Body": "Test Body",
        "Title": "Test Title"
      }
    }
  }
}

I am new to AWS and kinda stuck here. Already i have spent whole day on this but could not find what is the issue. Is there anything I am missing??

Please help me out.

Thanks, Munish


Solution

  • Somehow I am not getting deviceToken/Endpoint when the PushNotification onRegister method gets called. May be the reason why Pinpoint is not getting populated with firebase endpoint/token. So I had to use use updateEndpoint manually by passing the token. so i tried using firebase in my project and tried getting the token. i passed the same to Analytics updateEndpoint and it worked for me. Basicially I was not calling updateEndpoint as I did not have valid endpoint to pass in earlier.

    Posting this if someone is stuck in future. Thanks