使用 Amazon Pinpoint - Amazon SDK for Java
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

您现在可以使用Amazon S3转移经理(开发人员预览版)中的Amazon SDK for Java2.x 用于加速文件传输。试试一下然后告诉我们您的想法

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon Pinpoint

您可以使用 Amazon Pinpoint 通过多个通信通道(例如推送通知、短信和电子邮件)向客户发送相关的个性化消息。

创建项目

Amazon Pinpoint 中的项目(或应用程序)是设置、客户数据、细分和市场活动的集合。

要创建项目,请先构建一个 CreateApplicationRequest 对象,并将项目的名称用作该对象的 name() 的值。然后,构建一个 CreateAppRequest 对象,并传入 CreateApplicationRequest 对象作为前者的 createApplicationRequest() 方法的值。调用createApp()您的方式PinpointClient中,传入CreateAppRequest对象。将此请求的结果作为 CreateAppResponse 对象捕获,如以下代码段中所示。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.CreateAppRequest; import software.amazon.awssdk.services.pinpoint.model.CreateAppResponse; import software.amazon.awssdk.services.pinpoint.model.CreateApplicationRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException;

代码

public static String createApplication(PinpointClient pinpoint, String appName) { try { CreateApplicationRequest appRequest = CreateApplicationRequest.builder() .name(appName) .build(); CreateAppRequest request = CreateAppRequest.builder() .createApplicationRequest(appRequest) .build(); CreateAppResponse result = pinpoint.createApp(request); return result.applicationResponse().id(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

请参阅 GitHub 上的完整示例

创建动态细分

细分是共享特定属性(例如居住的城市或访问您网站的频率)的客户的集合。动态细分是基于您定义的属性并能随时间变化的细分。

要创建动态细分,请先为此细分构建所需的所有维度。例如,将以下代码段设置为包括过去 30 天内在网站上处于活动状态的客户。您可通过首先构建RecencyDimension使用对象执行duration()recencyType()你想要(也就是说,ACTIVE要么INACTIVE),然后将此对象传递给SegmentBehaviors构建器对象作为的值recency().

在定义细分属性后,可以将这些属性构建到 SegmentDimensions 对象中。然后,构建一个 WriteSegmentRequest 对象,并传入 SegmentDimensions 对象作为前者的 dimensions() 的值。接下来,构建一个 CreateSegmentRequest 对象,并传入 WriteSegmentRequest 对象作为前者的 writeSegmentRequest() 的值。最后,调用 PinpointClientcreateSegment() 方法,并传入 CreateSegmentRequest 对象。将此请求的结果作为 CreateSegmentResponse 对象捕获。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AttributeDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentResponse; import software.amazon.awssdk.services.pinpoint.model.AttributeType; import software.amazon.awssdk.services.pinpoint.model.RecencyDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentBehaviors; import software.amazon.awssdk.services.pinpoint.model.SegmentDemographics; import software.amazon.awssdk.services.pinpoint.model.SegmentLocation; import software.amazon.awssdk.services.pinpoint.model.SegmentDimensions; import software.amazon.awssdk.services.pinpoint.model.WriteSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; import java.util.Map;

代码

public static SegmentResponse createSegment(PinpointClient client, String appId) { try { Map<String, AttributeDimension> segmentAttributes = new HashMap<>(); segmentAttributes.put("Team", AttributeDimension.builder() .attributeType(AttributeType.INCLUSIVE) .values("Lakers") .build()); RecencyDimension recencyDimension = RecencyDimension.builder() .duration("DAY_30") .recencyType("ACTIVE") .build(); SegmentBehaviors segmentBehaviors = SegmentBehaviors.builder() .recency(recencyDimension) .build(); SegmentDemographics segmentDemographics = SegmentDemographics .builder() .build(); SegmentLocation segmentLocation = SegmentLocation .builder() .build(); SegmentDimensions dimensions = SegmentDimensions .builder() .attributes(segmentAttributes) .behavior(segmentBehaviors) .demographic(segmentDemographics) .location(segmentLocation) .build(); WriteSegmentRequest writeSegmentRequest = WriteSegmentRequest.builder() .name("MySegment") .dimensions(dimensions) .build(); CreateSegmentRequest createSegmentRequest = CreateSegmentRequest.builder() .applicationId(appId) .writeSegmentRequest(writeSegmentRequest) .build(); CreateSegmentResponse createSegmentResult = client.createSegment(createSegmentRequest); System.out.println("Segment ID: " + createSegmentResult.segmentResponse().id()); System.out.println("Done"); return createSegmentResult.segmentResponse(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; }

请参阅 GitHub 上的完整示例

导入静态细分

静态细分是您从 Amazon Pinpoint 的外部创建和导入的细分。以下示例代码演示如何通过从 Amazon S3 导入静态细分来创建它。

先决条件

您需要先创建一个 IAM 角色来向 Amazon Pinpoint 授予对 Amazon S3 的访问权限,然后才能完成本示例。有关更多信息,请参阅 。IAM用于导入终端节点或分段的 角色中的Amazon Pinpoint开发人员指南 的第一个版本。

要导入静态细分,请先构建 ImportJobRequest 对象。在构建器中,指定 s3Url()roleArn()format()

注意

有关的属性的更多信息ImportJobRequest,请参阅导入作业的 ImportJobRequest 部分中的Amazon PinpointAPI 参考。

然后,构建一个 CreateImportJobRequest 对象,再传入 ImportJobRequest 对象作为前者的 importJobRequest() 的值,并传入项目的 ID 作为 applicationId()。调用 PinpointClientcreateImportJob() 方法,并传入 CreateImportJobRequest 对象。将此请求的结果作为 CreateImportJobResponse 对象捕获,如下面的代码段中所示。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.CreateImportJobRequest; import software.amazon.awssdk.services.pinpoint.model.ImportJobResponse; import software.amazon.awssdk.services.pinpoint.model.ImportJobRequest; import software.amazon.awssdk.services.pinpoint.model.Format; import software.amazon.awssdk.services.pinpoint.model.CreateImportJobResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException;

代码

public static ImportJobResponse createImportSegment(PinpointClient client, String appId, String bucket, String key, String roleArn) { try { ImportJobRequest importRequest = ImportJobRequest.builder() .defineSegment(true) .registerEndpoints(true) .roleArn(roleArn) .format(Format.JSON) .s3Url("s3://" + bucket + "/" + key) .build(); CreateImportJobRequest jobRequest = CreateImportJobRequest.builder() .importJobRequest(importRequest) .applicationId(appId) .build(); CreateImportJobResponse jobResponse = client.createImportJob(jobRequest); return jobResponse.importJobResponse(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; }

请参阅 GitHub 上的完整示例

列出项目的细分

要列出与特定项目关联的细分,请先构建一个 GetSegmentsRequest 对象,并将项目的 ID 作为前者的 applicationId() 的值。接下来,调用 PinpointClientgetSegments() 方法,并传入 GetSegmentsRequest 对象。将此请求的结果作为 GetSegmentsResponse 对象捕获。最后,实例化列表对象升级到SegmentResponse类。然后调用 GetSegmentsResponsesegmentsResponse().item(),如以下代码段中所示。您可以从该处遍历结果。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.GetSegmentsRequest; import software.amazon.awssdk.services.pinpoint.model.GetSegmentsResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.SegmentResponse; import java.util.List;

代码

public static void listSegs( PinpointClient pinpoint, String appId) { try { GetSegmentsRequest request = GetSegmentsRequest.builder() .applicationId(appId) .build(); GetSegmentsResponse response = pinpoint.getSegments(request); List<SegmentResponse> segments = response.segmentsResponse().item(); for(SegmentResponse segment: segments) { System.out.println("Segement " + segment.id() + " " + segment.name() + " " + segment.lastModifiedDate()); } } catch ( PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

请参阅 GitHub 上的完整示例

创建市场活动

市场活动是一项旨在通过向特定的受众细分群体发送消息来吸引他们的方案。

要创建一个市场活动,请先为此市场活动构建所需的所有设置。例如,在以下代码段中,营销活动将立即启动,因为startTime()Schedule设置为IMMEDIATE. 要改为将它设置为在某个特定时间开始,请以 ISO 8601 格式指定时间。

注意

有关可用于广告活动的设置的更多信息,请参阅Schedule的部分活动中的Amazon PinpointAPI 参考。

定义市场活动配置后,在 WriteCampaignRequest 对象中构建它。WriteCampaignRequestbuilder() 的任何方法都不是必需的。但您确实需要包含为市场活动设置的任何配置设置 (MessageConfiguration)。此外,我们建议您为您的市场活动添加 namedescription,以便轻松地将它与其他市场活动区分开来。调用 PinpointClientcreateCampaign () 方法,并传入 WriteCampaignRequest 对象。将此请求的结果作为 CreateCampaignResponse 对象捕获。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.CampaignResponse; import software.amazon.awssdk.services.pinpoint.model.Message; import software.amazon.awssdk.services.pinpoint.model.Schedule; import software.amazon.awssdk.services.pinpoint.model.Action; import software.amazon.awssdk.services.pinpoint.model.MessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.WriteCampaignRequest; import software.amazon.awssdk.services.pinpoint.model.CreateCampaignResponse; import software.amazon.awssdk.services.pinpoint.model.CreateCampaignRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException;

代码

public static void createPinCampaign(PinpointClient pinpoint, String appId, String segmentId) { CampaignResponse result = createCampaign(pinpoint, appId, segmentId); System.out.println("Campaign " + result.name() + " created."); System.out.println(result.description()); } public static CampaignResponse createCampaign(PinpointClient client, String appID, String segmentID) { try { Schedule schedule = Schedule.builder() .startTime("IMMEDIATE") .build(); Message defaultMessage = Message.builder() .action(Action.OPEN_APP) .body("My message body.") .title("My message title.") .build(); MessageConfiguration messageConfiguration = MessageConfiguration.builder() .defaultMessage(defaultMessage) .build(); WriteCampaignRequest request = WriteCampaignRequest.builder() .description("My description") .schedule(schedule) .name("MyCampaign") .segmentId(segmentID) .messageConfiguration(messageConfiguration) .build(); CreateCampaignResponse result = client.createCampaign( CreateCampaignRequest.builder() .applicationId(appID) .writeCampaignRequest(request).build() ); System.out.println("Campaign ID: " + result.campaignResponse().id()); return result.campaignResponse(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; }

请参阅 GitHub 上的完整示例

发送消息

要通过 Amazon Pinpoint 发送 SMS 文本消息,请先构建一个 AddressConfiguration 对象来指定 channelType()。(在下面的示例中,它将设置为 ChannelType.SMS 以指示消息将通过 SMS 发送。) 初始化 HashMap 以存储目标电话号码和 AddressConfiguration 对象。接下来,构建一个包含相关值的 SMSMessage 对象。其中包括 originationNumber、消息类型 (messageType) 以及消息本身的 body

创建消息后,创建SMSMessage中的对象DirectMessageConfiguration对象。构建您的映射对象和DirectMessageConfiguration中的对象MessageRequest对象。构建一个 SendMessagesRequest 对象,包括您的项目 ID (applicationId) 和您的 MessageRequest 对象。调用 PinpointClientsendMessages() 方法,并传入 SendMessagesRequest 对象。将此请求的结果作为 SendMessagesResponse 对象捕获。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.SMSMessage; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesResponse; import software.amazon.awssdk.services.pinpoint.model.MessageResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; import java.util.Map;

代码

public static void sendSMSMessage(PinpointClient pinpoint, String message, String appId, String originationNumber, String destinationNumber) { try { Map<String, AddressConfiguration> addressMap = new HashMap<String, AddressConfiguration>(); AddressConfiguration addConfig = AddressConfiguration.builder() .channelType(ChannelType.SMS) .build(); addressMap.put(destinationNumber, addConfig); SMSMessage smsMessage = SMSMessage.builder() .body(message) .messageType(messageType) .originationNumber(originationNumber) .senderId(senderId) .keyword(registeredKeyword) .build(); // Create a DirectMessageConfiguration object DirectMessageConfiguration direct = DirectMessageConfiguration.builder() .smsMessage(smsMessage) .build(); MessageRequest msgReq = MessageRequest.builder() .addresses(addressMap) .messageConfiguration(direct) .build(); // create a SendMessagesRequest object SendMessagesRequest request = SendMessagesRequest.builder() .applicationId(appId) .messageRequest(msgReq) .build(); SendMessagesResponse response= pinpoint.sendMessages(request); MessageResponse msg1 = response.messageResponse(); Map map1 = msg1.result(); //Write out the result of sendMessage map1.forEach((k, v) -> System.out.println((k + ":" + v))); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

请参阅 GitHub 上的完整示例

有关更多信息,请参见 Amazon Pinpoint 开发人员指南