TLDR? Notifying you that you have new messages is done on more reliable, more instant networking channel than the channel used to actually download your message.
Keep reading for more detail on what that means.
I worked on two different chat systems for a job once.
One was automated, i.e you asked the server for something and got back a scripted chat.
One was the human kind, where one person sends messages and the other can see and respond.
I built the human kind out with Firebase Realtime Db. It could’ve also been done with Realm which is another service that manages syncing disparate data sources for you.
While this could’ve been built out ourselves, as a startup you don’t want to waste time building things that aren’t core IP.
You’d start off with two simple things, actually get messages to people and notify them when they receive new ones.
With Firebase, we could add these messages to the db, write rules to ensure only the right users had access to them, and simply set the keep synced on.
Keep synced would do two major major things for us.
- Messages would now be available offline and you could even attempt to send them while offline, they’d be sent whenever you did go online.
- When you receive new messages, they’d be put into storage and you could access them if you had since gone offline.
Synced distributed databases are kinda like time travel episodes in sci-fi. They have a lot of weird cases all over the place and you take a while to get used to it if you’ve never seen one before.
So, by now you might think. Why even bother with separate mobile app notifications? The data would be synced when the other person sends it right? Shouldn’t we only need the Firebase Realtime DB?
This would be fine if mobile OS developers did one very bad thing, which they don’t. Which is, if apps were allowed to run all the time in the background.
Your app is going to be shut down. Either for memory pressure, or to conserve battery or simply to avoid pointless checks when you’re not using it often.
There could be a new message and people would have received it, if the app ever started up that is.
This can be solved very simply, by sending a push notification!
Push notifications can ‘wake up’ devices in doze if marked high-priority. Android may prioritize them in such a way that a push notification gets through but no other networking is allowed at that time. Which means if you tried a subsequent network call it could outright fail.
That’s how the two work together. You might have had enough of a network to get the push notification, which alerts you to the possibility of a message, maybe even a title. But your actual database backing the chat, hasn’t updated yet since it has to operate through a different network channel.
Hence ‘ You may have new messages’.
The next time you see Whatsapp show you that notification, you’ll know what’s happening behind the scenes :)
Networking is complicated and fun and pretty unique for mobile devices. I was jumping up and down when I realised I recreated the situation that makes Whatsapp tell you that in my own chat system.
Ask me any questions by commenting below or sending me a message on Twitter at @AniketSMK !
Top comments (0)