UpdateEndpoint搭配使用 AWS SDK - Amazon Pinpoint

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

UpdateEndpoint搭配使用 AWS SDK

以下代码示例显示了如何使用UpdateEndpoint

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.EndpointResponse; import software.amazon.awssdk.services.pinpoint.model.EndpointRequest; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointRequest; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointResponse; import software.amazon.awssdk.services.pinpoint.model.GetEndpointRequest; import software.amazon.awssdk.services.pinpoint.model.GetEndpointResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.EndpointDemographic; import software.amazon.awssdk.services.pinpoint.model.EndpointLocation; import software.amazon.awssdk.services.pinpoint.model.EndpointUser; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; import java.util.UUID; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Date; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class UpdateEndpoint { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The ID of the application to create an endpoint for. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String appId = args[0]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); EndpointResponse response = createEndpoint(pinpoint, appId); System.out.println("Got Endpoint: " + response.id()); pinpoint.close(); } public static EndpointResponse createEndpoint(PinpointClient client, String appId) { String endpointId = UUID.randomUUID().toString(); System.out.println("Endpoint ID: " + endpointId); try { EndpointRequest endpointRequest = createEndpointRequestData(); UpdateEndpointRequest updateEndpointRequest = UpdateEndpointRequest.builder() .applicationId(appId) .endpointId(endpointId) .endpointRequest(endpointRequest) .build(); UpdateEndpointResponse updateEndpointResponse = client.updateEndpoint(updateEndpointRequest); System.out.println("Update Endpoint Response: " + updateEndpointResponse.messageBody()); GetEndpointRequest getEndpointRequest = GetEndpointRequest.builder() .applicationId(appId) .endpointId(endpointId) .build(); GetEndpointResponse getEndpointResponse = client.getEndpoint(getEndpointRequest); System.out.println(getEndpointResponse.endpointResponse().address()); System.out.println(getEndpointResponse.endpointResponse().channelType()); System.out.println(getEndpointResponse.endpointResponse().applicationId()); System.out.println(getEndpointResponse.endpointResponse().endpointStatus()); System.out.println(getEndpointResponse.endpointResponse().requestId()); System.out.println(getEndpointResponse.endpointResponse().user()); return getEndpointResponse.endpointResponse(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } private static EndpointRequest createEndpointRequestData() { try { List<String> favoriteTeams = new ArrayList<>(); favoriteTeams.add("Lakers"); favoriteTeams.add("Warriors"); HashMap<String, List<String>> customAttributes = new HashMap<>(); customAttributes.put("team", favoriteTeams); EndpointDemographic demographic = EndpointDemographic.builder() .appVersion("1.0") .make("apple") .model("iPhone") .modelVersion("7") .platform("ios") .platformVersion("10.1.1") .timezone("America/Los_Angeles") .build(); EndpointLocation location = EndpointLocation.builder() .city("Los Angeles") .country("US") .latitude(34.0) .longitude(-118.2) .postalCode("90068") .region("CA") .build(); Map<String, Double> metrics = new HashMap<>(); metrics.put("health", 100.00); metrics.put("luck", 75.00); EndpointUser user = EndpointUser.builder() .userId(UUID.randomUUID().toString()) .build(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone // offset String nowAsISO = df.format(new Date()); return EndpointRequest.builder() .address(UUID.randomUUID().toString()) .attributes(customAttributes) .channelType("APNS") .demographic(demographic) .effectiveDate(nowAsISO) .location(location) .metrics(metrics) .optOut("NONE") .requestId(UUID.randomUUID().toString()) .user(user) .build(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 UpdateEndpoint” 中的。

有关 AWS SDK开发者指南和代码示例的完整列表,请参阅将 Amazon Pinpoint 与 AWS SDK。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。