2

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alexander N.
  • 1,458
  • 14
  • 25

1 Answers1

3

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank! Can I run this on an application server / Google Apps Engine? Will Google Apps Engine constantly listen to the Firebase server for the time to occur? For instance, if I set the data listener on the server to listen for data added and then fire a message when that time occurs. This looks like it really just reflects whenever a data item is added rather than when 6:00 PM occurs. – Alexander N. May 26 '16 at 15:07
  • Ah... I missed the part about time-based triggering. For that you'd need to trigger it at specific times (i.e. with a cron job) or run a simple priority queue. – Frank van Puffelen May 26 '16 at 17:39
  • Awesome. That is what I was thinking after digging into a few more resources. Thank you! – Alexander N. May 26 '16 at 18:05
  • This methodology uses a request which requires billing, as firebase considers it potentially an out of network call. Is there a way to do this without requiring billing? – Cris Apr 14 '17 at 18:15