Page updated Nov 11, 2023

Integrate your application

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

npm install aws-amplify
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.

import { Amplify } from 'aws-amplify'; import amplifyconfig from './amplifyconfiguration.json'; import { initializeInAppMessaging } from 'aws-amplify/in-app-messaging'; Amplify.configure(amplifyconfig); initializeInAppMessaging();
1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3import { initializeInAppMessaging } from 'aws-amplify/in-app-messaging';
4
5Amplify.configure(amplifyconfig);
6initializeInAppMessaging();

Install the Amplify React Native Package and other dependencies

Installing the @aws-amplify/react-native will bring in the necessary polyfills.

npm install @aws-amplify/react-native @react-native-community/netinfo @react-native-async-storage/async-storage
1npm install @aws-amplify/react-native @react-native-community/netinfo @react-native-async-storage/async-storage

Install Amplify UI for React Native and its dependencies

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

npm install @aws-amplify/ui-react-native react-native-safe-area-context
1npm install @aws-amplify/ui-react-native react-native-safe-area-context

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.

import { withInAppMessaging } from '@aws-amplify/ui-react-native'; const App = () => ( {/* Your application */} ); export default withInAppMessaging(App);
1import { withInAppMessaging } from '@aws-amplify/ui-react-native';
2
3const App = () => (
4 {/* Your application */}
5);
6
7export default withInAppMessaging(App);

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

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