2

I am creating an Ionic 2 app with firebase and I need a way to listen to database changes (specifically on child_added) when the app is closed (I.e. in foreground,background and killed)

Basically, I want to use WebRTC to make calls within the app like whatsapp and I am following this post - https://websitebeaver.com/insanely-simple-webrtc-video-chat-using-firebase-with-codepen-demo

However, the only thing that puzzles me is how it will work when the app is closed. Can anyone please help me understand?

Thanks!

AppFirl
  • 165
  • 2
  • 15

1 Answers1

3

It's not possible to actively listen to database changes using the Firebase client SDK in exactly the same way that you can when your app's code is running.

If you want your app to receive information about changes to your database, you can instead use Firebase Cloud Messaging to send your app a notification with a small payload that contains information about the change. When your app receives the notification, it can then make a decision about what to do. There are some limitations with web support, so be sure to read about that.

Also look into Cloud Functions for Firebase to make it easier to write some server side code that can trigger in response to a database change and send a notification when those changes happen.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you that's exactly the type of brainstorming I needed. I definitely see your point. One more thing - is it possible to trigger a tune (to signify your phone is ringing) when the notification is received by the firebase cloud function? – AppFirl Jan 24 '18 at 15:57
  • I think you'd have to write that code yourself based on data you place in the payload of the notification. – Doug Stevenson Jan 24 '18 at 15:59
  • Thanks Doug. Have an awesome day! – AppFirl Jan 24 '18 at 16:00
  • @DougStevenson to send a notification when something changes in database, do I need both FCM and Cloud Functions? Neither one alone is not enough? – Sourav Kannantha B Apr 20 '21 at 19:30