アプリケーションの統合
Amplify React Nativeパッケージと他の依存関係をインストール
@aws-amplify/react-nativeをインストールするとReact Nativeに必要なポリフィルがもたらされます。
React Nativeバージョン0.72以下の手順
@aws-amplify/react-nativeはreact-nativeバージョンが0.72以下の場合、最小iOSデプロイメント対象が13.0である必要があります。_ios_ディレクトリにある_Podfile_を開き、target値を更新します:
- platform :ios, min_ios_version_supported+ platform :ios, 13.0npm add @aws-amplify/react-native @react-native-community/netinfo @react-native-async-storage/async-storageAmplify UI for React Nativeと依存関係をインストール
Amplify In-App MessagingはスタンドアロンのJavaScriptライブラリとして使用できますが、このガイドではAmplify UIと一緒に使用する方法を示します。Amplify UIは現在React とReact Nativeとの統合をサポートし、すぐに開始できます。
npm add @aws-amplify/ui-react-native react-native-safe-area-context@^4.2.5Amplify UI for Reactをインストール
Amplify In-App MessagingはスタンドアロンのJavaScriptライブラリとして使用できますが、このガイドではAmplify UIと一緒に使用する方法を示します。Amplify UIは現在React とReact Nativeとの統合をサポートし、すぐに開始できます。
npm add @aws-amplify/ui-react @aws-amplify/ui-react-notificationsAmplify UIを統合
Amplify UIは、In-App Messaging UIをアプリケーションに統合するための高階コンポーネントを提供します。例えばApp.jsなど、アプリケーションのルートコンポーネントをラップするだけです。
import { withInAppMessaging } from '@aws-amplify/ui-react-native';
const App = () => ( {/* Your application code */});
export default withInAppMessaging(App);以下は、エントリファイルの例です:
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 outputs from '../amplify_outputs.json';
Amplify.configure(outputs);initializeInAppMessaging();
// インアプリメッセージを表示するには、このイベント名がIn-App Messagingキャンペーンで作成したものと一致していることを確認してください!const myFirstEvent = { name: 'my_first_event' };
const App = () => { useEffect(() => { // キャンペーンからのメッセージは、表示される前にバックエンドから同期する必要があります。 // これはアプリのどこからでもトリガーできます。ここではこのコンポーネント(アプリ)が // 最初にレンダリングされるときに1回だけ同期します。 syncMessages(); }, []);
return ( <View> {/* このボタンは分析イベントがインアプリメッセージをトリガーする例です。 */} <Button onPress={() => { record(myFirstEvent); }} title="Record Analytics Event" />
{/* このボタンはIn-app Messagingイベントがインアプリメッセージをトリガーする例です。*/} <Button onPress={() => { dispatchEvent(myFirstEvent); }} title="Send In-App Messaging Event" /> </View> );};
export default withInAppMessaging(App);ターミナルでアプリをビルドして実行できるようになりました。上記の例で示されているボタンの1つをクリックすると、Pinpointコンソールで定義したインアプリメッセージがアプリに表示されます。