ChatGPT解决这个技术问题 Extra ChatGPT

How does push notification technology work on Android?

How has Google implemented their push notification feature? Does it work through polling done by a service running in the background or in a different way?

Sounds like a real question to me. I was wondering the answer to it and Google brought me here. Once again another quality question closed.

r
redfast00

From what I've heard during an Android developers conference in Israel:

There is simply a TCP socket waiting in accept mode on a cloud Google server. The TCP connection had been initiated by the Google Play application. That's why Google Play must be installed on the device for making Google Cloud Messaging (GCM) (formerly Android Cloud to Device Messaging Service - C2DM) work.

When this TCP client socket receives some message, the message contains information such as the package name of the application it should be addressed to, and of course - the data itself. This data is parsed and packed into an intent that is broadcast and eventually received by the application.

The TCP socket stays open even when the device's radio state turns into "idle" mode. Applications don't have to be running to receive the intents.

More information at http://developer.android.com/google/gcm/gcm.html


But I think it works on emulator as well. The thing that come into my mind is that the device keep server updated with its current path (IP). When google server needs to send notification, it takes its current path and forward message to that path. Am I right to some extent?
@Khawar Raza: when the device's IP changed from some reason, the device disconnects from server. when it happens - new connection astemblish instead, based on the device new IP.
I see. That would be quite interesting actually, I wonder if that's true.
So it's still polling, but the good side is that polling is centralized by Google Play.
In the end, all communication in computers is just polling. The router is polling whether the Google server has sent a packet. The network card is polling whether the router has redirected the packet to it. The CPU is polling whether the network card has raised an interrupt. It's all about how to poll efficiently.
h
henrebotha

Android keeps one active connection to Google's servers, but it doesn't use much power or data, because no traffic is sent along it until something sends a Google Cloud Messaging (GCM) message to an app on your phone. There's only one connection on the phone, used by all apps: installing a new app that uses GCM doesn't add any extra load.

The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app. The app must have registered with Android to use GCM, and it must have the relevant permission. When the app starts, it might create a notification straight away with the data from the message. GCM messages are very limited in size, so the app might instead open a normal connection to the third-party server to get more information (for example, downloading the headers of new emails).

The advantage of using push notifications is that apps don't have to run at regular intervals to check for new data, saving both power and data. The advantage of having a centralized mechanism like GCM is that the device only needs one open network connection and the Android GCM system is the only thing that needs to keep running, rather than each app having to stay running in the background to keep its own network connection to its own server.

Took this from : Source Also see here.


Just a comment about the GCM connection. That single connection is a Polling connection.
The crucial part for me was that a third-party server e.g. an e-mail server really does send a notification message to Google GCM servers. It is a service Google provides free of charge and any such third-party has to implement a communication channel with Google servers using their GCN protocol. By the way, the protocol is pretty much just a JSON-formatted HTTP response. See developers.google.com/cloud-messaging for detailed information.
S
Shreesh

You can implement the push notification on android yourself with a long polling tcp connection. But that would involve maintaining an extra socket => battery drain. Or you can open a connection at regular intervals using the Alarm Manager.

Google probably opens one socket for all the C2DM push notifications, hence its more battery efficient.


so its confirmed the client is polling along a tcp connection right ? do you know how often it polls for data ? is it every 5 seconds for example. or is it fast like a heartbeat ?
u
user1767754

As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features.

https://firebase.google.com/docs/cloud-messaging/


M
Mario Kutlev

Yes, you're right. Google had a service (GTalk Service) and this service asked Google servers in some periods of time.


I do not know. I've just read how C2DM was working. Now something can change.