Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a notification by Lambda function with AWS Pinpoint

I have an iOS app with a backend build by MobileHub with API Gateway, Lambda, and DynamoDB. I noticed that the SNS function of MobileHub has been replaced by Pinpoint, and I would like to create a notification system using this new service.

When a user creates a new post through API Gateway, a lambda function will be triggered and I suppose I can send a notification to the subscribers by Pinpoint. But I cannot find any example or reference doc in the official website of Pinpoint.

Do you have any resource for this scenario or any idea? Thank you very much!

like image 810
Azure Chen Avatar asked Jul 24 '17 07:07

Azure Chen


People also ask

Which AWS allows you to send alerts?

Amazon Simple Notification Service (SNS) sends notifications two ways, A2A and A2P.

How do I use AWS SDK in lambda function?

To integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer, and then add it to your function. You can use either the AWS Command Line Interface (AWS CLI) or the Lambda console to create a Lambda layer and add it to your function.

Does AWS pinpoint support MMS?

Changes This release of the Amazon Pinpoint API introduces MMS support for SMS messages. Changes This release of the Amazon Pinpoint API introduces support for integrating recommender models with email, push notification, and SMS message templates.


1 Answers

Depends what you mean by notification, I assume you would like to send a push notification to a particular user (Pinpoint endpoint).

Pinpoint stores each device associated with a user as an "endpoint", generally created by the AWS client side analytics library (e.g. amplify analytics).

The client

With the amplify analytics library, I call updateEndpoint so I can specify a userId that is available to Lambda, as well as the device token and remove optOut so user can receive the push notification:

  1. Address - The token generated from user accepting push notification permission (iOS)
  2. optOut - NONE so they can receive push notifications
  3. userId - unique id for user (Cognito's sub)

Lambda (node.js)

Now you can send a push notification, using the userId and the Pinpoint SDK.

Example:

const sendMessagesParams = {
    ApplicationId: process.env.PINPOINT_APP_ID,
    SendUsersMessageRequest: {
        Users: {
            [receiverUserId]: {}
        },
        MessageConfiguration: {
            APNSMessage: {
                Action: 'OPEN_APP',
                Title: 'Message received',
                SilentPush: false,
                Body: `You have a new message`
            },
            GCMMessage: {
                Action: 'OPEN_APP',
                Title: 'Message received',
                SilentPush: false,
                Body: `You have a new message`
            }
        }
    }
};

console.log('sendMessagesParams', JSON.stringify(sendMessagesParams));

pinpoint.sendUsersMessages(sendMessagesParams, (sendMessagesErr, sendMessagesData) => console.log('push sent')

For your particular scenario, I set up a DynamoDB stream and trigger a Lambda when a record changes within the table. You may need to add the IAM permissions manually once the lambda is created.

Sources

  • Full list of pinpoint methods you can use in lambda (Node.JS)
  • Update endpoint using Amplify Analytics (JS)
  • Dynamodb streams - trigger a lambda
like image 101
Dylan w Avatar answered Nov 15 '22 08:11

Dylan w