코틀린에 사용하는 SDK Amazon Pinpoint 예제 - AWS SDK코드 예제

AWS 문서 AWS SDK SDK 예제 GitHub 리포지토리에 더 많은 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

코틀린에 사용하는 SDK Amazon Pinpoint 예제

다음 코드 예제는 Amazon Pinpoint와 함께 Kotlin을 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다. AWS SDK

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예제에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 상황에 맞게 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

주제

작업

다음 코드 예시에서는 CreateApp을 사용하는 방법을 보여 줍니다.

SDKKotlin의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun createApplication(applicationName: String?): String? { val createApplicationRequestOb = CreateApplicationRequest { name = applicationName } PinpointClient { region = "us-west-2" }.use { pinpoint -> val result = pinpoint.createApp( CreateAppRequest { createApplicationRequest = createApplicationRequestOb }, ) return result.applicationResponse?.id } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 CreateApp.

다음 코드 예시에서는 CreateCampaign을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun createPinCampaign( appId: String, segmentIdVal: String, ) { val scheduleOb = Schedule { startTime = "IMMEDIATE" } val defaultMessageOb = Message { action = Action.OpenApp body = "My message body" title = "My message title" } val messageConfigurationOb = MessageConfiguration { defaultMessage = defaultMessageOb } val writeCampaign = WriteCampaignRequest { description = "My description" schedule = scheduleOb name = "MyCampaign" segmentId = segmentIdVal messageConfiguration = messageConfigurationOb } PinpointClient { region = "us-west-2" }.use { pinpoint -> val result: CreateCampaignResponse = pinpoint.createCampaign( CreateCampaignRequest { applicationId = appId writeCampaignRequest = writeCampaign }, ) println("Campaign ID is ${result.campaignResponse?.id}") } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 CreateCampaign.

다음 코드 예시에서는 CreateSegment을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun createPinpointSegment(applicationIdVal: String?): String? { val segmentAttributes = mutableMapOf<String, AttributeDimension>() val myList = mutableListOf<String>() myList.add("Lakers") val atts = AttributeDimension { attributeType = AttributeType.Inclusive values = myList } segmentAttributes["Team"] = atts val recencyDimension = RecencyDimension { duration = Duration.fromValue("DAY_30") recencyType = RecencyType.fromValue("ACTIVE") } val segmentBehaviors = SegmentBehaviors { recency = recencyDimension } val segmentLocation = SegmentLocation {} val dimensionsOb = SegmentDimensions { attributes = segmentAttributes behavior = segmentBehaviors demographic = SegmentDemographics {} location = segmentLocation } val writeSegmentRequestOb = WriteSegmentRequest { name = "MySegment101" dimensions = dimensionsOb } PinpointClient { region = "us-west-2" }.use { pinpoint -> val createSegmentResult: CreateSegmentResponse = pinpoint.createSegment( CreateSegmentRequest { applicationId = applicationIdVal writeSegmentRequest = writeSegmentRequestOb }, ) println("Segment ID is ${createSegmentResult.segmentResponse?.id}") return createSegmentResult.segmentResponse?.id } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 CreateSegment.

다음 코드 예시에서는 DeleteApp을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun deletePinApp(appId: String?) { PinpointClient { region = "us-west-2" }.use { pinpoint -> val result = pinpoint.deleteApp( DeleteAppRequest { applicationId = appId }, ) val appName = result.applicationResponse?.name println("Application $appName has been deleted.") } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 DeleteApp.

다음 코드 예시에서는 DeleteEndpoint을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun deletePinEncpoint( appIdVal: String?, endpointIdVal: String?, ) { val deleteEndpointRequest = DeleteEndpointRequest { applicationId = appIdVal endpointId = endpointIdVal } PinpointClient { region = "us-west-2" }.use { pinpoint -> val result = pinpoint.deleteEndpoint(deleteEndpointRequest) val id = result.endpointResponse?.id println("The deleted endpoint is $id") } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 DeleteEndpoint.

다음 코드 예시에서는 GetEndpoint을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun lookupPinpointEndpoint( appId: String?, endpoint: String?, ) { PinpointClient { region = "us-west-2" }.use { pinpoint -> val result = pinpoint.getEndpoint( GetEndpointRequest { applicationId = appId endpointId = endpoint }, ) val endResponse = result.endpointResponse // Uses the Google Gson library to pretty print the endpoint JSON. val gson: com.google.gson.Gson = GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create() val endpointJson: String = gson.toJson(endResponse) println(endpointJson) } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 GetEndpoint.

다음 코드 예시에서는 GetSegments을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun listSegs(appId: String?) { PinpointClient { region = "us-west-2" }.use { pinpoint -> val response = pinpoint.getSegments( GetSegmentsRequest { applicationId = appId }, ) response.segmentsResponse?.item?.forEach { segment -> println("Segement id is ${segment.id}") } } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 GetSegments.

다음 코드 예시에서는 SendMessages을 사용하는 방법을 보여 줍니다.

SDK코틀린의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/** Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html */ val body: String = """ Amazon Pinpoint test (AWS SDK for Kotlin) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Kotlin. """.trimIndent() suspend fun main(args: Array<String>) { val 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 toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email """ if (args.size != 3) { println(usage) exitProcess(0) } val subject = args[0] val senderAddress = args[1] val toAddress = args[2] sendEmail(subject, senderAddress, toAddress) } suspend fun sendEmail( subjectVal: String?, senderAddress: String, toAddressVal: String, ) { var content = Content { data = body } val messageBody = Body { text = content } val subContent = Content { data = subjectVal } val message = Message { body = messageBody subject = subContent } val destinationOb = Destination { toAddresses = listOf(toAddressVal) } val emailContent = EmailContent { simple = message } val sendEmailRequest = SendEmailRequest { fromEmailAddress = senderAddress destination = destinationOb this.content = emailContent } PinpointEmailClient { region = "us-east-1" }.use { pinpointemail -> pinpointemail.sendEmail(sendEmailRequest) println("Message Sent") } }
  • 자세한 API AWS SDK내용은 Kotlin API 참조를 참조하세요 SendMessages.