简体   繁体   中英

AWS SNS - Push to GCM

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.

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.\\\" } }\" }";

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM