我们正在尝试在无服务器环境(AWS Lambda)中将Firebase Cloud Messaging(FCM)与node.js一起使用。以下是我们使用SDK向设备发送推送的简单功能。
FCM登录后,一切似乎正常。但是,调用此函数后,node.js进程不会自动关闭。这意味着我们需要为在无服务器环境中花费的额外时间付费。
我们可以使用firebaseApp.delete()
来强制关闭应用程序,但这似乎过多。什么是使node.js进程自行停止的正确方法?
var admin = require("firebase-admin")
// authorize for Firebase with private key cleaned up to avoid PEM formatting error
let firebaseApp = admin.initializeApp({
databaseURL: process.env.GCLOUD_FIREBASE_DB_URL,
credential: admin.credential.cert({
projectId: process.env.GCLOUD_ADMIN_PROJECT_ID,
clientEmail: process.env.GCLOUD_ADMIN_CLIENT_EMAIL,
privateKey: process.env.GCLOUD_ADMIN_PRIVATE_KEY.replace(/\\n/g, "\n"),
})
})
async function sendPush(options) {
let msg = {
notification: {body: options.body, title: options.title},
token: options.token
}
const dryRun = true
firebaseApp.messaging().send(msg, dryRun)
.then((msgId) => {
// Response is a message ID string.
console.log(`Successfully sent a push to ${msgId}.`)
return msgId
})
// No catch as errors are expected to be handled outside this function
}
module.exports = sendPush