0

I tried below code from this url but the update popup never opens. I did internal testing app. I get toast message only till Toast(On CheckUpdate method,...).

I am basically trying to show in-app updates(Immediate) popup for users to allow update to the app but popup never shows up. Did i am writing incorrectly or i am missing something! Your help will be highly appreciated.

Whats wrong in below code?

implementation 'com.google.android.play:core:1.10.0'

--

     AppUpdateManager appUpdateManager ;
    private int MY_REQUEST_CODE = 999;

    private void checkUpdate() {
        Toast.makeText(MainActivity.this, "On CheckUpdate method", Toast.LENGTH_LONG).show();
         appUpdateManager = AppUpdateManagerFactory.create(this);

// Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

// Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    // This example applies an immediate update. To apply a flexible update
                    // instead, pass in AppUpdateType.FLEXIBLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                // Request the update.
                Toast.makeText(MainActivity.this, "Request the update.", Toast.LENGTH_LONG).show();
                try {
                    appUpdateManager.startUpdateFlowForResult(
                            // Pass the intent that is returned by 'getAppUpdateInfo()'.
                            appUpdateInfo,
                            // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                            AppUpdateType.IMMEDIATE,
                            // The current activity making the update request.
                            this,
                            // Include a request code to later monitor this update request.
                            MY_REQUEST_CODE);
                } catch (IntentSender.SendIntentException e) {
                    Toast.makeText(MainActivity.this, "check fail.."+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                Toast.makeText(MainActivity.this, "Update flow failed! Result code: ", Toast.LENGTH_LONG).show();
                Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
                // If the update is cancelled or fails,
                // you can request to start the update again.
            }
            if (resultCode != RESULT_CANCELED) {
                Toast.makeText(MainActivity.this, "Update flow failed! Result code: " + resultCode, Toast.LENGTH_LONG).show();
                Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
                // If the update is cancelled or fails,
                // you can request to start the update again.
            }
            if (resultCode != ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
                Toast.makeText(MainActivity.this, "Some other error prevented either the user from providing consent or the update from proceeding" + resultCode, Toast.LENGTH_LONG).show();

                // Some other error prevented either the user from providing consent or the update from proceeding.
            }
        }
    }

    // Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all entry points into the app.
    @Override
    protected void onResume() {
        super.onResume();

        appUpdateManager
                .getAppUpdateInfo()
                .addOnSuccessListener(
                        appUpdateInfo -> {
           // ...

                            if (appUpdateInfo.updateAvailability()
                                    == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                                Toast.makeText(MainActivity.this, "Update started where you left off", Toast.LENGTH_LONG).show();

                                // If an in-app update is already running, resume the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.IMMEDIATE,
                                            this,
                                            MY_REQUEST_CODE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }

1 Answers1

1

In-app updates works only if the current installed app version is lesser than the one in the Playstore. Until then you will not see any popup.

If you want to check whether your code is properly working or not you can use the option called Internal app sharing from developer console.

To use internal app sharing you need to build to apps one with the higher version and a normal version. For instance, if you current version code is 5 then build an app with version code 6 and upload this into Internal app sharing enable the account you want to check and you'll get the pop-up as you're looking for.

These are some answers and documentation you can folow.

  1. Answer - 1
  2. Google documentation
  3. Google answers

For any other help please comment.

Update: This is the working code I'm using in my app for triggering updates.

private AppUpdateManager appUpdateManager;
    private static final int RC_APP_UPDATE = 2;

    InstallStateUpdatedListener installStateUpdatedListener;

    installStateUpdatedListener = state -> {
                if (state.installStatus() == InstallStatus.DOWNLOADED){
                    launchSnackBar();
                }else if (state.installStatus() == InstallStatus.INSTALLED){
                    if (appUpdateManager != null){
                        appUpdateManager.unregisterListener(installStateUpdatedListener);
                    }
                }
            };
    
            StartUpdateCheck(installStateUpdatedListener);
    
    private void StartUpdateCheck(InstallStateUpdatedListener installStateUpdatedListener) {
            appUpdateManager = AppUpdateManagerFactory.create(MainActivity.this);
            appUpdateManager.registerListener(installStateUpdatedListener);
            appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
                    try {
                        appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, MainActivity.this, RC_APP_UPDATE);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                }else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED){
                    launchSnackBar();
                }
            });
        }
    
        private void launchSnackBar() {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogTheme);
            builder.setTitle("New Update Available")
                    .setMessage("A new update has been downloaded click on install to update your application and " +
                            "enjoy all new features")
                    .setPositiveButton("Install", (dialog, which) -> {
                        dialog.dismiss();
                        if (appUpdateManager != null){
                            appUpdateManager.completeUpdate();
                        }
                    });
            AlertDialog dialog = builder.create();
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }
    
    @Override
        protected void onResume() {
            super.onResume();
            if (appUpdateManager != null){
                appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
                    if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                        launchSnackBar();
                    }
                });
            }
        }
  • I already did these steps of internal app testing but still it did not work –  Jun 12 '21 at 05:38
  • What are the errors you got sor far like if you're code is working properly you'll get the info logs in your logcat. I'll post my working code – Venkatesh Talacheeru Jun 12 '21 at 08:47
  • How can I get logs if I published app in app internal testing! You can see in the code above in `checkUpdate()` method. I get only this Toast message `Toast.makeText(MainActivity.this, "On CheckUpdate method", Toast.LENGTH_LONG).show();` it will be helpful if you can share your code. –  Jun 12 '21 at 09:22
  • No the log out works irrespective of the internal app sharing logout all like if the app update listener notifies whether it is initiated and attatched to a activity checks for update and other. – Venkatesh Talacheeru Jun 12 '21 at 10:16
  • No i am not getting any errors in logcat, when i run/debug the app. If you have working code of in app updates for immediate, please share i will try that –  Jun 12 '21 at 10:32
  • Can you share full code, i did not understand what is `installStateUpdatedListener` And what is value for `RC_APP_UPDATE`? –  Jun 12 '21 at 11:04
  • I used your code but its automatically downloading and installing update. whats going on –  Jun 12 '21 at 12:03
  • The update is not installed until the positive button in the alert dialog is clicked. I never faced this issue I'm not sure what's wrong. – Venkatesh Talacheeru Jun 12 '21 at 12:13
  • In the `installStateUpdatedListener` is the flag `InstallStatus.DOWNLOADED` is correct? i doubt it should be something else because i never downloaded app, download update is pending, or is the status is correct? –  Jun 12 '21 at 12:22
  • And do i have to write the above few line of code(`installStateUpdatedListener`) at `onCreate` method? –  Jun 12 '21 at 12:25
  • yes `installStateUpdatedListener` has to be written in `onCreate();` method. We are passing that object to the `StartUpdateCheckMethod();` this will check if there is any update and completes the check – Venkatesh Talacheeru Jun 12 '21 at 12:27
  • I written your method as it is, i see `update` button in play store after update relese, and after that when i click app icon from mobile and click to open app, i don't see anything which talks about to take update. I don't know why your code has no effect, and even my pervious code as posted in this question has also no effect. I am using latest `implementation 'com.google.android.play:core:1.10.0'` which one you are using –  Jun 12 '21 at 13:01
  • I just tried the code to ensure if it works as said. And to my expectation, it works perfectly. I don't know what happens at your side. – Venkatesh Talacheeru Jun 12 '21 at 14:48
  • Ok. Which version you are using this ?`implementation 'com.google.android.play:core:X.X.X` ? –  Jun 12 '21 at 14:53
  • The same version like you're using..!! Upload you're code to GitHub and share the link I'll try and give you the best solution. – Venkatesh Talacheeru Jun 12 '21 at 14:55
  • I tried my best helping to find a solution to your issue. I don't know what issue you're correctly addressing. It a working code I post here please look for any info log messages. – Venkatesh Talacheeru Jun 12 '21 at 17:15
  • There are some things you need to take into account first your current app version is 1107 upload this app and use the link and install it. Now upload another app with version number 1108 and open the link if you see the update then head to your app now the play store update dialog will appear. Your app is initializing all the APIs required for in-app updates and there's nothing wrong in code. – Venkatesh Talacheeru Jun 13 '21 at 09:15
  • did you used internal testing or internal app testing? – Venkatesh Talacheeru Jun 13 '21 at 09:54
  • yesterday i open the app in app popup not shown but today when i open it works as expected. Not sure why it did not worked yesterday –  Jun 13 '21 at 10:15
  • May be some internal error. Do accept and upvote my answer if it helped and resolved your issue. – Venkatesh Talacheeru Jun 13 '21 at 10:24
  • Can I able to customize the in-app update layout – sejn Feb 17 '23 at 12:25
  • The general Google loading screen cannot be modifiable I guess. But you can show your own screen and do the magic! – Venkatesh Talacheeru Apr 17 '23 at 08:27