简体   繁体   English

在 AWS Lambda 上发出 https 请求

[英]Making https requests on AWS Lambda

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.最近发现了 AWS 并且我做得很好,今天我想向我的 iPhone X 发送一个测试通知。我正在尝试在我的数据库更新时这样做,使用 dynamoDB 触发器蓝图。

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.这是我到目前为止所尝试的,我既没有在 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();
};

I do not get the message on my iPhone.我的 iPhone 上没有收到消息。 What seems to be the problem here?这里似乎有什么问题?

But getting:但是得到:

Activation change detected.检测到激活变化。 message sent发送的消息

on the console.在控制台上。

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. HTTP请求是一个异步操作,这意味着您需要等待响应,但在您的情况下,您只是在调用该函数后从处理程序返回。 In order to fix this you need to wait for http request to finish before returning from handler. 为了解决这个问题,你需要在从处理程序返回之前等待http请求完成。 The following method assumes that you are using nodejs v8.x. 以下方法假定您使用的是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM