I have an app that allows responding to notifications through Notification.Actions. So basically, I have a Pending Intent with this
Intent actionIntent = actionIntent = new Intent(this, NotificationActionIntentService.class);
actionPendIntent = PendingIntent.getService(
this,
requestCodeBtn,
actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
What I do inside my NotificationActionIntentService is a HttpRequest to my BE indicating the update needed to make.
NOTE: When app is in the foreground, everything works fine.
Here is the thing:
Upon receiving the GCM, when I press the Notification.Action immediately, everything still works fine.
But, when I wait for like 30 seconds after receiving the GCM before pressing the Notification.Action, the network request isn't completed. The logs only show that it made a network request. But I never get any response. When I view the logs in my BE server. I do not receive the network request.
But when I launch the app manually, the network is then resumed. It seems like the network request is being paused then resumed when the app is already in foreground. (I do not want this behavior because I want it to execute just in the background)
UPDATE: After pressing the Notification.Action, after a minute or two (without opening the app) I received java.net.ConnectException: Failed to connect
in my logs. (Note: I have an internet connection). It seems like the app cannot connect to the network while it is running in background only.
PS: Im using Retrofit 2 in creating my network request. Also I'm using Android N as a test device.
Answering my own question:
source -> https://stackoverflow.com/a/41281059/5285687
It turns out that when the device is in power saving mode, the system prevents the background apps from accessing the network.
Here's a solution that you can use. -> https://stackoverflow.com/a/39167736/5285687
but you need to provide an android Notification for this.