文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 for Kotlin SDK 的 Amazon Pinpoint 範例
下列程式碼範例示範如何使用 for Kotlin 搭配 AWS SDKAmazon Pinpoint 來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
下列程式碼範例示範如何使用 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin 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
中的 AWS SDK for Kotlin API參考。
-