2

Recently discovered AWS and i'm doing great, today i want to send a test notification to my iPhone X. I'm trying to do so when my database get's an update, Using the dynamoDB trigger blueprint.

Regular Notifications Delivered on the Dashboard works

This is what i have tried to so far, I'm neither getting the rep console log on CloudWatch nor an error.

console.log('Loading function');
const async = require('async');
const https = require('https');

exports.handler = async (event, context) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    const name = "35b83a10-9f46-4c2c-95e1-22c6d40005a8"; 

    var message = { 
      app_id: "appid",
      contents: {"en": "Your order has arrived at your doorstep"},
      include_player_ids: ["14894201-64f7-486a-b65e-6beedf5880f1",name,"8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"]
     };

    sendNotification(message);

    console.log("Activation change detected. message sent");
    return `Successfully processed ${event.Records.length} records.`;
};


var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic hidden_in_question"
  };

  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };

  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("rep:");
      console.log(JSON.parse(data));
    });
  });

  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });

  req.write(JSON.stringify(data));
  req.end();
};

I do not get the message on my iPhone. What seems to be the problem here?

But getting:

Activation change detected. message sent

on the console.

1 Answer 1

5

HTTP request is an async action which means you need to wait for the response, but in your case you are returning from the handler just after calling the function. In order to fix this you need to wait for http request to finish before returning from handler. The following method assumes that you are using nodejs v8.x.

const https = require('https');

exports.handler = async (event, context) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    const name = "35b83a10-9f46-4c2c-95e1-22c6d40005a8"; 

    var message = { 
      app_id: "appid",
      contents: {"en": "Your order has arrived at your doorstep"},
      include_player_ids: ["14894201-64f7-486a-b65e-6beedf5880f1",name,"8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"]
     };

    await sendNotification(message);

    console.log("Activation change detected. message sent");
    return `Successfully processed ${event.Records.length} records.`;
};


var sendNotification = function(data) {
  return new Promise(function(resolve, reject) {
      var headers = {
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": "Basic hidden_in_question"
      };

      var options = {
        host: "onesignal.com",
        port: 443,
        path: "/api/v1/notifications",
        method: "POST",
        headers: headers
      };

      var req = https.request(options, function(res) {  
        res.on('data', function(data) {
          console.log("rep:");
          console.log(JSON.parse(data));
        });
        res.on('end', resolve);
      });

      req.on('error', function(e) {
        console.log("ERROR:");
        console.log(e);
        reject(e);
      });

      req.write(JSON.stringify(data));
      req.end();
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.