將SMS消息發佈到 Amazon SNS 主題 - Amazon Simple Notification Service

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

將SMS消息發佈到 Amazon SNS 主題

您可以通過將這些電話號碼訂閱到 Amazon SNS 主題,一次向多個電話號碼發布一SMS條消息。SNS主題是一種通訊管道,您可以在其中新增訂閱者,然後將訊息發佈給所有這些訂閱者。訂閱者會收到發佈至該主題的所有訊息,直到您取消訂閱,或訂閱者選擇不接收來自您 AWS 帳戶的SMS訊息為止。

使用控制台向主題發送消息

若要建立主題

如果您還沒有要傳送SMS郵件的主題,請完成下列步驟。

  1. 登錄到 Amazon 控SNS制台

  2. 在主控台功能表中,選擇支援SMS傳訊的 [AWS 地區]。

  3. 在導覽窗格中,選擇 Topics (主題)。

  4. Topics (主題) 頁面上,選擇 Create topic (建立主題)。

  5. Create topic (建立主題) 頁面上,於 Details (詳細資料) 下方,執行下列動作:

    1. 針對 Type (類型),選擇 Standard (標準)。

    2. 針對 Name (名稱),輸入主題名稱。

    3. (選擇性) 在 [顯示名稱] 中,輸入SMS訊息的自訂首碼。當您向主題發送消息時,Amazon 會在顯示名稱SNS前面加上一個直角括號(>)和一個空格。顯示名稱不區分大小寫,Amazon 會SNS將顯示名稱轉換為大寫字元。例如,如果主題的顯示名稱為 MyTopic,而訊息為 Hello World!,則訊息將顯示為:

      MYTOPIC> Hello World!
  6. 請選擇建立主題。主題的名稱和 Amazon 資源名稱 (ARN) 會顯示在「主題」頁面上。

若要建立SMS訂閱

您可以使用訂閱將SMS郵件僅發佈一次至您的主題,將郵件傳送給多位收件者。

注意

當您開始使用 Amazon 發SNS送SMS消息時,您的 AWS 帳戶位於SMS沙箱中。SMS沙盒提供了一個安全的環境,讓您可以試用 Amazon SNS 功能,而不會冒著SMS寄件者聲譽的風險。當您的帳戶位於SMS沙箱中時,您可以使用 Amazon 的所有功能SNS,但只能將SMS消息發送到經過驗證的目的地電話號碼。如需詳細資訊,請參閱Amazon SNS SMS 沙箱

  1. 登錄到 Amazon 控SNS制台

  2. 在導覽窗格中,選擇訂閱

  3. Subscriptions (訂閱) 頁面,選擇 Create subscription (建立訂閱)

  4. Create subscription (建立訂閱) 頁面上,於 Details (詳細資訊) 區段中,執行以下作業:

    1. 在「主題」中ARN,輸入或選擇您要傳送SMS訊息的主題的 Amazon 資源名稱 (ARN)。

    2. 針對 rotocol (通訊協定),選擇 SMS

    3. 針對 Endpoint (端點),輸入您要訂閱主題的電話號碼。

  5. 選擇 Create subscription (建立訂閱)。訂閱資訊會顯示於 Subscriptions (訂閱) 頁面上。

    若要新增更多電話號碼,請重複這些步驟。您也可以新增其他類型的訂閱,例如電子郵件。

傳送訊息

當您向某個主題發布消息時,Amazon 會SNS嘗試將該消息傳遞到訂閱該主題的每個電話號碼。

  1. Amazon 主SNS控台主題頁面上,選擇要傳送SMS訊息的主題名稱。

  2. 在主題詳細資訊頁面上,選擇 Publish message (發佈訊息)。

  3. Publish message to topic (發佈訊息至主題) 頁面上,Message details (訊息詳細資訊) 下方,執行下列步驟:

    1. 對於「主」,請將欄位保持空白,除非您的主題包含電子郵件訂閱,而且您想要同時發佈至電子郵件和SMS訂閱。Amazon SNS 使用您輸入的主題作為電子郵件主題行。

    2. (選擇性) 對於存留時間 (TTL),請輸入 Amazon SNS 必須將SMS訊息傳送給任何行動應用程式端點訂閱者的秒數。

  4. Message body (訊息內文) 下方,執行下列動作:

    1. 對於 Message structure (訊息結構),選擇 Identical payload for all delivery protocols (所有傳送通訊協定的相同酬載量),將相同訊息傳送至訂閱主題的所有通訊協定類型。或者,選擇 Custom payload for each delivery protocol (每個傳遞通訊協定的自訂酬載) 為不同通訊協定類型的訂閱者自訂訊息。例如,您可以為電話號碼訂閱者輸入預設訊息,以及為電子郵件訂閱者輸入自訂訊息。

    2. 對於 Message body to send to the endpoint (要傳送至端點的訊息內文),輸入您的訊息或您對每個傳遞通訊協定的自訂訊息。

      如果您的主題具有顯示名稱,Amazon 會SNS將其添加到消息中,從而增加了消息長度。顯示名稱長度是名稱中的字元數,加上右角括號 (>) 和 Amazon SNS 新增的空格的兩個字元。

      如需郵件大小配額的相關資SMS訊,請參閱使用 Amazon 將SMS消息發布到手機 SNS

  5. (選擇性) 針對訊息屬性,新增訊息中繼資料,例如時間戳記、簽名和IDs。

  6. 選擇 Publish message (發佈訊息)。Amazon SNS 發送消SMS息並顯示成功消息。

使用 AWS SDKs

若要使用 AWS SDK,您必須使用您的認證進行設定。如需詳細資訊,請參閱工具參考指南》中的共用設定AWS SDKs和認證檔案

以下程式碼範例顯示做法:

  • 創建一個 Amazon SNS 主題。

  • 使用手機號碼訂閱主題。

  • 發布SMS消息到主題,以便所有訂閱的電話號碼一次接收消息。

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

創建一個主題並返回其ARN。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.CreateTopicRequest; import software.amazon.awssdk.services.sns.model.CreateTopicResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * 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 CreateTopic { public static void main(String[] args) { final String usage = """ Usage: <topicName> Where: topicName - The name of the topic to create (for example, mytopic). """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String topicName = args[0]; System.out.println("Creating a topic with name: " + topicName); SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); String arnVal = createSNSTopic(snsClient, topicName); System.out.println("The topic ARN is" + arnVal); snsClient.close(); } public static String createSNSTopic(SnsClient snsClient, String topicName) { CreateTopicResponse result; try { CreateTopicRequest request = CreateTopicRequest.builder() .name(topicName) .build(); result = snsClient.createTopic(request); return result.topicArn(); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }

讓端點訂閱主題

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import software.amazon.awssdk.services.sns.model.SubscribeRequest; import software.amazon.awssdk.services.sns.model.SubscribeResponse; /** * 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 SubscribeTextSMS { public static void main(String[] args) { final String usage = """ Usage: <topicArn> <phoneNumber> Where: topicArn - The ARN of the topic to subscribe. phoneNumber - A mobile phone number that receives notifications (for example, +1XXX5550100). """; if (args.length < 2) { System.out.println(usage); System.exit(1); } String topicArn = args[0]; String phoneNumber = args[1]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); subTextSNS(snsClient, topicArn, phoneNumber); snsClient.close(); } public static void subTextSNS(SnsClient snsClient, String topicArn, String phoneNumber) { try { SubscribeRequest request = SubscribeRequest.builder() .protocol("sms") .endpoint(phoneNumber) .returnSubscriptionArn(true) .topicArn(topicArn) .build(); SubscribeResponse result = snsClient.subscribe(request); System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

設定訊息的屬性,例如寄件者的 ID、最高價格及其類型。訊息屬性為選用。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SetSmsAttributesRequest; import software.amazon.awssdk.services.sns.model.SetSmsAttributesResponse; import software.amazon.awssdk.services.sns.model.SnsException; import java.util.HashMap; /** * 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 SetSMSAttributes { public static void main(String[] args) { HashMap<String, String> attributes = new HashMap<>(1); attributes.put("DefaultSMSType", "Transactional"); attributes.put("UsageReportS3Bucket", "janbucket"); SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); setSNSAttributes(snsClient, attributes); snsClient.close(); } public static void setSNSAttributes(SnsClient snsClient, HashMap<String, String> attributes) { try { SetSmsAttributesRequest request = SetSmsAttributesRequest.builder() .attributes(attributes) .build(); SetSmsAttributesResponse result = snsClient.setSMSAttributes(request); System.out.println("Set default Attributes to " + attributes + ". Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

發布訊息至主題。訊息會傳送至每位訂閱者。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.PublishRequest; import software.amazon.awssdk.services.sns.model.PublishResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * 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 PublishTextSMS { public static void main(String[] args) { final String usage = """ Usage: <message> <phoneNumber> Where: message - The message text to send. phoneNumber - The mobile phone number to which a message is sent (for example, +1XXX5550100).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String message = args[0]; String phoneNumber = args[1]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); pubTextSMS(snsClient, message, phoneNumber); snsClient.close(); } public static void pubTextSMS(SnsClient snsClient, String message, String phoneNumber) { try { PublishRequest request = PublishRequest.builder() .message(message) .phoneNumber(phoneNumber) .build(); PublishResponse result = snsClient.publish(request); System.out .println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }