在 AWS Lambda 上发出 https 请求

Making https requests on AWS Lambda

最近发现了 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 上没有收到消息。这似乎是什么问题?

但是得到:

Activation change detected. message sent

在控制台上。

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