2016-07-31 100 views
0

我的lambda函數有問題。它獲取設備的數據併發送推送通知(某人將你添加到朋友的信息)。但是,經常發生推送通知未被髮送。有時,我必須激活幾次功能(通過同時點擊按鈕),才能發送推送通知。在Lambda中測試下面的函數時,它不會發送任何推送通知。什麼可能是這個問題?通過lambda發送SNS推送 - 沒有消息?

謝謝你在前進, 約翰

console.log("Loading friend-request function"); 
var AWS = require("aws-sdk"); 

exports.handler = function(event, context) { 

    var senderID = event.senderID; 
    var receiverID = event.receiverID; 
    var message = event.message; 

    var eventText = JSON.stringify(event); 
    console.log("Received event:", eventText); 
    var sns = new AWS.SNS(); 
    var params = { 
     Message: message, 
     Subject: "Test SNS From Lambda", 
     TargetArn: receiverID 
    }; 

    context.succeed(message); 
    sns.publish(params, context.done); 

}; 

回答

2

記得sns.publish()功能是異步的,即你必須等待它完成,你可以撥打context.succeed()之前。否則,Lambda函數可能會在消息發佈之前終止。

exports.handler = function(event, context) { 
    // same as before... 

    var params = {...} 

    // do not call context.succeed() yet 

    sns.publish(params, function(err, data) { 
     if (err) { 
      console.log('Failed to publish SNS message'); 
      context.fail(err); 
     } 
     else { 
      console.log('SNS message published successfully'); 
      context.succeed(data); 
     } 
    }); 
}; 
+0

This Works,thank you! :) –