1

I am attempting to use AWS SNS for push notifications for my app. I have successfully setup registation of individual endpoints ARN's using user info and regsitration ID.

I can send an individual message via the console fine, however I can't seem to figure out how to send it programmatically (JAVA)

Console way: (Working)

http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-directmobile.html

Attempted way via JAVA:

private void publishToSNSEndpoint(String username) {

        // Find an entry of a users SNS registration and Endpoint ARN
        SNSPush pushConfig = snsPushService.findByUsername(username);

        //Get ARN to String
        String endpointARN = pushConfig.getSNSEnpointARN();

        //Generate SNS Push to user
        String message = "{\"title\":\"Test_Title\",\"description\":\"Test_Description\"}";
        PublishRequest publishRequest = new PublishRequest();
        publishRequest.setMessage(message);
        publishRequest.setTargetArn(endpointARN);
        PublishResult publish = client.publish(publishRequest);

        //print MessageId of message published to SNS topic
        System.out.println("MessageId - " + publish.getMessageId());
    }

This currently produces an error of the following:

Using EndpointARN for use (Confirmed valid) Invalid parameter: TargetArn Reason: No endpoint found for the target arn specified (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter;

Using SNS Application ARN (Triple checked this is valid)

Invalid parameter: TargetArn Reason: arn:aws:sns:ap-southeast-xxxxxxxxx-xxxx is not valid to publish to (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter;

There is some documentation here. But it seems pretty old and doesn't work anymore.

My question is: How can I send Push notifications to an individual EndpointARN in AWS SNS programmatically using Java and the AWS SDK.

Yonkee
  • 1,781
  • 3
  • 31
  • 56
  • I've encountered a similar error somewhat recently. I had to **set the region** associated with my endpoint for it to be reachable by your connection. Have done explicitly set it? – ccjmne Jan 01 '17 at 20:43
  • Yes, I did that earlier on during endPoint creation. Set the SNSClient object to my region. Thanks for the suggestions though. – Yonkee Jan 01 '17 at 20:47

3 Answers3

0

I just worked it out for anyone who comes across the same issue.

Message formatting is a nightmare and different for each platform.

I had to add this.

publishRequest.setMessageStructure("json");

This is for GCM - Android

String gcmMessage = "{ \"GCM\": \"{ \\\"data\\\": { \\\"message\\\": \\\"Hi, AWS. Sort out your JSON parsing.\\\" } }\" }";
Yonkee
  • 1,781
  • 3
  • 31
  • 56
0

You need to set your region as well. Be careful, as if you are using some other AWS services, they may have different region. so I would recommend to specify the region for your SNS even if you have it globally.

e.g. my EC2 instance region was different than my SNS region and I was pushing notification based on the config I had for EC2.

Kevin
  • 911
  • 1
  • 9
  • 6
0

If you are using the CLI and facing this error, make sure your region and message are correct.

aws sns publish \
    --target-arn "arn:aws:sns:us-east-1:xxxxxxxxx:endpoint/GCM/my_sns_application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
    --message-structure "json" \
    --message '{"GCM":"{\"notification\":{\"title\":\"test push\",\"body\":\"test body\"}}"}' \
    --region us-east-1
secretshardul
  • 1,635
  • 19
  • 31