Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Subscribe a mobile application to an Amazon SNS topic using an Amazon SDK
The following code examples show how to subscribe a mobile application endpoint so it receives notifications from an Amazon SNS topic.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code examples:
- C++
-
- SDK for C++
-
//! Subscribe to an Amazon Simple Notification Service (Amazon SNS) topic with delivery to a mobile app.
/*!
\param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic.
\param endpointARN: The ARN for a mobile app or device endpoint.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool
AwsDoc::SNS::subscribeApp(const Aws::String &topicARN,
const Aws::String &endpointARN,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::SNS::SNSClient snsClient(clientConfiguration);
Aws::SNS::Model::SubscribeRequest request;
request.SetTopicArn(topicARN);
request.SetProtocol("application");
request.SetEndpoint(endpointARN);
const Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request);
if (outcome.IsSuccess()) {
std::cout << "Subscribed successfully." << std::endl;
std::cout << "Subscription ARN '" << outcome.GetResult().GetSubscriptionArn()
<< "'." << std::endl;
}
else {
std::cerr << "Error while subscribing " << outcome.GetError().GetMessage()
<< std::endl;
}
return outcome.IsSuccess();
}
- JavaScript
-
- SDK for JavaScript (v3)
-
Create the client in a separate module and export it.
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({});
Import the SDK and client modules and call the API.
import { SubscribeCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";
/**
* @param {string} topicArn - The ARN of the topic the subscriber is subscribing to.
* @param {string} endpoint - The Endpoint ARN of an application. This endpoint is created
* when an application registers for notifications.
*/
export const subscribeApp = async (
topicArn = "TOPIC_ARN",
endpoint = "ENDPOINT",
) => {
const response = await snsClient.send(
new SubscribeCommand({
Protocol: "application",
TopicArn: topicArn,
Endpoint: endpoint,
}),
);
console.log(response);
// {
// '$metadata': {
// httpStatusCode: 200,
// requestId: 'c8e35bcd-b3c0-5940-9f66-06f6fcc108f0',
// extendedRequestId: undefined,
// cfId: undefined,
// attempts: 1,
// totalRetryDelay: 0
// },
// SubscriptionArn: 'pending confirmation'
// }
return response;
};
For a complete list of Amazon SDK developer guides and code examples, see
Using Amazon SNS with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.