How to send a FCM when a time event occurs in the database?

后端 未结 1 498
你的背包
你的背包 2021-01-07 07:04

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 t

相关标签:
1条回答
  • 2021-01-07 07:40

    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:

    • How can I send a Firebase Cloud Messaging notification without use the Firebase Console?
    • Firebase, send message to specific platform (Android)
    0 讨论(0)
提交回复
热议问题