Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not sending network call when app is closed/background #3016

Closed
waqas-ansari opened this issue Jan 24, 2019 · 1 comment
Closed

Not sending network call when app is closed/background #3016

waqas-ansari opened this issue Jan 24, 2019 · 1 comment

Comments

@waqas-ansari
Copy link

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. SocketTimeoutException on Retrofit 1.9.0 + OkHttp 2.3.0 + Android 4.4.4 okhttp#1771
  2. java.net.SocketTimeoutException from HTTP/2 connection leaves dead okhttp clients in pool okhttp#3146
  3. SocketTimeoutException with OkHttp 2.0.0 + Retrofit 1.6.1 okhttp#1037
  4. ConnectException/ETIMEDOUT with retrofit/OkHttp okhttp#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.

@JakeWharton
Copy link
Collaborator

Dupe of square/okhttp#4572

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants