Add notifications


In this section you’ll be adding notifications to your app. Amazon Pinpoint is a service that helps you connect with customers over channels like email, SMS, push, or voice. In a mobile app you would typically connect with your customers using push notification. In this lab, you will use email to connect to users.

You will set up a system that sends an email to a user any time you start a message with @username.

Note: You will be working within the Pinpoint sandbox for this lab.

1. Configure Analytics in your app

The Amplify Analytics category configures a Pinpoint project project for you. set one up amplify; name it and select the default options.

amplify add analytics
? Select an Analytics provider Amazon Pinpoint
? Provide your pinpoint resource name: graphqlrealtimerace
? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started):  Yes

2. Configure a Lambda function triggered by DynamoDB streams

Configure a Lambda function that will be triggered any time an update is made to the DynamoDB table storing your Messages.

amplify add function
? Select which capability you want to add: **Lambda function (serverless function)**
? Provide an AWS Lambda function name: **pinpointInterface**
? Choose the runtime that you want to use: **NodeJS**
? Choose the function template that you want to use: **Lambda trigger**
? What event source do you want to associate with Lambda trigger? **Amazon DynamoDB Stream**
? Choose a DynamoDB event source option: Use API category graphql @model backed DynamoDB table(s) in the current Amplify project
Selected resource **graphqlrealtimerace**
? Choose the graphql @model(s) **Message**

? Do you want to configure advanced settings? **Yes**
? Do you want to access other resources in this project from your Lambda function? **Yes**
? Select the categories you want this function to have access to. **analytics**
? Select the operations you want to permit on graphqlrealtimerace **create, read, update, delete**
? Do you want to invoke this function on a recurring schedule? (y/N) **No**
? Do you want to configure Lambda layers for this function? (y/N) **No**
? Do you want to edit the local lambda function now? (Y/n) **No**

Next edit your function amplify/backend/function/pinpointInterface/src/index.js

[ 🌟 ] amplify/backend/function/pinpointInterface/src/index.js

Update the address variable <your-email-address> with the email address that the emails will be sent from. For this lab, this will also be the email address the emails are sent to.

Finally, update the permissions for you Lambda function. Open the cloudformation template file for the function at amplify/backend/function/pinpointInterface. Add the following statement to the Statement of the IAM Policy amplify-lambda-execution-policy.

  "Effect": "Allow",
  "Action": [
  "Resource": "*"

Now, deploy your changes:

amplify push --yes

3. Verify your email address in Pinpoint

Once your changes have been deployed, verify your email address with pinpoint. From the terminal:

amplify analytics console

Expand Settings, and click on Email. Edit the Identity details. Enable the email channel for this project, and follow the steps to verify the email you specified in your Lambda function.


4. Test out the notifications

From the app, send a message starting with @. Note that you may have to check your spam folder


The email is sent via Pinpoint: