当数据库中发生时间事件时如何发送 FCM? [英] How to send a FCM when a time event occurs in the database?

查看:11
本文介绍了当数据库中发生时间事件时如何发送 FCM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很肯定,当记录添加到数据库或发生其他一些数据库事件时,我可以发送 Firebase 云消息.我的问题是,如果我有一条记录需要在某个时间向某组设备发送通知,我可以轻松做到这一点吗?这是我可以在 Google App Engine 上做的事情吗?

I am positive that I can send a Firebase Cloud Message when a record is added to the database or when some other database event occurs. My question is, if I have a record that needs to send a notification to a certain group of devices at a certain time, would I be able to do that easily? Is this something I could do on Google App Engine?

示例:

我有一个包含不同时间值的记录列表.当该记录的时间值等于服务器机器的时间值时,发送消息.

I have a list of records with different time values in them. When the time value of that record is equal to that of the server machine, send the message.

推荐答案

这里没有隐藏的魔法.您必须编写代码来侦听数据库中的更改,然后调用 Firebase Cloud Messaging.

There is no hidden magic here. You'll have to write code that listens for the changes in the database and then calls Firebase Cloud Messaging.

ref.on('child_added', function(snapshot) {
  request({
     url: 'https://fcm.googleapis.com/fcm/send',
     method: 'POST',
     headers: {
       'Content-Type' :' application/json',
       'Authorization': 'key=AI...8o' 
     },
     body: JSON.stringify(
       { data: {
           message: "your message"
         },
         to : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
       }
     )
   }, function(error, response, body) {
     if (error) { 
       console.error(error); 
     }
     else if (response.statusCode >= 400) { 
       console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
     }
     else {
       console.log('Message sent');
     }
   });      
})

上面的 JavaScript 片段将在代码中运行并调用 Firebase 云消息传递 HTTP 端点发送消息.

The above snippet of JavaScript would run in code and calls the Firebase Cloud Messaging HTTP endpoint to send a message.

另见:

这篇关于当数据库中发生时间事件时如何发送 FCM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆