翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
携帯電話に発行する
Amazon SNS を使用して、Amazon SNS トピックに電話番号を登録せずに、SMS メッセージを携帯電話に直接送信できます。
1 つのメッセージを同時に複数の電話番号に送信する場合、電話番号をトピックにサブスクライブすると便利です。SMS メッセージをトピックに発行する方法については、「トピックへ発行する」を参照してください。
メッセージを送信するとき、メッセージがコストまたは信頼性に対して最適化されているかどうかを制御できます。送信者 ID または発信番号を指定することもできます。Amazon SNS API または AWS SDK を使用してメッセージをプログラムで送信する場合、メッセージ配信の上限価格を指定できます。
各 SMS メッセージは最大 140 バイトまで含めることができ、文字限度はエンコーディングスキームによって異なります。例えば、SMS メッセージには以下を含めることができます。
-
160 GSM 文字
-
140 ASCII 文字
-
70 UCS-2 文字
サイズ限度を超えてメッセージを発行する場合は、Amazon SNS により、複数のメッセージとして送信され、それぞれが文字数の限度以内に収められます。メッセージは単語の途中ではなく、単語の境目で切り離されます。1 回の SMS 発行アクションの合計サイズ限度は、1,600 バイトです。
SMS メッセージを送信するときは、E.164 形式を使用して電話番号を指定します。E.164 形式は国際電気通信に使用される標準電話番号付与構造です。この形式に従う電話番号は最大 15 桁を設定でき、プラス記号 (+) および国コードのプレフィックスが付いています。例えば、E.164 形式の米国の電話番号は +1XXX5550100 と表示されます。
メッセージの送信 (コンソール)
Amazon SNS コンソールにサインインします。
-
コンソールメニューで、AWS SMS メッセージングをサポートしているリージョン を選択します。
-
ナビゲーションペインで、[テキストメッセージング (SMS)] を選択します。
-
リポジトリの [モバイルテキストメッセージング (SMS)] ページで、[テキストメッセージの発行]。
-
リポジトリの [SMS メッセージを発行する] ページ、[メッセージの種類] で、次のいずれかを選択します。
このメッセージレベルの設定をすると、アカウントレベルのデフォルトメッセージタイプは上書きされます。アカウントレベルのデフォルトのメッセージタイプは、[モバイルテキストメッセージング (SMS)] ページの [テキストメッセージングの優先設定] のセクションで設定できます。
プロモーションおよびトランザクションメッセージの料金表の詳細については、「世界各地の SMS 料金表」を参照してください。
-
[送信先電話番号] に、メッセージを送信する電話番号を入力します。
-
[メッセージ] に、送信するメッセージを入力します。
-
(オプション) [送信元アイデンティティ] で、受信者を識別する方法を指定します。
-
(オプション) [送信者 ID] に、少なくとも 1 つの文字を含み、スペースは含まない、3~11 文字の英数字のカスタム ID を入力します。送信者 ID は、受信側デバイスにメッセージ送信者として表示されます。例えば、メッセージソースを識別しやすいように、ビジネスブランドを使用できます。
送信者 ID のサポートについては、国、リージョン、またはその両方によって異なります。例えば、米国の電話番号へ配信されるメッセージは、送信者 ID を表示しません。送信者 ID をサポートする国およびリージョンについては、「サポートされている国と地域」を参照してください。
送信者 ID を指定しない場合、次のいずれか 1 つが送信元 ID として表示されます。
このメッセージレベルの送信者 ID は、[テキストメッセージプリファレンス] ページで設定するデフォルトの送信者 ID を上書きします。
-
[送信元番号] を指定するには、受信者のデバイス上で送信者の電話番号として表示する 5~14 の数字の文字列を入力します。この文字列は、送信先の国のお客様の AWS アカウント に設定される送信元番号と一致シている必要があります。送信元番号は、10DLC 番号、フリーダイヤル、ロングコード、ショートコードのいずれでもかまいません。 person-to-person 詳細については、「SMS メッセージの送信元アイデンティティ」を参照してください。
発信元番号を指定しない場合、Amazon SNS は設定に基づいて SMS テキストメッセージに使用する発信番号を選択します。 AWS アカウント
-
インドの受取人に SMS メッセージを送信する場合は、[国固有の属性 を展開し、次の属性を指定します。
-
エンティティ ID - インドの受信者に SMS メッセージを送信するためのエンティティ ID または プリンシパルエンティティ (PE) ID。この ID は、1 ~ 50 文字の一意の文字列で、Telecom Regulatory Authority of India (TRAI) が提供する、TRAI に登録したエンティティを識別するものです。
-
テンプレート ID - インドの受信者に SMS メッセージを送信するためのテンプレート ID。この ID は、TRAI に登録したテンプレートを識別する 1 ~ 50 文字の一意の文字列です。テンプレート ID は、メッセージに対して指定した送信者 ID に関連付ける必要があります。
インドの受信者に SMS メッセージを送信する方法の詳細については、「インドの送信者 ID 登録要件」を参照してください。
-
[メッセージの発行] を選択します。
送信元番号から SMS メッセージを送信するには、Amazon SNS コンソールのナビゲーションパネルで送信元番号を送信します。[機能] 列で [SMS] 含む送信元番号を選択し、次に [テキストメッセージの発行] を選択します。
メッセージの送信 (SDK)AWS
いずれかの AWS SDK を使用して SMS メッセージを送信するには、Amazon SNS API Publish
のリクエストに対応するその SDK の API オペレーションを使用します。このリクエストでは、電話番号に SMS メッセージを直接送信できます。次の属性名の値を設定する場合、MessageAttributes
パラメータも使用できます。
AWS.SNS.SMS.SenderID
-
少なくとも 1 つの文字を含み、スペースは含まない、3 ~ 11 文字の英数字のカスタム ID。送信者 ID は、受信デバイスにメッセージ送信者として表示されます。例えば、メッセージ ソースを識別しやすいように、ビジネスブランドを使用できます。
送信者 ID のサポートについては、国またはリージョンによって異なります。例えば、米国の電話番号へ配信されるメッセージは、送信者 ID を表示しません。送信者 ID をサポートする国およびリージョンのリストについては、「サポートされている国と地域」を参照してください。
送信者 ID を指定しない場合、サポートされている国またはリージョンでは、メッセージは送信者 ID としてロングコードを表示します。アルファベットの送信者 ID を必要とする国またはリージョンでは、送信者 ID は「NOTICE」と表示されます。
このメッセージレベルの属性は、SetSMSAttributes
リクエストを使用して設定する、アカウントレベルの属性 DefaultSenderID
を上書きします。
AWS.MM.SMS.OriginationNumber
-
5~14 個の数値のカスタム文字列で、オプションの先頭にプラス記号 (+
) を付けられます。この番号文字列は、受信側のデバイスの送信者の電話番号として表示されます。文字列は、 AWS 送信先の国のアカウントで設定されている送信元番号と一致する必要があります。発信元番号は、10DLC 番号、フリーダイヤル、 person-to-person (P2P) ロングコード、ショートコードのいずれでもかまいません。詳細については、「送信元番号」を参照してください。
オリジネーション番号を指定しない場合、Amazon SNS はアカウント設定に基づいてオリジネーション番号を選択します。 AWS
AWS.SNS.SMS.MaxPrice
-
SMS メッセージの送信に使用できる上限価格 (USD)。Amazon SNS がメッセージの送信により上限価格を超えるコストが発生すると判断した場合、Amazon SNS はメッセージを送信しません。
month-to-date SMS コストがその属性に設定されたクォータをすでに超えている場合、この属性は効果がありません。MonthlySpendLimit
SetSMSAttributes
リクエストを使用して MonthlySpendLimit
属性を設定できます。
Amazon SNS トピックにメッセージを送信する場合、トピックにサブスクライブされている各電話番号への各メッセージの配信に上限価格が適用されます。
AWS.SNS.SMS.SMSType
-
送信するメッセージのタイプ。
このメッセージレベルの属性は、SetSMSAttributes
リクエストを使用して設定する、アカウントレベルの属性 DefaultSMSType
を上書きします。
AWS.MM.SMS.EntityId
-
この属性は、インドの受信者に SMS メッセージを送信するときにのみ必要です。
これは、インドの受信者に SMS メッセージを送信するためのエンティティ ID またはプリンシパルエンティティ (PE) ID です。この ID は、1 ~ 50 文字の一意の文字列で、Telecom Regulatory Authority of India (TRAI) が提供する、TRAI に登録したエンティティを識別するものです。
AWS.MM.SMS.TemplateId
-
この属性は、インドの受信者に SMS メッセージを送信するときにのみ必要です。
これは、インドの受信者に SMS メッセージを送信するためのテンプレートです。この ID は、TRAI に登録したテンプレートを識別する 1 ~ 50 文字の一意の文字列です。テンプレート ID は、メッセージに対して指定した送信者 ID に関連付ける必要があります。
メッセージを送信する
次のコード例は、Amazon SNS を使用して SMS メッセージを発行する方法を示しています。
- .NET
-
- AWS SDK for .NET
-
namespace SNSMessageExample
{
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
public class SNSMessage
{
private AmazonSimpleNotificationServiceClient snsClient;
/// <summary>
/// Initializes a new instance of the <see cref="SNSMessage"/> class.
/// Constructs a new SNSMessage object initializing the Amazon Simple
/// Notification Service (Amazon SNS) client using the supplied
/// Region endpoint.
/// </summary>
/// <param name="regionEndpoint">The Amazon Region endpoint to use in
/// sending test messages with this object.</param>
public SNSMessage(RegionEndpoint regionEndpoint)
{
snsClient = new AmazonSimpleNotificationServiceClient(regionEndpoint);
}
/// <summary>
/// Sends the SMS message passed in the text parameter to the phone number
/// in phoneNum.
/// </summary>
/// <param name="phoneNum">The ten-digit phone number to which the text
/// message will be sent.</param>
/// <param name="text">The text of the message to send.</param>
/// <returns>Async task.</returns>
public async Task SendTextMessageAsync(string phoneNum, string text)
{
if (string.IsNullOrEmpty(phoneNum) || string.IsNullOrEmpty(text))
{
return;
}
// Now actually send the message.
var request = new PublishRequest
{
Message = text,
PhoneNumber = phoneNum,
};
try
{
var response = await snsClient.PublishAsync(request);
}
catch (Exception ex)
{
Console.WriteLine($"Error sending message: {ex}");
}
}
}
}
- C++
-
- SDK for C++
-
/**
* Publish SMS: use Amazon Simple Notification Service (Amazon SNS) to send an SMS text message to a phone number.
* Note: This requires additional AWS configuration prior to running example.
*
* NOTE: When you start using Amazon SNS to send SMS messages, your AWS account is in the SMS sandbox and you can only
* use verified destination phone numbers. See https://docs.aws.amazon.com/sns/latest/dg/sns-sms-sandbox.html.
* NOTE: If destination is in the US, you also have an additional restriction that you have use a dedicated
* origination ID (phone number). You can request an origination number using Amazon Pinpoint for a fee.
* See https://aws.amazon.com/blogs/compute/provisioning-and-using-10dlc-origination-numbers-with-amazon-sns/
* for more information.
*
* <phone_number_value> input parameter uses E.164 format.
* For example, in United States, this input value should be of the form: +12223334444
*/
//! Send an SMS text message to a phone number.
/*!
\param message: The message to publish.
\param phoneNumber: The phone number of the recipient in E.164 format.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::SNS::publishSms(const Aws::String &message,
const Aws::String &phoneNumber,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::SNS::SNSClient snsClient(clientConfiguration);
Aws::SNS::Model::PublishRequest request;
request.SetMessage(message);
request.SetPhoneNumber(phoneNumber);
const Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request);
if (outcome.IsSuccess()) {
std::cout << "Message published successfully with message id, '"
<< outcome.GetResult().GetMessageId() << "'."
<< std::endl;
}
else {
std::cerr << "Error while publishing message "
<< outcome.GetError().GetMessage()
<< std::endl;
}
return outcome.IsSuccess();
}
- Java
-
- SDK for Java 2.x
-
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);
}
}
}
- Kotlin
-
- SDK for Kotlin
-
suspend fun pubTextSMS(messageVal: String?, phoneNumberVal: String?) {
val request = PublishRequest {
message = messageVal
phoneNumber = phoneNumberVal
}
SnsClient { region = "us-east-1" }.use { snsClient ->
val result = snsClient.publish(request)
println("${result.messageId} message sent.")
}
}
- PHP
-
- SDK for PHP
-
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
/**
* Sends a text message (SMS message) directly to a phone number using Amazon SNS.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$message = 'This message is sent from a Amazon SNS code sample.';
$phone = '+1XXX5550100';
try {
$result = $SnSclient->publish([
'Message' => $message,
'PhoneNumber' => $phone,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
- Python
-
- SDK for Python (Boto3)
-
class SnsWrapper:
"""Encapsulates Amazon SNS topic and subscription functions."""
def __init__(self, sns_resource):
"""
:param sns_resource: A Boto3 Amazon SNS resource.
"""
self.sns_resource = sns_resource
def publish_text_message(self, phone_number, message):
"""
Publishes a text message directly to a phone number without need for a
subscription.
:param phone_number: The phone number that receives the message. This must be
in E.164 format. For example, a United States phone
number might be +12065550101.
:param message: The message to send.
:return: The ID of the message.
"""
try:
response = self.sns_resource.meta.client.publish(
PhoneNumber=phone_number, Message=message
)
message_id = response["MessageId"]
logger.info("Published message to %s.", phone_number)
except ClientError:
logger.exception("Couldn't publish message to %s.", phone_number)
raise
else:
return message_id