【问题标题】:Send push notification after X minutes using Firebase Functions使用 Firebase 函数在 X 分钟后发送推送通知
【发布时间】:2018-12-29 21:27:13
【问题描述】:
【问题讨论】:
标签:
android
ios
firebase
google-app-engine
push-notification
【解决方案1】:
这是我为解决通知调度问题所做的工作。我允许 5 分钟的容差(即,如果您想在 10:03 发送通知,那么它将在 10:05 发送)。
假设您有一个函数 sendNotificationFunction(userId, notificationMessage) ,它以 userId 和 notificationMessage 作为参数并向该特定用户发送通知。
-
在 firebase 实时数据库中,我创建了一个节点,其中包含有关通知计划的信息:
scheduleNotification: {
<pushId>: {
"userId":<userId>,
"scheduledTimestamp":<1530000000000>,
"notificationMessage":<Message that you want to send>
}
}
-
制作每 5 分钟触发一次的函数,并检查要安排哪些通知。
exports.every5MinTrigger = functions.https.onRequest((req,res)=>{
let currentTime = new Date().getTime(); //Say 10:00
let startTime = currentTime; //10:00
let endTime = currentTime + 5*60*1000; //10:05
firebase.database.ref().child("scheduleNotification").orderByChild("scheduledTimestamp").once('value').then((snap)=>{
if(snap.exists()){
snap.forEach(childSnap=>{
let userId = childSnap.child('userId').val()
let notificationMessage = childSnap.child('notificationMessage').val()
//Now you have userId and your notification's language. Call your sendNotificationFunction() Here
})
}
})
})
在您的 Firebase 云函数中部署此函数。你会得到这个函数的 url,假设它是这样的:https://us-central1-<your-project>.cloudfunctions.net/every5MinTrigger。
每隔 5 分钟从您的 cron 作业中调用此 ,https://us-central1-<your-project>.cloudfunctions.net/every5MinTrigger, url,以便将通知安排在接下来的 5 分钟内。
希望对你有帮助。