im currently trying to send a push notification from AWS Lambda but the following code is getting skipped in my function as i tried to output something within and its not showing. Please help me
const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();
const sns = new AWS.SNS();
AWS.config.region = 'ap-southeast-1'
exports.handler = async event => {
const params = {
TableName: "Items" // The name of your DynamoDB table
};
try {
// Utilising the scan method to get all items in the table
const data = await documentClient.scan(params).promise();
var allItems = data.Items;
allItems.forEach(async function (item){
const tempItem = item;
var endDate = new Date(tempItem.endDate);
var today = new Date();
today.setHours(0, 0, 0, 0);
var currentDaysLeft = endDate - today;
if (currentDaysLeft < 0)
{
currentDaysLeft = 0;
}
// convert JS date milliseconds to number of days
currentDaysLeft = Math.ceil(currentDaysLeft / (1000 * 60 * 60 * 24));
if (tempItem.durationLeft != currentDaysLeft){
tempItem.durationLeft = currentDaysLeft;
}
const message = tempItem.username + "'s " + tempItem.title + " is expiring in " + tempItem.durationLeft + " days." ;
const msgparams = {
Message: message,
TopicArn: 'arn:aws:sns:ap-southeast-1:xxxxxxxxx:xxxxx'
};
var publishPromise = sns.publish(msgparams).promise();
});
return;
} catch (e) {
return {
body: e
};
}
};
I have tested that the for each loop runs correctly based on the number of items returned and that the data from the scan is all indeed correct, but the publish part is not running
this is my code for firebase messaging service
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
messageBody = message.GetNotification().Body;
}
// NOTE: test messages sent via the Azure portal or AWS SNS portal will be received here
else
{
messageBody = message.Data.Values.First();
}
// convert the incoming message to a local notification
SendLocalNotification(messageBody);
}
You can return all the sns publish promise and collect them in an array and use await on all the promise array.
const publishPromises = allItems.map(function (item){
......
return sns.publish(msgparams).promise();
});
await Promise.all(publishPromises);