Introducing UnifiedPush support in Jami

What is UnifiedPush?

Unlike desktop applications, Android applications generally can't run in the background for long periods of time. To preserve as much battery charge as possible, mobile phone manufactureres usually make their devices' battery manager kill (close) applications that are no longer in the foreground, as users prefer to have a phone that does not discharge in just a few hours. However, this imposes various limitations on what long-running background applications can do: in a client-server architecture, the client application cannot continuously stay connected to the server(s); and in a distributed architecture, the application may not work well, since it may be killed by the system battery manager as soon as it's no longer in the foreground.

Fortunately, applications have been able to use some approaches to work around this:

  • disabling battery optimization for the application, so that it is not killed when it's in the background, and can maintain connections;
  • using a permanent notification, to prevent the application from being killed (this is what the "Run in background" option in Jami has done); or
  • use a push notification mechanism.

Unfortunately, for a long time, the only option for push notifications on Android systems was using Google's proprietary Firebase Cloud Messaging (FCM) service. Basically, to wake the application up, the server would send a notification via FCM that would be received by the phone, and the application would be notified.

Thankfully, a free/libre replacement has been emerging: UnifiedPush, now supported by Jami on F-Droid since version 20230227-01 (361).

UnifiedPush is a set of specifications and tools that lets the user choose how push notifications are delivered.

UnifiedPush enables users to receive push notifications using only free/libre software, and frees users from being tied to a single company in order to receive push notifications, thus avoiding vendor lock-in and encouraging decentralization, as well as providing more privacy-friendly options and giving users more control over their data.

How can I use UnifiedPush?

UnifiedPush works similarly to Google's FCM. You will need to install a Distributor application that receives notifications and distributes them to other applications that subscribe for them. Common options are:

Once installed, when launching Jami, you can choose the distributor from which you'd like to receive push notifications. Then, if the DHT proxy is enabled in the account's settings and push notifications enabled in the application's settings, UnifiedPush will be used to receive notifications for incoming messages and calls.

If you do not have any distributor installed, you may see a notice like this:

If you have a distributor installed, Jami will subscribe to a topic via the distributor:

Screenshot from ntfy

In the above screenshot, we see that Jami (cx.ring) is listening for incoming notifications from ntfy.sh/updM8eGYBKuVG9. Then, if DHT proxy and push notifications are enabled in Jami, you should be able to easily and transparently receive push notifications.

Will UnifiedPush ever work on iOS?

iOS doesn’t support running services in the background, so running a UnifiedPush distributor won’t be possible without jailbreaking or Apple’s approval for the foreseeable future.

Source: UnifiedPush FAQ

How does UnifiedPush work?

In order to use UnifiedPush, Jami registers itself to a DHT proxy that will listen to the DHT on behalf of the device. In previous versions, users could use the proxy in two different ways:

  1. with push notifications enabled, so that the proxy would send push to FCM, without needing to disable battery optimizations; or
  2. without push notifications enabled, with battery optimization disabled, so the application would be able to maintain a connection to the proxy and receive messages from it.

The idea of UnifiedPush is to replace the first method with an approach similar to the second method. The user needs to install a distributor (e.g. ntfy) that works with battery optimization disabled. This distributor would then be able to stay connected to a server (for ntfy, this would be a ntfy server like https://ntfy.sh). Then, Jami would request the DHT proxy to send push notifications to a specific topic listened to by ntfy. Once a message is received, ntfy would receive the notification, wake Jami up, and provide it with the notification.

The major advantage of this approach is that one application can be used to receive push notifications for several applications, instead of having each application using a custom protocol and listening for notifications separately (and thus consuming more power). As such, this benefit becomes clearly apparent when the user has several applications that use the same approach (i.e. use UnifiedPush). A non-exhaustive list of applications using UnifiedPush is available.

Resources

By Sébastien Blin, Amin Bandali