【发布时间】:2021-10-20 05:12:26
【问题描述】:
我有一个云功能,它从数据库中读取一些数据并在每天上午 10:00 向用户发送推送通知...
这是函数:
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
exports.qFunction = functions.region('europe-west3').pubsub.schedule('0 10 * * *').timeZone("Europe/Berlin").onRun((context) => {
admin
.firestore()
.collection('users')
.doc('hb')
.get()
.then((querySnapshot) => {
var token = querySnapshot.data().tokens
console.log(`Found user to: ${token}`)
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
day += 1;
admin
.firestore()
.collection('quotes')
.doc(day.toString())
.get()
.then((querySnapshot) => {
const payload = {
notification: {
title: "HB",
body: querySnapshot.data().quote,
badge: '1',
sound: 'default'
}
}
admin
.messaging()
.sendToDevice(token, payload)
.then(response => {
console.log('Successfully sent message:', response)
})
.catch(error => {
console.log('Error sending message:', error)
})
})
})
return null;
});
但是,它有时会发送通知,有时不会。
比如前两天发送通知成功:
而今天它什么也没做:
这可能是什么原因?
因为我只有一个用户。可能是由于云功能冷启动吗?如果是,如何预防?
【问题讨论】:
-
嗨弗兰克。对不起我忘记了。但你的回答很完美!
标签: node.js google-cloud-functions firebase-cloud-messaging