Amazon Pinpoint SDK 的例子使用科特林 - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon Pinpoint SDK 的例子使用科特林

下列程式碼範例說明如何透過將 Kotlin 與 Amazon Pinpoint 搭配使用來執行動作和實作常見案例。 AWS SDK

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會顯示如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用CreateApp

SDK對於科特林
注意

還有更多關於 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關詳細資訊,請參閱CreateAppAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱CreateCampaignAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱CreateSegmentAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱DeleteAppAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱DeleteEndpointAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱GetEndpointAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱GetSegmentsAWS SDK的以取得 Kotlin API 的參考資料

下列程式碼範例會示範如何使用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關詳細資訊,請參閱SendMessagesAWS SDK的以取得 Kotlin API 的參考資料