简体   繁体   中英

Android background service with network connection

Goal
Create a long-running background service with a network connection, similar to Zello app.

Problem
Starting with API level 26 (Oreo), there're tight restrictions on background services and their network activity.

Looking at the Zello app which has a constantly running background service which is able to accept audio and text messages even if the device is sleeping, I wonder how they achieved that? Their service is not running on the foreground. Also, it doesn't look like they use push messages for that, since the app works quite stable in conditions where there's a problem with push messages reception (eg low-end Xiaomi phones).

Any ideas would be appreciated.

From what I gathered these apps use the Firebase Cloud messaging service: https://firebase.google.com/docs/cloud-messaging/

The Firebase Service is embedded in the Android system and maintains a constant network connection to the firebase server. The apps then contact the firebase server which in turn to notifies the destination device.

Advantages:

  • The Android system is responsible of keeping the server connection alive and running
  • The network load is minimized because one connection is used for all apps using the firebase service.

Disadvantages:

  • All data is sent through the firebase server and is therefore (in theory) directly accessible by Google
  • Depedning on how many devices use your app, you need to pay for the service.

A similar question asked might be helpful in tackling the problem too: How does push notification technology work on Android?

These apps don't have long-running services with constant network connections. They use push notifications. When there is a new message for a user, the server sends a push notification to wake the device up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM