

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon SNS の SMS メッセージプリファレンスを設定する
<a name="sms_preferences"></a>

Amazon SNS を使用して、SMS メッセージングのプリファレンスを指定します。例えば、配信をコストまたは信頼性に対して最適化するかや、毎月の使用量の上限、配信がログに記録される方法、SMS の毎日の使用状況レポートをサブスクライブするかどうか、などを指定できます。

これらの設定は、アカウントから送信するすべての SMS メッセージに対して有効になりますが、個々のメッセージの送信時に上書きすることができます。詳細については、「[Amazon SNS を使用して SMS メッセージを携帯電話に発行する](sms_sending-overview.md#sms_publish-to-phone)」を参照してください。

## を使用した SMS メッセージングの設定 AWS マネジメントコンソール
<a name="sms_preferences_console"></a>

1. [Amazon SNS コンソール](https://console.aws.amazon.com/sns/home)にサインインします。

1. [SMS メッセージングをサポートしているリージョン](https://docs.aws.amazon.com/general/latest/gr/end-user-messaging.html)を選択します。

1. ナビゲーションパネルで、**[モバイル]**、**[テキストメッセージング (SMS)]** の順に選択します。

1. [**モバイルテキストメッセージング (SMS)**] ページの [**テキストメッセージプリファレンス**] セクションで、[**編集**] を選択します。

1. [**テキストメッセージングの優先設定の編集**] ページの [**詳細**] セクションで、以下の操作を実行します。

   1. [**デフォルトメッセージタイプ**] で、次のいずれかを選択します。
      + **[プロモーション]** — 重要度が低いメッセージ (広告など)。Amazon SNS は、最も低いコストが発生するようにメッセージ配信を最適化します。
      + **[トランザクション]** - 多要素認証のワンタイムパスコードなど、お客様のトランザクションをサポートするクリティカルメッセージ。Amazon SNS は、メッセージ配信を最も高い信頼性の実現のために最適化します。

      プロモーションおよびトランザクションメッセージの料金表の詳細については、「[グローバル SMS 料金表](https://aws.amazon.com/sns/sms-pricing/)」を参照してください。

   1. [**アカウントの使用料制限**] に、各暦月で SMS メッセージに使用する額 (USD) を入力します。
**重要**  
デフォルトでは、使用限度は 1.00 USD に設定されます。サービス限度を引き上げるには、[リクエストを送信](https://console.aws.amazon.com/support/home#/case/create?issueType=service-limit-increase&limitType=service-code-sns)します。
コンソールに設定した量がサービス限度を超える場合、Amazon SNS は SMS メッセージの発行を停止します。
Amazon SNS は分散システムであるため、使用クォータを超えると数分以内に SMS メッセージの送信を停止します。その間に SMS メッセージを送信し続けると、クォータを超えるコストが発生する可能性があります。

1. (オプション) [**デフォルトの送信者 ID**] にカスタム ID (ビジネスブランドなど) を入力します。これは、受信デバイスに送信者として表示されます。
**注記**  
送信者 ID のサポートについては、国によって異なります。

1. (オプション) **使用状況レポート用の Amazon S3 バケット名**の名前を入力します。
**注記**  
Amazon S3 バケットポリシーによって Amazon SNS への書き込みアクセスが付与される必要があります。

1. **[Save changes]** (変更の保存) をクリックします。

## 設定 (AWS SDKsの設定
<a name="sms_preferences_sdk"></a>

 AWS SDKs のいずれかを使用して SMS 設定を設定するには、Amazon SNS API の`SetSMSAttributes`リクエストに対応する SDK の アクションを使用します。 Amazon SNS このリクエストによって、月ごとの使用限度およびデフォルトの SMS タイプ (プロモーションまたはトランザクション) などの、異なる SMS の属性に値を割り当てます。利用可能な SMS 属性の詳細については、『*Amazon Simple Notification Service API リファレンス*』の「[SetSMSAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetSMSAttributes.html)」を参照してください。

次のサンプルコードは、`SetSMSAttributes` を使用する方法を説明しています。

------
#### [ C\+\+ ]

**SDK for C\+\+**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/sns#code-examples)での設定と実行の方法を確認してください。
Amazon SNS を使用して DefaultSMSType 属性を設定する方法。  

```
//! Set the default settings for sending SMS messages.
/*!
  \param smsType: The type of SMS message that you will send by default.
  \param clientConfiguration: AWS client configuration.
  \return bool: Function succeeded.
 */
bool AwsDoc::SNS::setSMSType(const Aws::String &smsType,
                             const Aws::Client::ClientConfiguration &clientConfiguration) {
    Aws::SNS::SNSClient snsClient(clientConfiguration);

    Aws::SNS::Model::SetSMSAttributesRequest request;
    request.AddAttributes("DefaultSMSType", smsType);

    const Aws::SNS::Model::SetSMSAttributesOutcome outcome = snsClient.SetSMSAttributes(
            request);

    if (outcome.IsSuccess()) {
        std::cout << "SMS Type set successfully " << std::endl;
    }
    else {
        std::cerr << "Error while setting SMS Type: '"
                  << outcome.GetError().GetMessage()
                  << "'" << std::endl;
    }

    return outcome.IsSuccess();
}
```
+  API の詳細については、**「AWS SDK for C\+\+ API リファレンス」の「[SetSMSAttributes](https://docs.aws.amazon.com/goto/SdkForCpp/sns-2010-03-31/SetSMSAttributes)」を参照してください。

------
#### [ CLI ]

**AWS CLI**  
**SMS メッセージ属性を設定するには**  
次の `set-sms-attributes` の例では、SMS メッセージのデフォルトの送信者 ID を `MyName` に設定します。  

```
aws sns set-sms-attributes \
    --attributes {{DefaultSenderID=MyName}}
```
このコマンドでは何も出力されません。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[SetSMSAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/set-sms-attributes.html)」を参照してください。

------
#### [ Java ]

**SDK for Java 2.x**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns#code-examples)での設定と実行の方法を確認してください。

```
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);
        }
    }
}
```
+  API の詳細については、*AWS SDK for Java 2.x API リファレンス*の「[SetSMSAttributes](https://docs.aws.amazon.com/goto/SdkForJavaV2/sns-2010-03-31/SetSMSAttributes)」を参照してください。

------
#### [ JavaScript ]

**SDK for JavaScript (v3)**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/sns#code-examples)での設定と実行の方法を確認してください。
別のモジュールでクライアントを作成し、エクスポートします。  

```
import { SNSClient } from "@aws-sdk/client-sns";

// The AWS Region can be provided here using the `region` property. If you leave it blank
// the SDK will default to the region set in your AWS config.
export const snsClient = new SNSClient({});
```
SDK モジュールとクライアントモジュールをインポートし、API を呼び出します。  

```
import { SetSMSAttributesCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";

/**
 * @param {"Transactional" | "Promotional"} defaultSmsType
 */
export const setSmsType = async (defaultSmsType = "Transactional") => {
  const response = await snsClient.send(
    new SetSMSAttributesCommand({
      attributes: {
        // Promotional – (Default) Noncritical messages, such as marketing messages.
        // Transactional – Critical messages that support customer transactions,
        // such as one-time passcodes for multi-factor authentication.
        DefaultSMSType: defaultSmsType,
      },
    }),
  );
  console.log(response);
  // {
  //   '$metadata': {
  //     httpStatusCode: 200,
  //     requestId: '1885b977-2d7e-535e-8214-e44be727e265',
  //     extendedRequestId: undefined,
  //     cfId: undefined,
  //     attempts: 1,
  //     totalRetryDelay: 0
  //   }
  // }
  return response;
};
```
+  詳細については、「[AWS SDK for JavaScript デベロッパーガイド](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sns-examples-sending-sms.html#sending-sms-setattributes)」を参照してください。
+  API の詳細については、*AWS SDK for JavaScript API リファレンス*の「[SetSMSAttributes](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/sns/command/SetSMSAttributesCommand)」を参照してください。

------
#### [ PHP ]

**SDK for PHP**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/sns#code-examples)での設定と実行の方法を確認してください。

```
$SnSclient = new SnsClient([
    'profile' => 'default',
    'region' => 'us-east-1',
    'version' => '2010-03-31'
]);

try {
    $result = $SnSclient->SetSMSAttributes([
        'attributes' => [
            'DefaultSMSType' => 'Transactional',
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```
+  詳細については、「[AWS SDK for PHP デベロッパーガイド](https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/sns-examples-sending-sms.html#set-sms-attributes)」を参照してください。
+  API の詳細については、*AWS SDK for PHP API リファレンス*の「[SetSMSAttributes](https://docs.aws.amazon.com/goto/SdkForPHPV3/sns-2010-03-31/SetSMSAttributes)」を参照してください。

------

## 国に固有の配信のための SMS メッセージプリファレンスの設定
<a name="sms_preferences_country_specific"></a>

特定の国の送信先にのみメッセージを送信して、SMS トラフィックを管理および制御できます。これにより、メッセージは承認された国にのみ送信され、不要な SMS 料金を回避できます。次の手順により、Amazon Pinpoint の Protect 設定を使用して、許可またはブロックする国を指定します。

1. [https://console.aws.amazon.com/sms-voice/](https://console.aws.amazon.com/sms-voice/) で AWS SMS コンソールを開きます。

1. ナビゲーションペインの **[概要]** の **[クイックスタート]** セクションで、**[保護設定の作成]** を選択します。

1. **[保護設定の詳細]** で、保護設定の**わかりやすい名前**を入力します (例えば、「オーストラリアのみを許可」など）。

1. **[SMS に関する国ルール]** で、**[地域/国]** のチェックボックスをオンにして、サポートされているすべての国へのメッセージの送信をブロックします。

1. メッセージを送信する国のチェックボックスをオフにします。例えば、オーストラリアへのメッセージのみを許可するには、**[オーストラリア]** のチェックボックスをオフにします。

1. **[保護設定の関連付け]** セクションの **[関連付けタイプ]** で、**[アカウントのデフォルト]** を選択します。これにより、Protect AWS End User Messaging SMS 設定が Amazon SNS、[Amazon Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html)、Amazon Pinpoint [https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-sms.html](https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-sms.html) API コールを介して送信されるすべてのメッセージに影響するようになります。

1. **[保護設定を作成]** を選択して、設定を保存します。

   次の確認メッセージが表示されます。

   ```
   Success Protect configuration protect-abc0123456789 has been created.
   ```

1. [Amazon SNS コンソール](https://console.aws.amazon.com/sns/home)にサインインします。

1. [**メッセージを発行**](sns-publishing.md)して、例えばインドなど、ブロックされている国のいずれかに送信します。

   メッセージが配信されないことを確認します。これは、[CloudWatch](sms_stats_cloudwatch.md) を使用して、配信失敗ログで確認できます。ロググループ **sns/region/AccountID/DirectPublishToPhoneNumber/Failure** で、次の例のようなレスポンスを検索します。

   ```
   {
   "notification": {
   "messageId": "bd59a509-XXXX-XXXX-82f8-fbdb8cb68217",
   "timestamp": "YYYY-MM-DD XX:XX:XX.XXXX“
   },
   "delivery": {
   "destination": "+91XXXXXXXXXX",
   "smsType": "Transactional",
   "providerResponse": "Cannot deliver message to the specified destination country",
   "dwellTimeMs": 85
   },
   "status": "FAILURE"
   }
   ```