user2924714
user2924714

Reputation: 2008

firebase realtime db with FCM for implementing a chat

I'm developing an Android chat application and it seems I need to send each message both via FCM and also save to realtime DB.

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.

Assume the design is like WhatsApp. Each conversation opens a separate activity, presenting the messages in that conversation.

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).

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).

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?

Upvotes: 4

Views: 1426

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598688

It is indeed quite common to send cloud messages to a device even when the user has the app open and in the foreground. You'd in that case simply ignore the message in the onMessageReveived of your app.

Upvotes: 1

Related Questions