Page updated Jan 16, 2024

Integrate your application

In your application directory, you should first install the necessary dependencies for using In-App Messaging.

Install Amplify UI for React

Although Amplify In-App Messaging can be used as a standalone JavaScript library, this guide will show you how to use it together with Amplify UI which currently supports integration with React and React Native to get started quickly.

Learn more about Amplify In-App Messaging UI and how to fully unlock its capabilities here: Amplify UI for In-App Messaging

1npm install aws-amplify@^5 @aws-amplify/ui-react@^5 @aws-amplify/ui-react-notifications@^1
1import { Amplify } from 'aws-amplify';
2import awsconfig from './aws-exports';
3
4Amplify.configure(awsconfig);

Integrate In-App Messaging

Amplify UI provides a Higher-Order Component for ease of integrating the In-App Messaging UI with your application. Simply wrap your application root component in, for example, App.js.

1import { withInAppMessaging } from '@aws-amplify/ui-react-notifications';
2
3import '@aws-amplify/ui-react/styles.css';
4
5const App = () => <>In-App Messaging</>;
6
7export default withInAppMessaging(App);

Now your application is set up with Amplify In-App Messaging. To interact with Amplify In-App Messaging APIs, you will first need to import the Notifications category.

1import { Notifications } from 'aws-amplify';

The In-App Messaging feature is a subcategory of Notifications. To make it easier to access throughout your application, you can de-structure it.

1const { InAppMessaging } = Notifications;

Below is an example of what your entry file should look like:

1import React, { useEffect } from 'react';
2import { Analytics, Notifications } from 'aws-amplify';
3import { Button, View } from '@aws-amplify/ui-react';
4import { withInAppMessaging } from '@aws-amplify/ui-react-notifications';
5
6import '@aws-amplify/ui-react/styles.css';
7
8const { InAppMessaging } = Notifications;
9
10// To display your in-app message, make sure this event name matches one you created
11// in an In-App Messaging campaign!
12const myFirstEvent = { name: 'my_first_event' };
13
14const App = () => {
15 useEffect(() => {
16 // Messages from your campaigns need to be synced from the backend before they
17 // can be displayed. You can trigger this anywhere in your app. Here you are
18 // syncing just once when this component (your app) renders for the first time.
19 InAppMessaging.syncMessages();
20 }, []);
21
22 return (
23 <View>
24 {/* This button has an example of an analytics event triggering the in-app message. */}
25 <Button
26 onClick={() => {
27 Analytics.record(myFirstEvent);
28 }}
29 >
30 Record Analytics Event
31 </Button>
32
33 {/* This button has an example of an In-app Messaging event triggering the in-app message.*/}
34 <Button
35 onClick={() => {
36 InAppMessaging.dispatchEvent(myFirstEvent);
37 }}
38 >
39 Send In-App Messaging Event
40 </Button>
41 </View>
42 );
43};
44
45export default withInAppMessaging(App);

You can now build and run your app in your terminal. If you click on one of the buttons shown in the above example, the in-app message you defined in the Pinpoint console should be displayed in your app.