8
votes

In Java, I am trying to publish a AWS SNS message to a specific ARN endpoint using the following code:

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;

...

AmazonSNS snsClient = new AmazonSNSClient(new BasicAWSCredentials(System.getenv("AWS_KEY"), System.getenv("AWS_SECRET")));

snsClient.setRegion(Region.getRegion(Regions.US_EAST_1));

String message = "{\"APNS_SANDBOX\":\"{\\\"aps\\\":{\\\"type\\\":\\\"XXX\\\",\\\"email\\\":\\\"[email protected]\\\",\\\"alert\\\":\\\"some alert\\\"}}\"}";

PublishResult pr = snsClient.publish(new PublishRequest("arn:aws:sns:us-east-1:XXX:endpoint/APNS_SANDBOX/XXX/XXX-XXX-XXX-XXX", message));

I am consistently getting the following error message:

Invalid parameter: Topic Name (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: XXX

Any idea on this?

I can publish to the ARN endpoint with no issues from the SNS console and I have tried different variations of the message

3

3 Answers

9
votes

You should use TargetArn instead of TopicArn which is the first parameter of PublishRequest constructor.

PublishRequest pr = new PublishRequest();
pr.TargetArn = "arn:aws:sns:us-east-1:XXX:endpoint/APNS_SANDBOX/XXX/XXX-XXX-XXX-XXX";
pr.Message = message;

(maybe it is too late to answer lol)

6
votes

The error message is a bit confusing. If you provide the wrong topic ARN, you get error message saying Invalid Parameter: Topic Name as well.

From what you posted, it looks like you might have used the subscription ARN, instead of topic ARN. Topic ARN format looks like this arn:aws:sns:AWS_REGION:AWS_ACCOUNT_NUMBER:TOPIC_NAME.

1
votes

Your problem was this: String message = "{"APNS_SANDBOX":"{\"aps\":{\"type\":\"XXX\",\"email\":\"[email protected]\",\"alert\":\"some alert\"}}"}";

instead it should have been: String message = "{"APNS_SANDBOX":"{\"aps\":{\"type\":\"XXX\",\"email\":\"[email protected]\",\"alert\":\"somealert\"}}"}";

You cant put name of the topic with spaces. So "some alert" will be rejected. "somealert" will be accepted.

In case someone else stumbles upon this problem.