アプリ内メッセージングのセットアップ
Amplifyはアプリ内メッセージングAPIとのやり取りを可能にし、アプリユーザーにメッセージを送信できます。アプリ内メッセージングはユーザーと連携し、関連情報を提供するための強力なツールです。 キャンペーンはメッセージング施策で、特定のオーディエンスセグメントに関与します。キャンペーンは定義したスケジュールに従って調整されたメッセージを送信します。AWS Cloud Development Kit (AWS CDK)を使用して、Amazon Pinpointがサポートする任意の単一チャネル(モバイルプッシュ、アプリ内、メール、SMS、またはカスタムチャネル)を通じてメッセージを送信するキャンペーンを作成できます。
以下は、Amazon Pinpointで動作するアプリ内メッセージングリソースを作成するためにAWS CDKを利用した例です。注:このサービスにはまだ公式なハンドライティング(L2)コンストラクトがありません。
セキュリティに関する考慮事項
import { defineBackend } from "@aws-amplify/backend";import { auth } from "./auth/resource";import { data } from "./data/resource";import { CfnApp, CfnCampaign, CfnSegment,} from "aws-cdk-lib/aws-pinpoint";import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";import { Stack } from "aws-cdk-lib/core";
const backend = defineBackend({ auth, data, // additional resources });
const inAppMessagingStack = backend.createStack("inAppMessaging-stack");
// create a Pinpoint appconst pinpoint = new CfnApp(inAppMessagingStack, "Pinpoint", { name: "myPinpointApp",});
// create a segment const mySegment = new CfnSegment(inAppMessagingStack, "Segment", { applicationId: pinpoint.ref, name: "mySegment",});
// create a campaign with event and in-app message templatenew CfnCampaign(inAppMessagingStack, "Campaign", { applicationId: pinpoint.ref, name: "MyCampaign", segmentId: mySegment.attrSegmentId, schedule: { // ensure the start and end time are in the future startTime: "2024-02-23T14:39:34Z", endTime: "2024-02-29T14:32:40Z", frequency: "IN_APP_EVENT", eventFilter: { dimensions: { eventType: { dimensionType: "INCLUSIVE", values: ["my_first_event"], }, }, filterType: "ENDPOINT", }, },
messageConfiguration: { inAppMessage: { layout: "TOP_BANNER", content: [ { // define the content of the in-app message bodyConfig: { alignment: "CENTER", body: "This is an example in-app message.", textColor: "#FFFFFF", }, backgroundColor: "#000000", headerConfig: { alignment: "CENTER", header: "Welcome!", textColor: "#FFFFFF", }, // optionally, define buttons, images, etc. }, ], }, },});
//create an IAM policy to allow interacting with Pinpoint in-app messagingconst pinpointPolicy = new Policy(inAppMessagingStack, "PinpointPolicy", { policyName: "PinpointPolicy", statements: [ new PolicyStatement({ actions: [ "mobiletargeting:GetInAppMessages", "mobiletargeting:UpdateEndpoint", "mobiletargeting:PutEvents", ], resources: [pinpoint.attrArn + "/*", pinpoint.attrArn], }), ],});
// apply the policy to the authenticated and unauthenticated rolesbackend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
// patch the custom Pinpoint resource to the expected output configurationbackend.addOutput({ notifications: { amazon_pinpoint_app_id: pinpoint.ref, aws_region: Stack.of(pinpoint).region, channels: ["IN_APP_MESSAGING"], },});Amplifyライブラリのインストール
まず、aws-amplifyライブラリをインストールします:
npm add aws-amplifyアプリ内メッセージングの初期化
Amplifyを使用してアプリケーションをセットアップ完了するには、configure APIを使用して設定する必要があります。次に、アプリ内メッセージングAPIと対話するには、in-app-messagingサブパスから直接インポートされるinitializeInAppMessaging APIを呼び出してアプリ内メッセージングを初期化する必要があります。これはアプリライフサイクルの可能な限り早く呼び出すことが必須です。
import { Amplify } from 'aws-amplify';import { initializeInAppMessaging } from 'aws-amplify/in-app-messaging';import outputs from '../amplify_outputs.json';
Amplify.configure(outputs);initializeInAppMessaging();