用于 Kotlin 的亚马逊 Pinpoint SDK 示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

用于 Kotlin 的亚马逊 Pinpoint SDK 示例

以下代码示例向您展示了如何使用带有 Amazon Pinpoint 的 Kotlin 来执行操作和实现常见场景。 AWS SDK

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅CreateApp中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅CreateCampaign中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅CreateSegment中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅DeleteApp中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅DeleteEndpoint中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅GetEndpoint中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅GetSegments中的 Kotlin AWS SDK API 参考

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

SDK对于 Kotlin 来说
注意

还有更多相关信息 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详细信息,请参阅SendMessages中的 Kotlin AWS SDK API 参考