Not sending network call when android app is closed/background
See original GitHub issueI am using Retrofit2 with OkHttp. Everything is working fine with it except the one thing - a network call when app is closed or in background.
Working Whenever Firebase notification arrives, I make a network call to fetch data related to that notifications and then show notification.
Scenario Consider an Android app which is in background or closed and notification arrives.
What’s Happening Notification arrives successfully, I can see it. But when I try to make a call, it is always giving an error Failed to connect to: ******* and I can’t see any call coming on server.
I have seen some similar type of issues but none of them could help me. Here’s a list:
- https://github.com/square/okhttp/issues/1771
- https://github.com/square/okhttp/issues/3146
- https://github.com/square/okhttp/issues/1037
- https://github.com/square/okhttp/issues/1792
Some suggested to use connectionPool and some to use pingInterval. Nothing worked in my case. I am not sure if it’s bug of Retrofit or OkHttp.
Below is the piece of code I am using for the call. Remember that it’s working perfectly fine when app is in foreground.
OkHttpClient.Builder builder = getHttpBuilder()
.connectionSpecs(getSpecList());
retrofit = new Retrofit.Builder()
.baseUrl(Session.getBaseUrl())
.client(builder.build())
.build();
Call<ResponseBody> call = retrofit
.create(Webservice.class)
.pullNotify(authToken, ivB64, digest, Session.getTimestamp());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
listener.onNetworkCallSuccess(response);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
listener.onNetworkCallFailure(t);
}
});
It always returns SocketTimeoutException
. Any help/guidance is appreciated.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
I had the same issue. Finally, I found that the device needs “Enable Unrestricted data usage” in App setting which allows the app run in the background/ closed…
Android 6 Enable background data. Android phone Settings > Data usage > Cellular data usage > Your App > Background data > Enable Unrestricted data usage.
Android 7 Enable background data. Android phone Settings > Data usage > Data saver > If data saver is disabled, stop here. If data saver is enabled go to Unrestricted data access > Enable Your App.
Android 8 above Enable You App to run in the background. Android phone Settings > Apps > App Info > tap your app > Battery > turn on the ‘Background activity’ option. Android phone Settings > Data usage > Data saver > If data saver is disabled, stop here. If data saver is enabled go to Unrestricted data access > Enable.
Hope this can help someone hit this issue again.
@marain87 This helped me, I had network call working when app is in the background, but not when app was closed. Thanks for the above-detailed answer, saved a lot.