Amazon Pinpoint examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Pinpoint examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with Amazon Pinpoint.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateApp.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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; /** * 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 CreateApp { public static void main(String[] args) { final String usage = """ Usage: <appName> Where: appName - The name of the application to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String appName = args[0]; System.out.println("Creating an application with name: " + appName); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); String appID = createApplication(pinpoint, appName); System.out.println("App ID is: " + appID); pinpoint.close(); } 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 ""; } }
  • For API details, see CreateApp in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateCampaign.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create a campaign.

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; /** * 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 CreateCampaign { public static void main(String[] args) { final String usage = """ Usage: <appId> <segmentId> Where: appId - The ID of the application to create the campaign in. segmentId - The ID of the segment to create the campaign from. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String appId = args[0]; String segmentId = args[1]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); createPinCampaign(pinpoint, appId, segmentId); pinpoint.close(); } 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; } }
  • For API details, see CreateCampaign in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateExportJob.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Export an endpoint.

import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.ExportJobRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.CreateExportJobRequest; import software.amazon.awssdk.services.pinpoint.model.CreateExportJobResponse; import software.amazon.awssdk.services.pinpoint.model.GetExportJobResponse; import software.amazon.awssdk.services.pinpoint.model.GetExportJobRequest; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; import software.amazon.awssdk.services.s3.model.S3Object; import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * To run this code example, you need to create an AWS Identity and Access * Management (IAM) role with the correct policy as described in this * documentation: * https://docs.aws.amazon.com/pinpoint/latest/developerguide/audience-data-export.html * * Also, set up your development environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ExportEndpoints { public static void main(String[] args) { final String usage = """ This program performs the following steps: 1. Exports the endpoints to an Amazon S3 bucket. 2. Downloads the exported endpoints files from Amazon S3. 3. Parses the endpoints files to obtain the endpoint IDs and prints them. Usage: ExportEndpoints <applicationId> <s3BucketName> <iamExportRoleArn> <path> Where: applicationId - The ID of the Amazon Pinpoint application that has the endpoint. s3BucketName - The name of the Amazon S3 bucket to export the JSON file to.\s iamExportRoleArn - The ARN of an IAM role that grants Amazon Pinpoint write permissions to the S3 bucket. path - The path where the files downloaded from the Amazon S3 bucket are written (for example, C:/AWS/). """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String applicationId = args[0]; String s3BucketName = args[1]; String iamExportRoleArn = args[2]; String path = args[3]; System.out.println("Deleting an application with ID: " + applicationId); Region region = Region.US_EAST_1; PinpointClient pinpoint = PinpointClient.builder() .region(region) .build(); S3Client s3Client = S3Client.builder() .region(region) .build(); exportAllEndpoints(pinpoint, s3Client, applicationId, s3BucketName, path, iamExportRoleArn); pinpoint.close(); s3Client.close(); } public static void exportAllEndpoints(PinpointClient pinpoint, S3Client s3Client, String applicationId, String s3BucketName, String path, String iamExportRoleArn) { try { List<String> objectKeys = exportEndpointsToS3(pinpoint, s3Client, s3BucketName, iamExportRoleArn, applicationId); List<String> endpointFileKeys = objectKeys.stream().filter(o -> o.endsWith(".gz")) .collect(Collectors.toList()); downloadFromS3(s3Client, path, s3BucketName, endpointFileKeys); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static List<String> exportEndpointsToS3(PinpointClient pinpoint, S3Client s3Client, String s3BucketName, String iamExportRoleArn, String applicationId) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH_mm:ss.SSS_z"); String endpointsKeyPrefix = "exports/" + applicationId + "_" + dateFormat.format(new Date()); String s3UrlPrefix = "s3://" + s3BucketName + "/" + endpointsKeyPrefix + "/"; List<String> objectKeys = new ArrayList<>(); String key; try { // Defines the export job that Amazon Pinpoint runs. ExportJobRequest jobRequest = ExportJobRequest.builder() .roleArn(iamExportRoleArn) .s3UrlPrefix(s3UrlPrefix) .build(); CreateExportJobRequest exportJobRequest = CreateExportJobRequest.builder() .applicationId(applicationId) .exportJobRequest(jobRequest) .build(); System.out.format("Exporting endpoints from Amazon Pinpoint application %s to Amazon S3 " + "bucket %s . . .\n", applicationId, s3BucketName); CreateExportJobResponse exportResult = pinpoint.createExportJob(exportJobRequest); String jobId = exportResult.exportJobResponse().id(); System.out.println(jobId); printExportJobStatus(pinpoint, applicationId, jobId); ListObjectsV2Request v2Request = ListObjectsV2Request.builder() .bucket(s3BucketName) .prefix(endpointsKeyPrefix) .build(); // Create a list of object keys. ListObjectsV2Response v2Response = s3Client.listObjectsV2(v2Request); List<S3Object> objects = v2Response.contents(); for (S3Object object : objects) { key = object.key(); objectKeys.add(key); } return objectKeys; } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } private static void printExportJobStatus(PinpointClient pinpointClient, String applicationId, String jobId) { GetExportJobResponse getExportJobResult; String status; try { // Checks the job status until the job completes or fails. GetExportJobRequest exportJobRequest = GetExportJobRequest.builder() .jobId(jobId) .applicationId(applicationId) .build(); do { getExportJobResult = pinpointClient.getExportJob(exportJobRequest); status = getExportJobResult.exportJobResponse().jobStatus().toString().toUpperCase(); System.out.format("Export job %s . . .\n", status); TimeUnit.SECONDS.sleep(3); } while (!status.equals("COMPLETED") && !status.equals("FAILED")); if (status.equals("COMPLETED")) { System.out.println("Finished exporting endpoints."); } else { System.err.println("Failed to export endpoints."); System.exit(1); } } catch (PinpointException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } // Download files from an Amazon S3 bucket and write them to the path location. public static void downloadFromS3(S3Client s3Client, String path, String s3BucketName, List<String> objectKeys) { String newPath; try { for (String key : objectKeys) { GetObjectRequest objectRequest = GetObjectRequest.builder() .bucket(s3BucketName) .key(key) .build(); ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest); byte[] data = objectBytes.asByteArray(); // Write the data to a local file. String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); newPath = path + fileSuffix + ".gz"; File myFile = new File(newPath); OutputStream os = new FileOutputStream(myFile); os.write(data); } System.out.println("Download finished."); } catch (S3Exception | NullPointerException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • For API details, see CreateExportJob in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateImportJob.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Import a segment.

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; /** * 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 ImportSegment { public static void main(String[] args) { final String usage = """ Usage: <appId> <bucket> <key> <roleArn>\s Where: appId - The application ID to create a segment for. bucket - The name of the Amazon S3 bucket that contains the segment definitons. key - The key of the S3 object. roleArn - ARN of the role that allows Amazon Pinpoint to access S3. You need to set trust management for this to work. See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String appId = args[0]; String bucket = args[1]; String key = args[2]; String roleArn = args[3]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); ImportJobResponse response = createImportSegment(pinpoint, appId, bucket, key, roleArn); System.out.println("Import job for " + bucket + " submitted."); System.out.println("See application " + response.applicationId() + " for import job status."); System.out.println("See application " + response.jobStatus() + " for import job status."); pinpoint.close(); } 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; } }
  • For API details, see CreateImportJob in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateSegment.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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; /** * 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 CreateSegment { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The application ID to create a segment 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(); SegmentResponse result = createSegment(pinpoint, appId); System.out.println("Segment " + result.name() + " created."); System.out.println(result.segmentType()); pinpoint.close(); } 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; } }
  • For API details, see CreateSegment in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DeleteApp.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete an application.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.DeleteAppRequest; import software.amazon.awssdk.services.pinpoint.model.DeleteAppResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; /** * 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 DeleteApp { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The ID of the application to delete. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String appId = args[0]; System.out.println("Deleting an application with ID: " + appId); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); deletePinApp(pinpoint, appId); System.out.println("Done"); pinpoint.close(); } public static void deletePinApp(PinpointClient pinpoint, String appId) { try { DeleteAppRequest appRequest = DeleteAppRequest.builder() .applicationId(appId) .build(); DeleteAppResponse result = pinpoint.deleteApp(appRequest); String appName = result.applicationResponse().name(); System.out.println("Application " + appName + " has been deleted."); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see DeleteApp in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DeleteEndpoint.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete an endpoint.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.DeleteEndpointRequest; import software.amazon.awssdk.services.pinpoint.model.DeleteEndpointResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; /** * 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 DeleteEndpoint { public static void main(String[] args) { final String usage = """ Usage: <appName> <endpointId > Where: appId - The id of the application to delete. endpointId - The id of the endpoint to delete. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String appId = args[0]; String endpointId = args[1]; System.out.println("Deleting an endpoint with id: " + endpointId); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); deletePinEncpoint(pinpoint, appId, endpointId); pinpoint.close(); } public static void deletePinEncpoint(PinpointClient pinpoint, String appId, String endpointId) { try { DeleteEndpointRequest appRequest = DeleteEndpointRequest.builder() .applicationId(appId) .endpointId(endpointId) .build(); DeleteEndpointResponse result = pinpoint.deleteEndpoint(appRequest); String id = result.endpointResponse().id(); System.out.println("The deleted endpoint id " + id); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("Done"); } }
  • For API details, see DeleteEndpoint in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use GetEndpoint.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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.GetEndpointResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.GetEndpointRequest; /** * 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 LookUpEndpoint { public static void main(String[] args) { final String usage = """ Usage: <appId> <endpoint> Where: appId - The ID of the application to delete. endpoint - The ID of the endpoint.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String appId = args[0]; String endpoint = args[1]; System.out.println("Looking up an endpoint point with ID: " + endpoint); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); lookupPinpointEndpoint(pinpoint, appId, endpoint); pinpoint.close(); } public static void lookupPinpointEndpoint(PinpointClient pinpoint, String appId, String endpoint) { try { GetEndpointRequest appRequest = GetEndpointRequest.builder() .applicationId(appId) .endpointId(endpoint) .build(); GetEndpointResponse result = pinpoint.getEndpoint(appRequest); EndpointResponse endResponse = result.endpointResponse(); // Uses the Google Gson library to pretty print the endpoint JSON. Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create(); String endpointJson = gson.toJson(endResponse); System.out.println(endpointJson); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("Done"); } }
  • For API details, see GetEndpoint in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use GetSegments.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

List segments.

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; /** * 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 ListSegments { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The ID of the application that contains a segment. """; 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(); listSegs(pinpoint, appId); pinpoint.close(); } 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); } } }
  • For API details, see GetSegments in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use GetSmsChannel.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.SMSChannelResponse; import software.amazon.awssdk.services.pinpoint.model.GetSmsChannelRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.SMSChannelRequest; import software.amazon.awssdk.services.pinpoint.model.UpdateSmsChannelRequest; import software.amazon.awssdk.services.pinpoint.model.UpdateSmsChannelResponse; /** * 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 UpdateChannel { public static void main(String[] args) { final String usage = """ Usage: CreateChannel <appId> Where: appId - The name of the application whose channel is updated. """; 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(); SMSChannelResponse getResponse = getSMSChannel(pinpoint, appId); toggleSmsChannel(pinpoint, appId, getResponse); pinpoint.close(); } private static SMSChannelResponse getSMSChannel(PinpointClient client, String appId) { try { GetSmsChannelRequest request = GetSmsChannelRequest.builder() .applicationId(appId) .build(); SMSChannelResponse response = client.getSmsChannel(request).smsChannelResponse(); System.out.println("Channel state is " + response.enabled()); return response; } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } private static void toggleSmsChannel(PinpointClient client, String appId, SMSChannelResponse getResponse) { boolean enabled = !getResponse.enabled(); try { SMSChannelRequest request = SMSChannelRequest.builder() .enabled(enabled) .build(); UpdateSmsChannelRequest updateRequest = UpdateSmsChannelRequest.builder() .smsChannelRequest(request) .applicationId(appId) .build(); UpdateSmsChannelResponse result = client.updateSmsChannel(updateRequest); System.out.println("Channel state: " + result.smsChannelResponse().enabled()); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see GetSmsChannel in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use GetUserEndpoints.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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.GetUserEndpointsRequest; import software.amazon.awssdk.services.pinpoint.model.GetUserEndpointsResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.List; /** * 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 ListEndpointIds { public static void main(String[] args) { final String usage = """ Usage: <applicationId> <userId> Where: applicationId - The ID of the Amazon Pinpoint application that has the endpoint. userId - The user id applicable to the endpoints"""; if (args.length != 2) { System.out.println(usage); System.exit(1); } String applicationId = args[0]; String userId = args[1]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); listAllEndpoints(pinpoint, applicationId, userId); pinpoint.close(); } public static void listAllEndpoints(PinpointClient pinpoint, String applicationId, String userId) { try { GetUserEndpointsRequest endpointsRequest = GetUserEndpointsRequest.builder() .userId(userId) .applicationId(applicationId) .build(); GetUserEndpointsResponse response = pinpoint.getUserEndpoints(endpointsRequest); List<EndpointResponse> endpoints = response.endpointsResponse().item(); // Display the results. for (EndpointResponse endpoint : endpoints) { System.out.println("The channel type is: " + endpoint.channelType()); System.out.println("The address is " + endpoint.address()); } } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see GetUserEndpoints in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use SendMessages.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Send an email message.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.SimpleEmailPart; import software.amazon.awssdk.services.pinpoint.model.SimpleEmail; import software.amazon.awssdk.services.pinpoint.model.EmailMessage; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.HashMap; import java.util.Map; /** * 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 SendEmailMessage { // The character encoding the you want to use for the subject line and // message body of the email. public static String charset = "UTF-8"; // The body of the email for recipients whose email clients support HTML content. static final String body = """ Amazon Pinpoint test (AWS SDK for Java 2.x) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Java 2.x """; public static void main(String[] args) { final String usage = """ Usage: <subject> <appId> <senderAddress> <toAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String subject = args[0]; String senderAddress = args[1]; String toAddress = args[2]; System.out.println("Sending a message"); PinpointEmailClient pinpoint = PinpointEmailClient.builder() .region(Region.US_EAST_1) .build(); sendEmail(pinpoint, subject, senderAddress, toAddress); System.out.println("Email was sent"); pinpoint.close(); } public static void sendEmail(PinpointEmailClient pinpointEmailClient, String subject, String senderAddress, String toAddress) { try { Content content = Content.builder() .data(body) .build(); Body messageBody = Body.builder() .text(content) .build(); Message message = Message.builder() .body(messageBody) .subject(Content.builder().data(subject).build()) .build(); Destination destination = Destination.builder() .toAddresses(toAddress) .build(); EmailContent emailContent = EmailContent.builder() .simple(message) .build(); SendEmailRequest sendEmailRequest = SendEmailRequest.builder() .fromEmailAddress(senderAddress) .destination(destination) .content(emailContent) .build(); pinpointEmailClient.sendEmail(sendEmailRequest); System.out.println("Message Sent"); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Send an email message with CC values.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.ArrayList; /** * 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 SendEmailMessageCC { // The body of the email. static final String body = """ Amazon Pinpoint test (AWS SDK for Java 2.x) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Java 2.x """; public static void main(String[] args) { final String usage = """ Usage: <subject> <senderAddress> <toAddress> <ccAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s ccAddress - The CC address. """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String subject = args[0]; String senderAddress = args[1]; String toAddress = args[2]; String ccAddress = args[3]; System.out.println("Sending a message"); PinpointEmailClient pinpoint = PinpointEmailClient.builder() .region(Region.US_EAST_1) .build(); ArrayList<String> ccList = new ArrayList<>(); ccList.add(ccAddress); sendEmail(pinpoint, subject, senderAddress, toAddress, ccList); pinpoint.close(); } public static void sendEmail(PinpointEmailClient pinpointEmailClient, String subject, String senderAddress, String toAddress, ArrayList<String> ccAddresses) { try { Content content = Content.builder() .data(body) .build(); Body messageBody = Body.builder() .text(content) .build(); Message message = Message.builder() .body(messageBody) .subject(Content.builder().data(subject).build()) .build(); Destination destination = Destination.builder() .toAddresses(toAddress) .ccAddresses(ccAddresses) .build(); EmailContent emailContent = EmailContent.builder() .simple(message) .build(); SendEmailRequest sendEmailRequest = SendEmailRequest.builder() .fromEmailAddress(senderAddress) .destination(destination) .content(emailContent) .build(); pinpointEmailClient.sendEmail(sendEmailRequest); System.out.println("Message Sent"); } catch (PinpointException e) { // Handle exception e.printStackTrace(); } } }

Send an SMS message.

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; /** * 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 SendMessage { // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. public static String messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. public static String registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html public static String senderId = "MySenderID"; public static void main(String[] args) { final String usage = """ Usage: <message> <appId> <originationNumber> <destinationNumber>\s Where: message - The body of the message to send. appId - The Amazon Pinpoint project/application ID to use when you send this message. originationNumber - The phone number or short code that you specify has to be associated with your Amazon Pinpoint account. For best results, specify long codes in E.164 format (for example, +1-555-555-5654). destinationNumber - The recipient's phone number. For best results, you should specify the phone number in E.164 format (for example, +1-555-555-5654).\s """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String message = args[0]; String appId = args[1]; String originationNumber = args[2]; String destinationNumber = args[3]; System.out.println("Sending a message"); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); sendSMSMessage(pinpoint, message, appId, originationNumber, destinationNumber); pinpoint.close(); } 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); } } }

Send batch SMS messages.

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; /** * 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 SendMessageBatch { // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. public static String messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. public static String registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html public static String senderId = "MySenderID"; public static void main(String[] args) { final String usage = """ Usage: <message> <appId> <originationNumber> <destinationNumber> <destinationNumber1>\s Where: message - The body of the message to send. appId - The Amazon Pinpoint project/application ID to use when you send this message. originationNumber - The phone number or short code that you specify has to be associated with your Amazon Pinpoint account. For best results, specify long codes in E.164 format (for example, +1-555-555-5654). destinationNumber - The recipient's phone number. For best results, you should specify the phone number in E.164 format (for example, +1-555-555-5654). destinationNumber1 - The second recipient's phone number. For best results, you should specify the phone number in E.164 format (for example, +1-555-555-5654).\s """; if (args.length != 5) { System.out.println(usage); System.exit(1); } String message = args[0]; String appId = args[1]; String originationNumber = args[2]; String destinationNumber = args[3]; String destinationNumber1 = args[4]; System.out.println("Sending a message"); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); sendSMSMessage(pinpoint, message, appId, originationNumber, destinationNumber, destinationNumber1); pinpoint.close(); } public static void sendSMSMessage(PinpointClient pinpoint, String message, String appId, String originationNumber, String destinationNumber, String destinationNumber1) { try { Map<String, AddressConfiguration> addressMap = new HashMap<String, AddressConfiguration>(); AddressConfiguration addConfig = AddressConfiguration.builder() .channelType(ChannelType.SMS) .build(); // Add an entry to the Map object for each number to whom you want to send a // message. addressMap.put(destinationNumber, addConfig); addressMap.put(destinationNumber1, 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); } } }
  • For API details, see SendMessages in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use UpdateEndpoint.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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; } }
  • For API details, see UpdateEndpoint in Amazon SDK for Java 2.x API Reference.