Page updated Jan 16, 2024

Integrate your application

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

1npm install aws-amplify

Initialize In-App Messaging

To finish setting up your application with Amplify, you need to configure it using the configure API. Next, to interact with In-App Messaging APIs, you need to first initialize In-App Messaging by calling the initializeInAppMessaging API directly imported from the in-app-messaging sub-path. This is required to be called as early as possible in the app lifecycle.

1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3import { initializeInAppMessaging } from 'aws-amplify/in-app-messaging';

Make sure you call Amplify.configure as early as possible in your application’s life-cycle. A missing configuration or NoCredentials error is thrown if Amplify.configure has not been called before other Amplify JavaScript APIs. Review the Library Not Configured Troubleshooting guide for possible causes of this issue.

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/ui-react @aws-amplify/ui-react-notifications

Integrate Amplify UI

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';
3import '@aws-amplify/ui-react/styles.css';
5const App = () => <>In-App Messaging</>;
7export default withInAppMessaging(App);

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

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