play-billing-samples icon indicating copy to clipboard operation
play-billing-samples copied to clipboard

Android GPBL 6.0.0 : Getting multiple callback in OnBillingDisconnected & onBillingSetupFinished method.

Open axitasavani opened this issue 10 months ago • 8 comments

About

  • Android API Support: 33
  • Gradle Version: 6.7.1
  • Billing Library Version: 6.0.0

How to reproduce the Issue?

  1. Open the app
  • Billing Client Create
  • Start Connection
  • QueryInventory for InApp & Sub
  • Querypurchase
  1. Minimize App
  • App will be in Background
  1. Goto Mobile Setting
  2. Change Mobile Language to another (Like English to Hindi etc.)
  3. Wait for a few seconds
  • You can see in Android Logcat received lod from BillingService "Billing Service Disconnected".
  • Also You will receive a callback in the override method of void onBillingServiceDisconnected()
  • In this override interface method we will Forcefully destroy BillingClient Object and re-init BillingClient
  • Now, repeat these steps multiple times (5+) and keep the App in the Background.
  • Also Open Android Logcat and see the Logs inside the Logcat.
  • It will be a number of times getting Callback in OnBillingDisconnected (more than we have performed the steps, Like if we have changed language 5 times then 5 times Disconnect Billing Client But here, 100+ times callback received.
  • Noted that From the app we are calling startConnection only a similar match count as disconnected billing client. Which is correct.
public void onBillingServiceDisconnected() {
        if (billingClient != null) {
            if (billingClient.isReady()) {
                billingClient.endConnection();
            }
            billingClient = null;
        }
        retryBillingServiceConnectionWithExponentialBackoff();
    }

private static final long RECONNECT_TIMER_MAX_TIME_MILLISECONDS = 1000L * /*60L **/ 4L; // 5 sec
    
    private void retryBillingServiceConnectionWithExponentialBackoff() {
        if (reconnectMilliseconds <= RECONNECT_TIMER_MAX_TIME_MILLISECONDS) {
            if (isBillingClientReadyForReConnect()) {
                if (handler != null) {
                    handler.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (isValidContext(activity)) {
                                                    activity.runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            establishConnection(isShowProgressBar, retryComeFrom, actionType);
                                                        }
                                                    });
                                                }
                                            }
                                        },
                            reconnectMilliseconds);
                }
                reconnectMilliseconds = (reconnectMilliseconds * 2);
            }
        } 
    }
public void establishConnection() {
         if (getBillingClient() == null && isValidContext(context)) {
          this.mBillingClient = BillingClient
                    .newBuilder(context)
                    .enablePendingPurchases()
                    .setListener(this)
                    .build();
        }

        if (getBillingClient() != null && !getBillingClient().isReady()) {
          this.getBillingClient().startConnection(this);
        }
    }

public BillingClient getBillingClient() {
        return this.mBillingClient;
    }

private boolean isBillingClientReadyForReConnect() {
        if (getBillingClient() == null) {
            return true;
        } else if (getBillingClientConnectionState() == BillingClient.ConnectionState.DISCONNECTED) {
            destroyBillingClient();
            return true;
        } else if (getBillingClientConnectionState() == BillingClient.ConnectionState.CLOSED) {
            destroyBillingClient();
            return true;
        } else {
            return false;
        }
    }
 public void destroyBillingClient() {
        if (getBillingClient() != null) {
            if (getBillingClient().isReady()) {
                getBillingClient().endConnection();
            }
            mBillingClient = null;
        }
    }

If anyone helps how to solve or figure out where is my mistake in implementation then it's saved me...

  • Also One more think that I have also verified is that in this Billing Sample same issue comes when performing the above steps.
  • Classic Texi Java & Koltin both

axitasavani avatar Aug 23 '23 05:08 axitasavani

Yeah, I'm facing this same issue in billing 4,5, and 6. In My case I m also open the App and try to lunch the billing flow but not purchased because continues callback comes in onBillingServiceDisconnected() as well as onBillingSetupFinished() listener. If anyone has a solution then please help us to resolve this issue.

AkshayOptimumbrew avatar Aug 23 '23 07:08 AkshayOptimumbrew

contrib

derik1984 avatar Aug 29 '23 00:08 derik1984

I don't know if code is the problem in my case these logs just never stop. I even reinstalled the app and it is still keeps going. Only restarts help. Someone who understands more than me should open an issue on google tracker.

Fossor avatar Sep 19 '23 09:09 Fossor

I do have this problem. Does anyone have any idea?

aabolfazl avatar Sep 20 '23 08:09 aabolfazl

I'm also affected by this issue.

UPDATE: Clearing the cache of Google Play store app, seems to resolve the issue for now.

vladimir-paskov avatar Sep 28 '23 06:09 vladimir-paskov

As far as I understand, this problem is related to the Google Billing library. Even though I updated my RevenueCat version to 7.0.1, it did not improve and some of my users cannot make purchases in the application (around 3%). I removed RevenueCat from the project and added Google Billing directly, and it keeps connecting-disconnecting. Has anyone found a solution to the problem?

ahmtbrk avatar Oct 16 '23 08:10 ahmtbrk

I work at RevenueCat and I am looking into it. I have noticed that it's not even necessary to clear caches, just opening the Play Store seems to fix it. I've also reproduced it in my physical device (Android 33), but not in an emulator running Android 34 nor 33, they both run same version of the Play Store.

vegaro avatar Oct 27 '23 16:10 vegaro

Same issue here. Event I try to end connection when app in background and start connection onResume, the billing service still keep going disconnect after change system language. Anyone have a solution will be appreciated.

Also file an issue tracker to Google: https://issuetracker.google.com/issues/309541595

Alan72528 avatar Nov 07 '23 07:11 Alan72528