Amazon Simple Notification Service
API Reference (API Version 2010-03-31)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Did this page help you?  Yes | No |  Tell us about it...

Publish

Description

Sends a message to all of a topic's subscribed endpoints. When a messageId is returned, the message has been saved and Amazon SNS will attempt to deliver it to the topic's subscribers shortly. The format of the outgoing message to each subscribed endpoint depends on the notification protocol selected.

To use the Publish action for sending a message to a mobile endpoint, such as an app on a Kindle device or mobile phone, you must specify the EndpointArn. The EndpointArn is returned when making a call with the CreatePlatformEndpoint action. The second example below shows a request and response for publishing to a mobile endpoint.

Request Parameters

For information about the common parameters that all actions use, see Common Parameters.

Message

The message you want to send to the topic.

If you want to send the same message to all transport protocols, include the text of the message as a String value.

If you want to send different messages for each transport protocol, set the value of the MessageStructure parameter to json and use a JSON object for the Message parameter. See the Examples section for the format of the JSON object.

Constraints: Messages must be UTF-8 encoded strings at most 256 KB in size (262144 bytes, not 262144 characters).

JSON-specific constraints:

  • Keys in the JSON object that correspond to supported transport protocols must have simple JSON string values.
  • The values will be parsed (unescaped) before they are used in outgoing messages.
  • Outbound notifications are JSON encoded (meaning that the characters will be reescaped for sending).
  • Values have a minimum length of 0 (the empty string, "", is allowed).
  • Values have a maximum length bounded by the overall message size (so, including multiple protocols may limit message sizes).
  • Non-string values will cause the key to be ignored.
  • Keys that do not correspond to supported transport protocols are ignored.
  • Duplicate keys are not allowed.
  • Failure to parse or validate any key or value in the message will cause the Publish call to return an error (no partial delivery).

Type: String

Required: Yes

MessageStructure

Set MessageStructure to json if you want to send a different message for each protocol. For example, using one publish action, you can send a short message to your SMS subscribers and a longer message to your email subscribers. If you set MessageStructure to json, the value of the Message parameter must:

  • be a syntactically valid JSON object; and
  • contain at least a top-level JSON key of "default" with a value that is a string.

You can define other top-level keys that define the message you want to send to a specific transport protocol (e.g., "http").

For information about sending different messages for each protocol using the AWS Management Console, go to Create Different Messages for Each Protocol in the Amazon Simple Notification Service Getting Started Guide.

Valid value: json

Type: String

Required: No

Subject

Optional parameter to be used as the "Subject" line when the message is delivered to email endpoints. This field will also be included, if present, in the standard JSON messages delivered to other endpoints.

Constraints: Subjects must be ASCII text that begins with a letter, number, or punctuation mark; must not include line breaks or control characters; and must be less than 100 characters long.

Type: String

Required: No

TargetArn

Either TopicArn or EndpointArn, but not both.

Type: String

Required: No

TopicArn

The topic you want to publish to.

Type: String

Required: No

Response Elements

The following element is returned in a structure named PublishResult.

MessageId

Unique identifier assigned to the published message.

Length Constraint: Maximum 100 characters

Type: String

Errors

For information about the errors that are common to all actions, see Common Errors.

AuthorizationError

Indicates that the user has been denied access to the requested resource.

HTTP Status Code: 403

EndpointDisabled

Exception error indicating endpoint disabled.

HTTP Status Code: 400

InternalError

Indicates an internal service error.

HTTP Status Code: 500

InvalidParameter

Indicates that a request parameter does not comply with the associated constraints.

HTTP Status Code: 400

NotFound

Indicates that the requested resource does not exist.

HTTP Status Code: 404

PlatformApplicationDisabled

Exception error indicating platform application disabled.

HTTP Status Code: 400

Examples

Sample Request

        
  The following example publishes the same message to all protocols:
  
  http://sns.us-east-1.amazonaws.com/
  ?Subject=My%20first%20message
  &TopicArn=arn%3Aaws%3Asns%3Aus-east-1%3A698519295917%3AMy-Topic
  &Message=Hello%20world%21
  &Action=Publish
  &SignatureVersion=2
  &SignatureMethod=HmacSHA256
  &Timestamp=2010-03-31T12%3A00%3A00.000Z
  &AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE
  &Signature=9GZysQ4Jpnz%2BHklqM7VFTvEcjR2LIUtn6jW47054xxE%3D
  
  Use the following JSON object format for the Message parameter to send different messages to each protocol (linebreaks added for readability):
  {
  "default" : "some message",
  "email" : "some email message",
  "email-json" : "some email-json message",
  "http" : "some http message",
  "https" : "some https message",
  "sqs" : "some sqs message"
  }
  

       
        
POST http://sns.us-west-2.amazonaws.com/ HTTP/1.1
...
Action=Publish
&Message=%7B%22default%22%3A%22This+is+the+default+Message%22%2C%22APNS_SANDBOX%22%3A%22%7B+%5C%22aps%5C%22+%3A+%7B+%5C%22alert%5C%22+%3A+%5C%22You+have+got+email.%5C%22%2C+%5C%22badge%5C%22+%3A+9%2C%5C%22sound%5C%22+%3A%5C%22default%5C%22%7D%7D%22%7D
&TargetArn=arn%3Aaws%3Asns%3Aus-west-2%3A803981987763%3Aendpoint%2FAPNS_SANDBOX%2Fpushapp%2F98e9ced9-f136-3893-9d60-776547eafebb
&SignatureMethod=HmacSHA256
&AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE
&SignatureVersion=2
&Version=2010-03-31
&Signature=vmqc4XRupKAxsDAdN4j4Ayw5LQljXMps3kss4bkDfCk%3D
&Timestamp=2013-07-18T22%3A44%3A09.452Z
&MessageStructure=json
  

      

Sample Response

        
<PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
  <PublishResult>
    <MessageId>94f20ce6-13c5-43a0-9a9e-ca52d816e90b</MessageId>
  </PublishResult>
  <ResponseMetadata>
    <RequestId>f187a3c1-376f-11df-8963-01868b7c937a</RequestId>
  </ResponseMetadata>
</PublishResponse>
  
       
        
HTTP/1.1 200 OK
...
<PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
  <PublishResult>
    <MessageId>567910cd-659e-55d4-8ccb-5aaf14679dc0</MessageId>
  </PublishResult>
  <ResponseMetadata>
    <RequestId>d74b8436-ae13-5ab4-a9ff-ce54dfea72a0</RequestId>
  </ResponseMetadata>
</PublishResponse>