Integrate your application
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.
npm add @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
.
import { withInAppMessaging } from '@aws-amplify/ui-react-notifications';
import '@aws-amplify/ui-react/styles.css';
const App = () => ( {/* Your application code */});
export default withInAppMessaging(App);
Below is an example of what your entry file should look like:
import React, { useEffect } from 'react';import { initializeInAppMessaging, syncMessages, dispatchEvent} from 'aws-amplify/in-app-messaging';import { Button, View } from '@aws-amplify/ui-react';import { withInAppMessaging } from '@aws-amplify/ui-react-notifications';import { record } from 'aws-amplify/analytics';import '@aws-amplify/ui-react/styles.css';import outputs from '../amplify_outputs.json';
Amplify.configure(outputs);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 onClick={() => { record(myFirstEvent); }} > Record Analytics Event </Button>
{/* This button has an example of an In-app Messaging event triggering the in-app message.*/} <Button onClick={() => { dispatchEvent(myFirstEvent); }} > Send In-App Messaging Event </Button> </View> );};
export 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.