Overview
In-App Messaging allows you to better engage users with contextually appropriate messages rendered on events triggered by their activity while using your application. Create messages that look native to your application and deliver them to your users all without additional code changes.
@aws-amplify/ui-react-native
package is currently on version 2. Working with@aws-amplify/ui-react-native
version 1 or earlier? See our migration guide.Amplify UI In-App Messaging uses the Amplify In-App Messaging API and Amazon Pinpoint.
Prerequisites
Getting Started
Integration with your application can be done with the InAppMessagingProvider
and InAppMessageDisplay
components directly,
or wrap your app in withInAppMessaging
(a React Higher-Order Component):
import React, { useEffect } from 'react';
import { Text } from 'react-native';
import { Amplify, Notifications } from 'aws-amplify';
import {
InAppMessageDisplay,
InAppMessagingProvider,
} from '@aws-amplify/ui-react-native';
import config from './aws-exports';
const { InAppMessaging } = Notifications;
Amplify.configure(config);
function App() {
useEffect(() => {
// sync remote in-app messages
InAppMessaging.syncMessages();
}, []);
return (
<InAppMessagingProvider>
<InAppMessageDisplay />
<Text>In-App Messaging Example</Text>
</InAppMessagingProvider>
);
}
export default App;
import React, { useEffect } from 'react';
import { Text } from 'react-native';
import { Amplify, Notifications } from 'aws-amplify';
import { withInAppMessaging } from '@aws-amplify/ui-react-native';
import config from './aws-exports';
const { InAppMessaging } = Notifications;
Amplify.configure(config);
function App() {
useEffect(() => {
// sync remote in-app messages
InAppMessaging.syncMessages();
}, []);
return <Text>In-App Messaging Example</Text>;
}
export default withInAppMessaging(App);
InAppMessaging.syncMessages
is an asynchronous function that handles syncing remote in-app messages with the end user application.
InAppMessagingProvider
and useInAppMessaging
InAppMessagingProvider
exposes the value of the InAppMessagingContext
(a React context)
to its children
. useInAppMessaging
can be used to directly interact with InAppMessagingContext
from within InAppMessagingProvider
.
useInAppMessaging
exposes the following functions and values of the InAppMessagingContext
:
Name | Description | Type |
---|---|---|
clearMessage | Removes the current in-app message (if any) from context state |
|
displayMessage | Render a local in-app message |
|
message | current in-app message (if any) loaded in context state |
|
In some use cases, you may want to forgo the usage of the default UI handling altogether while still leveraging the Amplify provided In-App Messaging React context and provider for in-app message context state. This can be achieved by wrapping your application in an InAppMessagingProvider
and utilizing the useInAppMessaging
hook to expose the values of the InAppMessagingContext
.
import {
InAppMessagingProvider,
useInAppMessaging
} from '@aws-amplify/ui-react-native';
import { Home } from './src/Home';
const MyInAppMessageDisplay = () => {
const { inAppMessage } = useInAppMessaging();
// ...do something with inAppMessage
};
const App = () => {
return (
<InAppMessagingProvider>
<MyInAppMessageDisplay />
<Home />
</InAppMessagingProvider>
);
};
InAppMessageDisplay
InAppMessageDisplay
handles the display and lifecycle of an in-app message.
Name | Description | Type |
---|---|---|
components? | Message override UI components |
|
MessageComponents
prop
The Functional UI components that render in-app message content
Name | Description | Type |
---|---|---|
BannerMessage? | Banner UI component (top, middle, and bottom layouts) |
|
CarouselMessage? | Carousel UI component (default provided for React Native only) |
|
FullScreenMessage? | FullScreen UI component |
|
ModalMessage? | Modal UI component |
|
Integrate Custom Components
You may provide your own In-App Messaging UI components to override the default Amplify provided UI components by utilizing the components prop of InAppMessagingProvider.
import React, { useCallback, useEffect } from 'react';
import { Button, Text, View } from 'react-native';
import { Amplify, Notifications } from 'aws-amplify';
import {
useInAppMessaging,
withInAppMessaging,
} from '@aws-amplify/ui-react-native';
import config from './aws-exports';
const { InAppMessaging } = Notifications;
Amplify.configure(config);
const CustomBannerMessage = (props) => {
return (
<View
style={{
alignItems: 'center',
backgroundColor='teal',
borderRadius: 4,
padding: 16,
position: 'absolute',
}}
>
<Text>{props.header.content}</Text>
<Button onPress={props.onClose} title="Close!" />
</View>
);
};
function App() {
const { displayMessage } = useInAppMessaging();
useEffect(() => {
// sync remote in-app messages
InAppMessaging.syncMessages();
}, []);
const displayCustomBannerMessage = useCallback(
() =>
displayMessage({
content: [{ header: { content: 'Hello World!' } }],
id: 'Custom message',
layout: 'TOP_BANNER',
}),
[displayMessage]
);
// display custom message component on initial render
useEffect(displayCustomBannerMessage, [displayCustomBannerMessage]);
return (
<Button
onPress={displayCustomBannerMessage}
title="Display Custom Banner Message"
/>
);
}
export default withInAppMessaging(App, {
components: { BannerMessage: CustomBannerMessage },
});
Style Override
The default Amplify UI components handle custom style using the styles
prop:
import React, { useCallback, useEffect } from 'react';
import { Button } from 'react-native';
import { Amplify, Notifications } from 'aws-amplify';
import {
InAppMessageDisplay,
useInAppMessaging,
withInAppMessaging,
} from '@aws-amplify/ui-react-native';
import '@aws-amplify/ui-react/styles.css';
import config from './aws-exports';
const { InAppMessaging } = Notifications;
Amplify.configure(config);
const StyledModalMessage = (props) => (
<InAppMessageDisplay.ModalMessage
{...props}
style={{ container: { backgroundColor: 'antiquewhite' } }}
/>
);
function App() {
const { displayMessage } = useInAppMessaging();
useEffect(() => {
// sync remote in-app messages
InAppMessaging.syncMessages();
}, []);
const displayStyledModalMessage = useCallback(
() =>
displayMessage({
content: [{ header: { content: 'Hello World!' } }],
id: 'styled message',
layout: 'MODAL',
}),
[displayMessage]
);
// display message component on initial render
useEffect(displayStyledModalMessage, [displayStyledModalMessage]);
return (
<Button
onPress={displayStyledModalMessage}
title="Display Custom Modal Message"
/>
);
}
export default withInAppMessaging(App, {
components: { ModalMessage: StyledModalMessage },
});
Available style props:
Name | Description | Type |
---|---|---|
body? | style applied to the message body |
|
closeIconButton? | style applied to the close button |
|
closeIconColor? | string color value applied to close icon |
|
container? | style applied to the primary container of the message component |
|
header? | style applied to the message header |
|
image? | style applied to the message image |
|
pageIndicatorActive? | style applied to an active page indicator (Carousel only) |
|
pageIndicatorInactive? | style applied to an inactive page indicator (Carousel only) |
|
primaryButton? | style applied to the message primary button |
|
secondaryButton? | style applied to the message secondary button |
|