Sawan
Sawan

Reputation: 101

Implementing push notification in a chat app

I am building a chat feature using firebase Realtime database where an integer variable stores a number (int online) which when a chat is opened by person A it adds 1 to the database and then if another person B comes online and opens the chat then it adds another 1. So, if the number is 2 I know that both are online and active in the chat app so no need to send push notification.

But the issue that I am facing now is that if a user closes the app without pressing the in-built back button of the chat app (which when pressed subtracts 1 and becomes 0 when both users are inactive), then the number in the database is not changed which causes error in the push notification logic as when Person A sends a message, the code assumes Person B is online. How could this be solved? Please someone help me understand how push notifications generally work?

Upvotes: 0

Views: 644

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7388

First off all sending push notifications is completely FREE. The only costs are made by the the backend (cloud functions) that executes the code to send them.

Secondly. If your users are in the app no push notifications will be shown. Even if you would like to show some in your app you would need to handle the onMessageReceived to show something to the user while he is in the app.

That means there is no need for you to do the thing you try to do. Just send all messages as if no user is in the app. It would even cost you more to check for each user if he is online or not and send the messages only if he is offline than just to send them to everyone.

I would recommend to read the docs a little bit to get a deeper understanding of FCM and if you have any specific questions we can all help you with it :)

Upvotes: 3

Related Questions