question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not sending network call when android app is closed/background

See original GitHub issue

I 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:

  1. https://github.com/square/okhttp/issues/1771
  2. https://github.com/square/okhttp/issues/3146
  3. https://github.com/square/okhttp/issues/1037
  4. 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:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
marain87commented, Jul 10, 2020

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.

0reactions
ak-singlacommented, Oct 23, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrofit - Not sending network call when app is closed ...
Consider an Android app which is in the background or closed and notification arrived. What's Happening. Notification arrives successfully, I ...
Read more >
Connect to the network - Android Developers
Most network-connected apps use HTTP to send and receive data. The Android platform includes the HttpsURLConnection client, which supports TLS, ...
Read more >
Workspace ONE Compromised Device Overview | Dell Barbados
Compromised devices include "jailbroken" iOS and "rooted" Android devices that a ... If the application is manually closed, background checks do not resume ......
Read more >
What is draining my data on Android phone? - Interview Area
Open your phone's Settings app. Tap Network & internet. Internet. Next to your carrier, tap Settings . Tap Data warning & limit. If...
Read more >
S6 edge plus to nexus 6p? - Android Forums at AndroidCentral.com
Posted via the Android Central App. ... It is also significant to me that AT&T, my network, does not support HD Voice of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found