简体   繁体   English

AWS SNS收到多个通知

[英]AWS SNS Multiple notifications received

I have an API Gateway method calling a Lambda Node.js function. 我有一个调用Lambda Node.js函数的API网关方法。 The Lambda function calls SNS and posts an APNS notification to my iPhone. Lambda函数调用SNS并将APNS通知发布到我的iPhone。 When I invoke the API gateway or the Lambda function in the AWS console, I get one notification as expected. 当我在AWS控制台中调用API网关或Lambda函数时,收到了一条预期的通知。 I also get one notification when running the Lambda code on the command line (Grunt and Node.js). 在命令行上运行Lambda代码(Grunt和Node.js)时,我还会收到一条通知。 I also get one notification when running the javascript from eclipse. 从eclipse运行javascript时,我还会收到一条通知。

However, when I POST to the API gateway, I get 2-5 notifications. 但是,当我发布到API网关时,会收到2-5条通知。 Every thing looks the same. 每件事看起来都一样。 I checked the Cloudwatch logs and it seems only one request is sent each time. 我检查了Cloudwatch日志,似乎每次都只发送一个请求。 Anybody have any idea how to debug this? 有人知道如何调试吗?

I've had similar. 我也有类似的经历。 For me, it was that I wasn't calling the success callback properly. 对我来说,那是因为我没有正确调用成功回调。

I figured it out. 我想到了。 I had my function outside the exports.handler function: 我在exports.handler函数之外有函数:

var AWS = require('aws-sdk');
var sns = new AWS.SNS();

var myAlerter = function(){

    var numSent = 0;
    var callback;
    var arn = "arn:aws:sns:us-west-2:45435475457:endpoint/APNS/MyAlerter/5a11c61f-1122-3344-5566-656845463";

    var sendNotification = function(messageText){

            var apns = {
                    aps : {
                        alert : messageText,
                        sound : 'default'
                    }
            };
            var message = {
                    "APNS" : JSON.stringify(apns)
            };
            message = JSON.stringify(message);

            var params = {
                    Message: message, 
                    MessageStructure: 'json',
                    TargetArn: arn
            };

            numSent++;
            sns.publish(params, function(err, data){
                if (err){
                    callback(err, err.stack);
                }else {
                    var result = {
                            error: false,
                            numSent : numSent,
                            data: data
                    };              

                    callback(false,result);
                }
            });            
    };

    return {

        alert : function(message, cb){
            callback = cb;
            sendNotification(message);
        }

    }

}();

exports.handler = function(event, context){

    var alertedCallback = function(error, data){
        if (error){
            context.done(error);
        } else {
            context.succeed(data);
        }
    };          

    myAlerter.alert(event.message, alertedCallback);

};

Everytime I called the API Gateway and invoked my Lambda function, the numSent variable would increment. 每当我调用API网关并调用Lambda函数时,numSent变量都会增加。 I guess putting my function inside the exports.handler ensured that my function wasn't global or something. 我想把我的函数放到exports.handler中可以确保我的函数不是全局的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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