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 ch
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.
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:
Users can manually configure the whitelist in Settings > Battery > Battery Optimization.
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.
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.
PARTIAL_WAKE_LOCK is required to keep receiving messages in background for all Android versions to prevent device from dozing.