【问题标题】:AWS SNS Multiple notifications receivedAWS SNS 收到多个通知
【发布时间】:2016-09-07 15:51:27
【问题描述】:

我有一个调用 Lambda Node.js 函数的 API Gateway 方法。 Lambda 函数调用 SNS 并将 APNS 通知发布到我的 iPhone。当我在 AWS 控制台中调用 API 网关或 Lambda 函数时,我收到了一个预期的通知。在命令行(Grunt 和 Node.js)上运行 Lambda 代码时,我也会收到一条通知。从 Eclipse 运行 javascript 时,我也会收到一条通知。

但是,当我 POST 到 API 网关时,我会收到 2-5 条通知。每件事看起来都一样。我检查了 Cloudwatch 日志,似乎每次只发送一个请求。有人知道如何调试吗?

【问题讨论】:

  • “POST”是什么意思?

标签: ios node.js amazon-web-services lambda aws-api-gateway


【解决方案1】:

我也遇到过类似情况。对我来说,是我没有正确调用成功回调。

【讨论】:

    【解决方案2】:

    我想通了。我的函数在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);
    
    };
    

    每次我调用 API Gateway 并调用我的 Lambda 函数时,numSent 变量都会增加。我想把我的函数放在exports.handler中可以确保我的函数不是全局的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2016-07-12
      相关资源
      最近更新 更多