Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish sns to a specific endpoint?

I have a issue with publishing sns to a specific endpoint.

My code:

var AWS = require('aws-sdk');
AWS.config.loadFromPath('/web/config.json');

var sns = new AWS.SNS();
sns.publish({
    // TopicArn:'arn:aws:sns:us-west-2:302467918846:MyTestTopik',
    TargetArn: 'arn:aws:sns:us-west-2:302467918846:MyTestTopik:613ee49c-d4dc-4354-a7e6-c1d9d8277c56',
    Message: "Success!!! ",
    Subject: "TestSNS"
}, function(err, data) {
    if (err) {
        console.log("Error sending a message " + err);
    } else {
        console.log("Sent message: " + data.MessageId);

    }
});

When I use TopicArn, everything is fine. But when I try to send notification to a specific endpoint I take error:

Error sending a message InvalidParameter: Invalid parameter: Topic Name

And I have no idea what kind of parameters it is and from where.

like image 366
Alex Cebotari Avatar asked Oct 14 '13 13:10

Alex Cebotari


People also ask

How do I publish a SNS event?

Sign in to the Amazon SNS console . In the left navigation pane, choose Topics. On the Topics page, select a topic, and then choose Publish message. The console opens the Publish message to topic page.

Can you publish to a SNS topic in a different region?

Yes, of course. When you're using the SDK, just set the region to us-east-1 . Nothing is stopping you from sending messages to an SNS topic in another region.

Can VPC be an endpoint for SNS?

You can create an Amazon SNS endpoint in your VPC using the AWS Management Console, the AWS CLI, an AWS SDK, the Amazon SNS API, or AWS CloudFormation. For information about creating and configuring an endpoint using the Amazon VPC console or the AWS CLI, see Creating an Interface Endpoint in the Amazon VPC User Guide.

What kind of message does SNS send to endpoint?

When Amazon SNS sends a notification to a subscribed HTTP or HTTPS endpoint, the POST message sent to the endpoint has a message body that contains a JSON document with the following name-value pairs. The Message value specified when the notification was published to the topic.


2 Answers

Something similar is working fine for me. I'm able to publish to a specific endpoint using: Apple Push Notification Service Sandbox (APNS_SANDBOX)

You might also want to try and update the aws-sdk, current version is 1.9.0.

Here's my code, TargetArn was copied directly from the SNS console. I omitted some of the data, like &

var sns = new AWS.SNS();
var params = {
    TargetArn:'arn:aws:sns:us-west-2:302467918846:endpoint/APNS_SANDBOX/<APP_NAME>/<USER_TOKEN>'
    Message:'Success!!! ',
    Subject: 'TestSNS'
};

sns.publish(params, function(err,data){
        if (err) {
            console.log('Error sending a message', err);
        } else {
            console.log('Sent message:', data.MessageId);
        }
    });
like image 142
guya Avatar answered Oct 17 '22 15:10

guya


You might have an invalid Region. Check you Region for the Topic and set it accordingly. For example if you are us-west-2 you could do something like

var sns = new aws.SNS({region:'us-west-2'});
like image 21
Click Ahead Avatar answered Oct 17 '22 17:10

Click Ahead