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

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

问题描述

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

示例:

我有一个不同时间值的记录列表。当该记录的时间值等于服务器的时间值时,发送消息。 解决方案

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

  ref.on 'child_added',函数(快照){
request({
url:'https://fcm.googleapis.com/fcm/send',
method:'POST',
标题:{
'Content-Type':'application / json',
'Authorization':'key = AI ... 8o'
},
body: JSON.stringify(
{data:{
message:your message
},
to:bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...
}

},函数(error,response,body){
if(error){
console.error(error);
}
else if .statusCode> = 400){
console.error('HTTP Error:'+ response.statusCode +' - '+ response.statusMessage);
}
else {
console .log('Message sent');
}
});
))

上面的代码片段可以运行在代码中, Firebase 云消息传递HTTP端点发送消息



另见:


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?

Example:

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.

解决方案

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');
     }
   });      
})

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

Also see:

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

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