27

So, Android has this view (I think left to the home button is standard) where you see all the "currently open" apps, whatever that means exactly. What I'm wondering is, how can apps that are not amongst the currently "running" ones receive notifications? For example, Whatsapp, Reddit or Email will still send alerts even on a freshly restarted smartphone where I definitely didn't start the app manually.

How exactly does this work? Are, in a sense, all installed apps running in the background all the time, and in regular intervals asking their home server if there has been a notification? Does the notification somehow get sent to the Android system itself, which in turn activates the requested app on demand? At least on a PC I would expect that a program has to be running in order to send alerts, but maybe Android is completely different?

6
  • 4
    Question is slightly different but the answer is same: How do certain apps show new notifications when internet access is restricted to them? Aug 31 at 9:21
  • 2
    When you install an app, you possibly install more than just the "app" you see on screen. These apps can also install services that are running in the background - regardless of whether you see them in the "previous apps". Note also that those "previous apps" are not actually running when you're looking at them in that view - they are suspended, and only come back alive once you bring them to the foreground.
    – Steve
    Aug 31 at 23:22
  • 1
    For example, an app like Life 360 you never have to open when you start your phone, yet it still occasionally checks your location and reports it up to the server on the internet. The Life360 app itself isn't running, but the service it installed is.
    – Steve
    Aug 31 at 23:23
  • 1
    @Steve what you are saying is that even if an app is not running an Activity (the visible part of an app), it might have a background service running which is receiving and displaying the notifications. Well that's not correct. Since Android 8 apps cannot run background services. They either need to run a foreground service (with persistent notification; see the example of Telegram) or they have to rely on the OS to wake them up when needed (e.g. using BroadcastReceiver, Alarm, WorkManager, JobScheduler etc.). Sep 1 at 8:26
  • 3
    But on a Google supported device, Google Play services always runs a background service (com.google.android.gms.gcm.GcmService) which maintains a network connection to Firebase server to receive push messages and delivers them to the target apps. Since Play Services is a privileged system app, it's not subjected to background service restrictions. In addition to GcmService, GMS also runs a number of other background services (usually 20+). You can find a process named com.google.android.gms.persistent always running on any GMS supported device. Sep 1 at 11:47
45

Notifications of Whatsapp and Reddit are triggered by push notifications sent from Google servers, received by Google Play Services, a system app which always runs in background. All Google Push notifications (or as they are named today "Firebase Cloud Messaging" notification) are received and initially processed by the Google Play Services.

The next processing step of the received push notification depends on the push notification type. Plain text notifications can be directly displayed without even starting the app it is targeting. Other push notifications are delivered to the app it belongs to (which is automatically started if it is not already running) and the app can then "translate" the push message into a notification (in case of Whatsapp this require e.g. decryption of the received message or even fetching additional data from Whatsapp servers).

10
  • 6
    @MaxD No only Google push notifications are processed by Google Play Services. Apps usually directly retrieve data from web servers. But even for push notifications apps can bypass this service and implement their own push message system - e.g. Signal has this as optional feature or for devices that don't include Google services (e.g. custom ROM or Huawei devices). But this costs more battery data.
    – Robert
    Aug 31 at 8:35
  • 2
    @MaxD Just in case you're interested in googling for more details, the current system handling this is called Firebase and it can handle two kinds of messages: "notification" (something that's handled directly without involving the app, thus Google can see it as well) and "data" messages delivered to an app for further processing. Many apps use encryption for "data" messages, so that all that Google can see is that an app's server just sent you something, not what it was.
    – TooTea
    Sep 1 at 9:29
  • 3
    @MaxD all notification data is handled by Google, but not other data used by the app. I used to work on an app where data we wanted to notify for was highly sensitive, so we used to just send a generic message with an ID in the notification. The app could then use the ID to get more specific data when the user tapped the notification.
    – Tom Bowen
    Sep 1 at 12:14
  • 2
    It should be noted that this current system allows for massive battery saving compared to the old one where every app that wanted to provide push notification of anything needed to continuously poll on its own. I wish someone built a system that could get the best of both worlds: avoid pointless polling per-app and still avoid use of a centralized system. If an app ever asks you to disable "battery optimizations", then that's a hint that it's probably doing the polling on its own. Sep 2 at 10:27
  • 2
    @Tarps Google Firebase ist the server side. On client side notifications are processes by Google Play Services which can show notifications "directly" or forward it to the app it belongs to. Then the notification will be processed by the "Google Services plugin", a library you include into your app and afterwards by your app code . This included Google code part is sometimes also called Firebase, but that is just a marketing name as it has nothing to do with the actual Firebase system.
    – Robert
    Sep 8 at 8:40
12

I know there is a good answer to this question already, but I feel that there could also be a detailed technical answer as well.

When you install a new app on your phone that uses push notifications, and give it permission to access the internet, it registers interest in it's push service website with the Google Push Notification service on your phone.

The Google Push Service will then register interest in updates with the push notification server for the app via Web Sockets. This will also occur automatically when you power on your phone when the app has been installed.

When an update occurs, the message is passed from the push notification server to the Google Push Service. Then it is passed to the interested app, which then wakes up if sleeping, or loads it's push monitoring code if it's not loaded to load the notification and notify you of the message.

When you power on your phone, and it registers interest all at once, you may get a flood of push notifications. These are all the notifications since the last time the app last notified the server as you read them. This is done standard in Web Sockets to bring you up to date in what you said you registered interest in.

Some apps may have a server that sends a generic push message to the Google Push App to cause it to wake up the actual app, and then the actual app retrieves the real push message. Not all services do this. This is done for security and privacy.

The reason why the Google Push Notification service is used as a middle man is simply because of the fact that it is always running, unlike almost every other app on your phone. It will always be there to receive a push notification. While you could circumvent it, it could mean that you could miss notifications if your app is not running when they come.

4
  • 2
    What if one wants to use a Google-free device? Sep 2 at 5:02
  • @irfan every smart phone has it's own version of this Google play services. Apple have their own and even Google Firebase is able to target iOS apps firebase.google.com/docs/cloud-messaging/ios/client Sep 2 at 15:16
  • 1
    @DeanMeehan Google Firebase on iOS just uses Apple push in the background. That may be even enforceed by Apple.
    – Robert
    Sep 2 at 21:06
  • @IrfanLatif: If you replace Android with LineageOS, your device will be Google-free. If you later change your mind and decide you do want Google cloud messaging after all, you can either install Google's closed-source Google Play Services utility, or you can install the open-source third-party MicroG utility. Despite being open-source, MicroG is still able to connect to Google's push messaging service. Nov 2 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.