32

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?

MaxD
  • 522
  • 1
  • 5
  • 10
  • 4
  • 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 '21 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 '21 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.). – Irfan Latif Sep 01 '21 at 08: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. – Irfan Latif Sep 01 '21 at 11:47
  • 1
    @IrfanLatif I am working on some years-old knowledge ... thanks for correcting. – Steve Sep 01 '21 at 23:43

2 Answers2

49

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).

Robert
  • 20,025
  • 6
  • 47
  • 66
  • Thanks very much, crystal clear. One question though, that does not mean that ALL data that reaches my phone runs over Google services, does it? (Thinking of, for example, the page data from browsing Wikipedia on the mobile Firefox app.) I think it just refers to the notification type? – MaxD Aug 31 '21 at 08:21
  • 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 '21 at 08: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 01 '21 at 09: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 01 '21 at 12:14
  • 1
    Makes you wonder, @TomBowen, how many apps don't go those extra steps. Even if it's not PHI/HIPPA levels of "sensitive", just letting Google read everything is... annoying... :/ – FreeMan Sep 01 '21 at 15:59
  • 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. – Joachim Sauer Sep 02 '21 at 10:27
  • 1
    Note that this doesn't work with all the apps or protocols: e.g. an XMPP client called Conversations has to keep its own service running (and visible in the main drop-down menu) to get updates — unless the server supports XEP-0357. – Ruslan Sep 02 '21 at 21:03
  • @Ruslan Conversations is not the only example. Open-source projects, not wanting to rely on Play Services due to technical or privacy reasons, do run a service on device to maintain their own persistent connection to their servers. Telegram-FOSS and K-9 Mail are other notable examples. – Irfan Latif Sep 03 '21 at 11:32
  • Robert is very incorrect here. Apps don't usually receive push notifications directly. Every notification goes through Google Firebase. If you implement your own system, the notifications can be easily blocked by any power saving setting. You would have to keep the app "awake" at all times. – Tarps Sep 08 '21 at 07:12
  • 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 08 '21 at 08:40
13

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.

Chthonic One
  • 231
  • 1
  • 2
  • 2
    What if one wants to use a Google-free device? – Irfan Latif Sep 02 '21 at 05: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 https://firebase.google.com/docs/cloud-messaging/ios/client – Dean Meehan Sep 02 '21 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 02 '21 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. – unforgettableidSupportsMonica Nov 02 '21 at 13:50