Firebase push notifications, how did they make Firebase push notification battery efficient for your Android? 🔋

Shouman B. Shuvo

4 April, 2024

Previously for my work and out of interest, I have read the architecture and documentation of FCM and Firebase push notifications. However, one of the questions of my team lead forced my mind to dive deeper into the architecture and SDK. While my assumption was it creates a WebSocket connection to the server every time the IP is changed, the outcome came very different with a lesson for me as to why I shouldn’t rely on assumption but stand with concrete evidence. Long talks short, assuming you understand the Firebase architecture, still I will follow up shortly.

This article is written with the help of multiple Google searches.

The thought
When we run the app for the first time, an instance of the client app registers and obtains a unique registration token. Firebase SDK uses these different protocols to connect to the FCM service: HTTP, HTTPS, and WebSocket. (can have more!)
How does the GCM protocol work?
It uses a combination of HTTP and XMPP to deliver notifications reliably and efficiently. Steps are:
  • The user opens an app that uses GCM.
  • The app registers the device with the GCM server.
  • The user performs an action in the app that triggers a push notification.
  • The app sends an XMPP message to the GCM server with the device’s registration ID and the payload of the notification.
  • The GCM server forwards the XMPP message to the device.
  • The device receives the XMPP message and displays the notification to the user.
Why WebSockets are not used here? Why XMPP is used?
  • Battery life: WebSockets require a persistent connection between the client and the server. This can drain the battery life of mobile devices.
  • Resource usage: WebSockets can be more resource-intensive than other protocols, such as XMPP. This can be a problem for servers that need to handle a large number of concurrent connections.
  • Efficiency: XMPP is a very efficient protocol. It can deliver messages with very low latency and overhead.
  • Resource usage: XMPP is very lightweight and does not require a lot of resources. This makes it a good choice for mobile devices and servers.

How can XMPP deliver messages at very low latency while websocket cannot?

One key technique is that XMPP uses a decentralized architecture. This means that there is no single server that controls the XMPP network. Instead, XMPP servers are interconnected in a peer-to-peer fashion. This allows XMPP messages to be routed between servers quickly and efficiently.

You can read more here: https://developer.ibm.com/tutorials/x-xmppintro/

Now, the most awaited question. Think that Shouman’s device is connected to the FCM server and he is on the move, changing from network tower to tower. The bus’s WiFi is unstable. So he is also switching from wifi to mobile data, he is crazy!!! In this situation we all are! How Firebase SDK is optimizing battery here? 🔌⚡🪫

As I have mentioned before firebase uses XMPP (sort of) for their push notifications. When a device is continuously changing its IP, XMPP will work by using a feature called session restoration. Session restoration allows a device to reconnect to an XMPP server and resume its session, even if its IP address has changed. When the device changes its IP address, it sends a message to the XMPP server with its new IP address and RID. The XMPP server then updates the device’s session information. The next time the device connects to the XMPP server, it sends its RID. The XMPP server then looks up the device’s session information and restores the session. Session restoration ensures that devices can stay connected to XMPP servers even if their IP addresses change. This is important for mobile devices, which often change IP addresses when they move between different networks.

XMPP session restoration is more efficient than making a new connection for several reasons:

  • Reduced overhead: Session restoration does not require the device to establish a new TCP connection with the XMPP server. This can save a significant amount of time and battery life, especially on mobile devices.
  • Reduced latency: Session restoration allows the device to resume its session with the XMPP server immediately. This reduces the latency of message delivery, which is important for applications like instant messaging.
  • Preserved state: Session restoration preserves the device’s state on the XMPP server. This includes things like the device’s presence information and contact list. This can save the device time and energy, as it does not need to re-establish this information when it reconnects to the server.
  • Improved reliability: Session restoration is more reliable than making a new connection. This is because it is less likely that the device will lose its connection to the XMPP server while it is restoring its session. This is important for applications that need to be highly reliable, such as VOIP and video conferencing.

You may compare this to WebSocket or HTTP/2, but when you create a new WebSocket connection, you are establishing a new TCP connection between your client and the server. This can be expensive in terms of time and resources, especially on mobile devices.

When you restore an XMPP session, you are reusing an existing TCP connection. This is much more efficient, and it also allows you to resume your session immediately without having to re-authenticate.

Shouman B. Shuvo

4 April, 2024