Jim*_*Jim 3 amazon-web-services firebase aws-lambda firebase-cloud-messaging firebase-admin
我使用 Lambda 到 Firebase 消息。我参考这个。但 lambda 函数仍然超时,因为它无法连接到谷歌服务器。
处理程序.js
/ [START imports]
const firebase = require('firebase-admin');
const serviceAccount = require("../serviceAccount.json");
module.exports.message = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const registrationToken = "xxxxxxx";
const payload = {
data: {
score: "850",
time: "2:45"
}
};
// [START initialize]
if(firebase.apps.length == 0) { // <---Important!!! In lambda, it will cause double initialization.
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: 'https://messaging-xxxxx.firebaseio.com'
});
}
// Send a message to the device corresponding to the provided
// registration token.
firebase.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
console.log("Successfully sent message:", response);
callback(null, {
statusCode: 200,
body: JSON.stringify("Successful!"),
});
})
.catch(function(error) {
console.log("Error sending message:", error);
callback(null, {
statusCode: 500,
body: JSON.stringify({
"status": "error",
"message": error
})
})
});
};
Run Code Online (Sandbox Code Playgroud)
云观察
[错误:通过“credential”属性提供给initializeApp()的凭证实现无法获取有效的Google OAuth2访问令牌,并出现以下错误:“connect ETIMEDOUT 172.217.26.45:443”。]
但我使用相同的 serviceAccount.json 在我的 ec2 上运行并工作查找。有人遇到这个吗?
| 归档时间: |
|
| 查看次数: |
5323 次 |
| 最近记录: |