使用 Amazon 软件开发工具包确认终端节点拥有者想要接收 Amazon SNS 消息
以下代码示例展示如何通过验证通过较早的订阅操作发送到终端节点的令牌来确认终端节点的拥有者想要接收 Amazon SNS 消息。
- Java
-
- SDK for Java 2.x
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 public static void confirmSub(SnsClient snsClient, String subscriptionToken, String topicArn ) { try { ConfirmSubscriptionRequest request = ConfirmSubscriptionRequest.builder() .token(subscriptionToken) .topicArn(topicArn) .build(); ConfirmSubscriptionResponse result = snsClient.confirmSubscription(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription Arn: \n\n" + result.subscriptionArn()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考中的 ConfirmSubscription。
-
- JavaScript
-
- SDK for JavaScript V3
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 在单独的模块中创建客户端并将其导出。
import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };
导入软件开发工具包和客户端模块,然后调用 API。
// Import required AWS SDK clients and commands for Node.js import {ConfirmSubscriptionCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Token: "TOKEN", // Required. Token sent to the endpoint by an earlier Subscribe action. */ TopicArn: "TOPIC_ARN", // Required AuthenticateOnUnsubscribe: "true", // 'true' or 'false' }; const run = async () => { try { const data = await snsClient.send(new ConfirmSubscriptionCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
-
有关更多信息,请参阅 Amazon SDK for JavaScript 开发人员指南。
-
有关 API 详细信息,请参阅 Amazon SDK for JavaScript API 参考中的 ConfirmSubscription。
-
- PHP
-
- SDK for PHP
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException; /** * Verifies an endpoint owner's intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action. * * 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' ]); $subscription_token = 'arn:aws:sns:us-east-1:111122223333:MyTopic:123456-abcd-12ab-1234-12ba3dc1234a'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->confirmSubscription([ 'Token' => $subscription_token, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
-
有关 API 详细信息,请参阅 Amazon SDK for PHP API 参考中的 ConfirmSubscription。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon SNS 与 Amazon 开发工具包结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。
检查电话号码是否选择不接收消息
创建主题