在AWS Lambda上发出https请求

时间:2018-12-21 17:45:22

标签: node.js amazon-web-services aws-lambda onesignal

最近发现AWS,我做得很好,今天我想向我的iPhone X发送测试通知。我正在尝试在数据库得到更新时使用dynamoDB触发蓝图。

仪表板上传递的常规通知有效

这是到目前为止我一直在尝试的操作,我既没有在CloudWatch上获得代表控制台日志,也没有出现错误。

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();
};

我没有在iPhone上收到消息。这里似乎是什么问题?

但是得到:

  

检测到激活更改。邮件已发送

在控制台上。

2 个答案:

答案 0 :(得分:1)

HTTP请求是一个异步操作,这意味着您需要等待响应,但在这种情况下,您只是在调用函数后从处理程序中返回。 为了解决这个问题,您需要等待http请求完成才能从处理程序返回。以下方法假定您使用的是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();
  });
}

答案 1 :(得分:0)

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function (body) {
    console.log('Body: ' + body);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.write(data);
req.end();