3

I am using react-native and amplify to send push notifications to devices via AWS Pinpoint. I can get the generated token for the devices. But I just need to send push notifications using user Id. I try to update the endpoint, but it's not working. Can anyone suggest me the proper way to handle this?

PushNotification.onRegister((token) => {
  console.log('in app registration', token);
  Analytics.updateEndpoint({
    address: token,
    channelType: "GCM",
    OptOut: 'NONE',
    userId: "12345"
  }).then(data => {
    console.log(data)
  }).catch(error => {
    console.log(error)
  });
});
Amila Dulanjana
  • 1,884
  • 1
  • 16
  • 33
  • 1
    How did you get device token in react-native android? I have this issue with gutting device token, can you please take a look: https://github.com/aws-amplify/amplify-js/issues/3080 – Lucky_girl Apr 15 '19 at 11:42
  • @Lucky_girl you have to add amplify method in the start up .js (App.js) file. I also had this issue and i moved `onRegister` method to start up.js file, then after that i was able to get the registered token. – Amila Dulanjana Apr 15 '19 at 18:19
  • I have already moved it to App.js in componentDidMount(), but still can’t get token there. Do you use standard react-native or expoKit? – Lucky_girl Apr 15 '19 at 18:22
  • i used react-native init not the expo. Did you add firebase Api Key ? – Amila Dulanjana Apr 15 '19 at 18:23
  • Yes! What version of aws-amplify and aws pushnotifications do you use? – Lucky_girl Apr 15 '19 at 18:25
  • `"@aws-amplify/pushnotification": "^1.0.23", "aws-amplify": "^1.1.22", "aws-amplify-react-native": "^2.1.8",` – Amila Dulanjana Apr 15 '19 at 18:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191870/discussion-between-amila-dulanjana-and-lucky-girl). – Amila Dulanjana Apr 15 '19 at 18:31

3 Answers3

0

It depends how you would like to send the push notification. We have created UI that allows sending a push, which triggers a lambda.

Firstly, you need the app to update the endpoint with the token / address as you have done.

Then you can send the push from the lambda, as shown in this code.

const sendPushNotification = async () => {
  const params = {
    ApplicationId: "PINPOINT_ANALYTICS_ID",
    SendUsersMessageRequest: {
      Users: { 
        "12345": {} // replace numbers with userid here connected with pinpoint endpoint
      },
      MessageConfiguration: {
        APNSMessage: {
          Action: 'OPEN_APP',
          Title: 'Title of push notification',
          SilentPush: false,
          Sound: 'default',
          Body: 'Message you would like to send'
        },
        GCMMessage: {
          Action: 'OPEN_APP',
          Title: 'Title of push notification',
          SilentPush: false,
          Sound: 'default',
          Body: 'Message you would like to send'
        },
      },
    },
  };

  return pinpoint.sendUsersMessages(params).promise();
};
await sendPushNotification();
Dylan w
  • 2,565
  • 1
  • 18
  • 30
-1

I was able to do that using @aws-amplify/analytics library. Following is the method that I used.

Analytics.configure(aws_exports);

PushNotification.onRegister((token) => {
        //alert(token) 
        console.log('in app registration', token);
        Analytics.updateEndpoint({
            address: token, // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
            attributes: {
              // Custom attributes that your app reports to Amazon Pinpoint. You can use these attributes as selection criteria when you create a segment.
              hobbies: ['piano', 'hiking'],
              interests: ['basketball']
            },
            channelType: 'GCM', // The channel type. Valid values: APNS, GCM
            userId: '221XWsdfER234',
            // User attributes
            optOut: 'ALL',
            userAttributes: {
                interests: ['football', 'basketball', 'AWS']
                // ...
            }
        }).then((data) => {
            console.log(data)
        }).catch(error => {
            console.log(error)
        })

    });
Amila Dulanjana
  • 1,884
  • 1
  • 16
  • 33
  • it looks like you have experience with aws pinpoint. Could you help look at the question I am linking below. Thanks. https://stackoverflow.com/questions/55226785/how-do-i-store-aws-pinpoint-messages-to-rds – Samuel Nde Mar 18 '19 at 22:45
  • @SamuelNde i checked but couldn't find an answer – Amila Dulanjana Mar 20 '19 at 09:09
  • What is the difference between original question and the solution? except the fact that in the solutions users attributes are added. – Lucky_girl Apr 15 '19 at 11:48
  • i forgot to add `Analytics.configure(aws_exports);` in the solution. I will update the answer – Amila Dulanjana Apr 15 '19 at 18:12
-2

With Amazon Pinpoint, you can’t send transactional message as Push notification. Meaning, you can’t send direct Push notification to a specific recipient.

Amazon Pinpoint - Push notification supports sending notifications to a targeted audience by creating a campaign and segment.

If it’s for testing only, from Pinpoint dashboard, you can send Test message to a specific user by using User ID or by using device token.

Read more here => Sending Transactional Messages from Your Apps - Amazon Pinpoint

Robert Ng
  • 22
  • 2