Integrate your application
In your application directory, you should first install the necessary dependencies for using In-App Messaging.
Install Amplify and its dependencies
npm install aws-amplify@^5 amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage react-native-get-random-values react-native-url-polyfill
You need to add the crypto.getRandomValues and URL polyfills to your application's entry point file (in most React Native apps this will be the top level index.js).
// Example index.jsimport 'react-native-get-random-values';import 'react-native-url-polyfill/auto';
import { AppRegistry } from 'react-native';import App from './App';import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
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.
npm install @aws-amplify/ui-react-native react-native-safe-area-context
Configure Amplify
Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point. For example, App.js (Expo) or index.js (React Native CLI).
import { Amplify } from 'aws-amplify';import awsconfig from './src/aws-exports';
Amplify.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
.
import { withInAppMessaging } from '@aws-amplify/ui-react-native';
const App = () => ( {/* Your application */});
export 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.
import { 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.
const { InAppMessaging } = Notifications;
Below is an example of what your entry file should look like:
import React, { useEffect } from 'react';import { Button, View } from 'react-native';import { Analytics, Notifications } from 'aws-amplify';import { withInAppMessaging } from '@aws-amplify/ui-react-native';
const { InAppMessaging } = Notifications;
// 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. InAppMessaging.syncMessages(); }, []);
return ( <View> {/* This button has an example of an analytics event triggering the in-app message. */} <Button onPress={() => { Analytics.record(myFirstEvent); }} title="Record Analytics Event" />
{/* This button has an example of an In-app Messaging event triggering the in-app message.*/} <Button onPress={() => { InAppMessaging.dispatchEvent(myFirstEvent); }} title="Send In-App Messaging Event" /> </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.