Search code examples
push-notificationpushtelegramwhatsappinstant-messaging

How are incoming instant messages and notifications handled by app clients?


I am interested in the procedure behind the handling of inbound messages and notifications from instant messaging apps such as Telegram and WhatsApp. I am acquainted with the push protocol, but I am curious about how instant messaging apps implement the receiving part.

First, are instant messages from services like WhatsApp and Telegram received in the form of a push notification, or is there duplication/redundancy giving rise to some sort of a race condition between push notifications proper handed to an app instance service worker, and messages handed to an app instance foreground/main processes? Alternatively, are instant messages always and only sent as push notifications, as least for end-to-end encrypted messages from apps like Whatsapp?

Secondly, under the duplication/redundancy hypothesis mentioned above, is the notification handled by the service worker passed over to the app instance, which then displays it in the target chat, or is the notification discarded in favour of the app instance's fetching the original message from Telegram/Whatsapp server? (I have in mind a scenario where this is required to ensure that the sender is provided a reliable confirmation that the message has been received.)


Solution

  • Actually it's a tricky question cause apps like Telegram and WhatsApp probably have some multiple cases handling logic, very complex logic.

    But from what I can imagine and actually used in some of my chat apps is the following:

    • Some real-time protocol is used for messaging. Telegram uses their own proprietary protocol, WhatsApp uses XMPP.
    • when both parties are online (sender & recipient) then a real-time XMPP message is delivered. Mainly & usually an app has a persistent TCP/TLS/WSS connection to chat server, mainly while a user uses it.
    • when a recipient is not online (not connected to chat server) e.g. does not use an app (an app is in background/suspended mode) then a push notifications will be delivered. And this is only to notify a user that there is a new message. Then a user (recipient) opens an app and receives a real message (via real-time XMPP connection) or via sync with server by REST/HTTP API.

    So there is no any duplications since pushes are just to notify a user there is a new message while a user is not in app.