發佈到主題 - Amazon Simple Notification Service

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

發佈到主題

您可以透過訂閱電話號碼到 Amazon SNS 主題,一次發佈單一簡訊至多組電話號碼。SNS 主題是您可以新增訂閱者然後發佈訊息至所有那些訂閱者的通訊通道。訂閱者將收到所有發佈至主題的訊息,除非您取消訂閱,或者訂閱者從您的 AWS 帳戶退訂接收簡訊。

傳送訊息至主題 (主控台)

若要建立主題

如果您尚未擁有想要傳送簡訊的主題,請完成下列步驟。

  1. 登入 Amazon SNS 主控台

  2. 在主控台功能表中,選擇 AWS 支援簡訊的區域

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

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

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

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

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

    3. (選用) 對於 Display name (顯示名稱),輸入簡訊的自訂字首。傳送訊息至主題時,Amazon SNS 會將顯示名稱置於前面,後面加上右尖括號 (>) 和空格。顯示名稱不區分大小寫,Amazon SNS 會將顯示名稱轉換為大寫字元。例如,如果主題的顯示名稱為 MyTopic,而訊息為 Hello World!,則訊息將顯示為:

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

建立簡訊訂閱

訂閱可讓您只要發佈一次訊息到主題,就能傳送簡訊至多位收件人。

注意

當您開始使用 Amazon SNS 傳送簡訊時,AWS 帳戶位於簡訊沙盒。簡訊沙盒為您提供了一個安全的環境,讓您嘗試 Amazon SNS 功能,而不會危及您作為簡訊寄件者的聲譽。當您的帳戶位在簡訊沙盒中時,您可以使用 Amazon SNS 的所有功能,但您只能傳送簡訊給已驗證的目的地電話號碼。如需更多詳細資訊,請參閱 簡訊沙盒

  1. 登入 Amazon SNS 主控台

  2. 在導覽窗格中,選擇 Subscriptions (訂閱)。

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

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

    1. 適用於 ARN 主題中,輸入或選擇您要傳送簡訊之主題的 Amazon 資源名稱 (ARN)。

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

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

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

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

傳送訊息

當您發佈訊息至主題時,Amazon SNS 會嘗試傳遞該訊息到每個已訂閱主題的電話號碼。

  1. Amazon SNS 主控台中,在主題頁面上,選擇您要傳送簡訊的主題名稱。

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

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

    1. Subject (主旨) 欄位留白,除非您的主題包含電子郵件訂閱而您想要發佈至電子郵件和簡訊訂閱二者。Amazon SNS 使用您在主旨行輸入的電子郵件 Subject (主旨)。

    2. (選擇性) 對於Time to Live (TTL) 存留時間 (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 所新增的右尖括號 (>) 和空格兩個字元。

      如需有關簡訊大小配額的詳細資訊,請參閱 發佈至行動電話

  5. (選擇性) 對於 Message attributes (訊息屬性),新增郵件中繼資料,例如時間戳記、簽名和 ID。

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

傳送訊息至主題 (AWS 開發套件)

若要使用 AWS 開發套件,您必須使用您的認證進行設定。如需詳細資訊,請參閱 AWS 開發套件和工具參考指南中的共享的配置和認證文件

以下代碼範例顯示做法:

  • 建立 Amazon SNS 主題。

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

  • 將簡訊發布至主題,讓所有訂閱的電話號碼一次接收訊息。

Java
適用於 Java 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); } } }