I have developed a push notification engine in AWS Lambda, using One Signal over https, and it is working just fine. I can trigger notifications based on incoming IoT rules and send them off in near real time.
async function sendNotification(data, api_key) {
console.log("Data to POST : " , data);
const response = await new Promise((resolve, reject) => {
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": api_key
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
const req = https.request(options, function(res){
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (data) => {
var buff = Buffer.from(data, 'utf8');
console.log('data received: ', buff.toString());
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(JSON.stringify(data));
req.end();
return;
});
return response;
}
The condition to cause this code to run can trigger many times per day. The problem is that I need to only send notifications up to a maximum of once a day in some cases, and sometimes only if a previous notification has been sent to that user. My issue I suppose is where is the best place to store state?
Is there is a way of scheduling the notifications in an idempotent way so that rather than send multiple, subsequent calls would overwrite each other, and result in just one call? Could SNS be used somehow?
Another thought was using device shadows. Is this a possible solution where the state could be stored there and used to ensure once and only once delivery of a first notification when needed and escalate to send more serious notifications, when required?