title: firebase realtime db with FCM for implementing a chat
body: <p>I'm developing an <strong>Android chat application</strong> <strong>and it seems I need to send each message both via FCM and also save to realtime DB.</strong></p>
  
  <p>I use Firebase realtime DB for storing and sending messages. I still need to send each message (or at least the message id) also via the FCM. I'm afraid that if I DON'T do it, and send FCM only when the app is in background, the users might miss some incoming messages. </p>
  
  <p>Assume the design is like WhatsApp. Each conversation opens a separate activity, presenting the messages in that conversation. </p>
  
  <p>E.g. I'm in a chat with person A, at this time person B sends me a message. I need to have an active listener in all the conversations I have, to get to know I have a new message from B.
   This means I will need to put listeners to a Service, and not to Activity (because activity is representing one active conversation with its messages). </p>
  
  <p>Putting listeners on service seems not reliable enough (what if android kills the service? Until I restart it, I might miss some of new messages and will see them only when I enter the relevant conversation, so that I get them from database).</p>
  
  <p><strong>For the above reason, it seems I have to send the FCM notification for each message that is sent, in addition to storing it to the database.
  Does it sound reasonable? Is there a better way of achieving reliable messaging?</strong></p>