使用 Amazon Pinpoint 以程式設計方式擷取端點的應用程式內訊息 - Amazon Pinpoint

支援終止通知:2026 年 10 月 30 日, AWS 將結束對 Amazon Pinpoint 的支援。2026 年 10 月 30 日之後,您將無法再存取 Amazon Pinpoint 主控台或 Amazon Pinpoint 資源 (端點、區段、行銷活動、旅程和分析)。如需詳細資訊,請參閱 Amazon Pinpoint 終止支援注意:與 SMS、語音、行動推播、OTP 和電話號碼驗證相關的 APIs 不受此變更影響,並受 AWS 最終使用者傳訊支援。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Amazon Pinpoint 以程式設計方式擷取端點的應用程式內訊息

您的應用程式可以呼叫 GetInAppMessages API,以擷取指定端點有權存取的所有應用程式內訊息。呼叫 GetInAppMessages API 時,會提供以下參數:

  • ApplicationId – 與應用程式內訊息行銷活動相關聯的 Amazon Pinpoint 專案的唯一 ID。

  • EndpointId – 您擷取訊息的端點的唯一 ID。

您使用這些值呼叫 API 時,會傳回訊息清單。如需深入了解此操作產生的回應,請參閱 GetInAppMessages Amazon Pinpoint API 回應 JSON 範例

您可以使用 AWS SDKs來呼叫 GetInAppMessages操作。以下程式碼範例,包含擷取應用程式內訊息的函數。

JavaScript

在單獨的模組中建立用戶端並將其匯出:

import { PinpointClient } from "@aws-sdk/client-pinpoint"; const REGION = "us-east-1"; const pinClient = new PinpointClient({ region: REGION }); export { pinClient };

擷取端點的應用程式內訊息:

// Import required AWS SDK clients and commands for Node.js import { PinpointClient, GetInAppMessagesCommand } from "@aws-sdk/client-pinpoint"; import { pinClient } from "./lib/pinClient.js"; ("use strict"); //The Amazon Pinpoint application ID. const projectId = "4c545b28d21a490cb51b0b364example"; //The ID of the endpoint to retrieve messages for. const endpointId = "c5ac671ef67ee3ad164cf7706example"; const params = { ApplicationId: projectId, EndpointId: endpointId }; const run = async () => { try { const data = await pinClient.send(new GetInAppMessagesCommand(params)); console.log(JSON.stringify(data, null, 4)); return data; } catch (err) { console.log("Error", err); } }; run();
Python
import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def retrieve_inapp_messages( pinpoint_client, project_id, endpoint_id): """ Retrieves the in-app messages that a given endpoint is entitled to. :param pinpoint_client: A Boto3 Pinpoint client. :param project_id: An Amazon Pinpoint project ID. :param endpoint_id: The ID of the endpoint to retrieve messages for. :return: A JSON object that contains information about the in-app message. """ try: response = pinpoint_client.get_in_app_messages( ApplicationId=project_id, EndpointId=endpoint_id) except ClientError: logger.exception("Couldn't retrieve messages.") raise else: return response def main(): project_id = "4c545b28d21a490cb51b0b364example" endpoint_id = "c5ac671ef67ee3ad164cf7706example" inapp_response = retrieve_inapp_messages( boto3.client('pinpoint'), project_id, endpoint_id) print(inapp_response) if __name__ == '__main__': main()