How Does WhatsApp overcome the GCM Push notifications delay?

后端 未结 2 1796
孤城傲影
孤城傲影 2021-02-20 15:05

I am trying to use GCM service, Every thing is OK except of the long delay that GCM push notification or deliver payload... some times it take to my App 5 minutes receive notifi

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 16:09

    The big players

    Lets focus on WhatsApp from a scale point of view. Their scale is global and one of the largest in terms of the market share. For players like these it becomes necessary to provide a consistent and smooth user experience no matter what the circumstances are. This means that the "small fish developers" like us are left with pre-defined rules by the big fish companies. Sometimes, it is the implementation that is challenging enough that a lot of the small time developers have to do with whatever is available.

    I would like to take WhatsApp's push notification as an example to illustrate the above.

    First of all, we must stop associating Push notification with Google's services exclusively. Would a device without Google services won't receive Push notifications? No, of course it can -- try focusing on the core mechanism of a push notification

    How does a Push Notification work?

    Contrary to a protocol like HTTP which assumes a client-server architecture and is a uni-directional protocol(server can't initiate communication by itself), a plain socket enables communication bi-directionally.

    You want implement your own File transfer protocol over socket? You can!

    You want implement your own Chatting protocol over socket? You can!

    You want implement your own Push notifications protocol over socket? You can!

    A socket is the canvas of communication over a network. You can pretty much do anything. Personally we have developed a custom request-response protocol in our organization.

    Multiple implementations

    Don't stick with default GCM/FCM notification messages protocol or implementation. You can deploy a mechanism to maintain a persistent socket connection with the device and can listen to whatever the server wants to push.

    • WhatsApp uses both GCM as well as their proprietary XMPP server implementation for Push notifications
    • WhatsApp(and several other Apps) relies upon Play services as it is present as a system App on lot of devices and therefore holds a special status where it is very less likely to be killed like the normal Apps do. I don't claim to know the exact strategy WhatsApp adopts w.r.t Play services but one thing is sure when the message needs to be delivered, it would, be it using FCM or their own socket.
    • For devices that do not have play services, its own socket communication is relied upon. It maybe that FCM is preferred over Play Services when the latter are available or there is some optimal strategy in place conjuncting both the FCM and Custom socket implementation but those are the internals.

    From WhatsApp's latest build(2.19.203):

    Notice that the relevant permission for FCM is present.

    Suspected socket implementation: https://android.stackexchange.com/questions/43970/how-is-whatsapp-able-to-receive-messages-when-not-in-use

    XMPP based Push Notifications

    From, https://www.quora.com/Does-WhatsApp-use-GCM-to-exchange-messages

    No, whatsapp doesn't use GCM[NOT entirely true]. It uses a modified version of XMPP called FunXMPP instead. It changes the XML keywords in message frames to save bandwidth as it's users aka mostly mobile network users, doesn't have a good internet connection. The replacements can be found here: mgp25/Chat-API (I am not the author though)

    More proof(official source):

    If browsing works, but WhatsApp doesn't, please contact your mobile provider and system administrator and make sure that your APN and router are configured correctly to allow non-web and socket connections. You may also try a different connection. If you are connected to Wi-Fi, try mobile data or vice versa.

    They have even asked for whitelisting of any port(custom) in use by them.

    So, WhatsApp does it. Facebook does it. Google does it. A lot of these companies do this. You can do it as well(to an extent) using Ejjaberd, Openfire or some other technology and having a proper client side implementation for the same.

    Chinese ROMs

    Some chinese ROMs have taken it 10 steps further and radically changed the way the memory and processes are managed in Android. As an example, in Oppo if the Application doesn't have Auto Start turned on, it will not let you do anything once your App is killed. No hopes of any experience whatsoever except hoping that the user will open the App again. In these cases, these OEMs have whitelisted WhatsApp, Facebook, Google and other players selectively. So, who suffers?

    Us. Period.

提交回复
热议问题