Is leaving a pubnub subscription open in a service optimal
Asked Answered
A

1

4

I have a choice to either use GCM for push notifications but I have decided to leave a pubnub subscription opened in a running service in the background.

It's a chat interface where every user leaves his channel open in the background and only publish to the destination channel when the chat screen is opened.

What are the drawbacks to this and if this is not the optimal way, what can I do to optimize it?

Applesauce answered 28/1, 2016 at 1:52 Comment(0)
S
4

PubNub Android Background Service vs GCM

Using GCM might be a more acceptable way for end users due to the perceived battery usage of background services. And with PubNub, you just publish the message including the GCM push payload and the device will receive the realtime message when app is active and the push notification when the app is not active (or not running).

But perhaps you have a requirement to stay connected to PubNub in the background. Now you have to decide whether you want this service to be started when the device is powered on (even if the app is never started) or only start service when the app is first started. Either way, read up on Android Services. I assume you (@olayinka) know this but just putting this here for the wider audience reading this post.

With Android 6 (not 5), you have to notify the end user that you are starting a service to run in background and the user has to allow it. Only way to get around this is whitelisting the app.

Doze Mode & App Whitelisting

There are three ways to whitelist the app and all of them require user interaction. You can read the full docs about doze mode and app whitelisting but here are the highlights:

  1. Users can manually configure the whitelist in Settings > Battery > Battery Optimization.

  2. An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog. This is a normal permission: an app requesting it will always be granted the permission, without the user needing to approve or see it.

  3. An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog. This is a normal permission: an app requesting it will always be granted the permission, without the user needing to approve or see it. NOTE: it has been reported in some blogs that apps were removed from Google Play store and put under scrutiny when they tried doing this.

  4. PARTIAL_WAKE_LOCK is required to keep receiving messages in background for all Android versions to prevent device from dozing.

Stratify answered 28/1, 2016 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.