2 amazon-web-services node.js 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();
};
Run Code Online (Sandbox Code Playgroud)
我的 iPhone 上没有收到消息。这里似乎有什么问题?
但是得到:
检测到激活变化。发送的消息
在控制台上。
小智 5
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();
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2150 次 |
| 最近记录: |