Create In-app Update Notice Use Firebase Easily (Android Studio)- 2022

Monir Hossain
Author: Monir Hossain

নিজেকে এমন ভাবে বদলে নিতে শিখুন.!🥀 যাতে মানুষ আপনার পুরাতন রুপ দেখার জন্যে আফসোস করে.!🙂💔

First connect to a Firebase then connect remote config.

Variable Declare

FirebaseRemoteConfig remoteConfig;

Java Code

 int currentVersionCode;

        currentVersionCode = getCurrentVersionCode();
        Log.d("myApp",String.valueOf(currentVersionCode));

        remoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setMinimumFetchIntervalInSeconds(2)
                .build();
        remoteConfig.setConfigSettingsAsync(configSettings);
        remoteConfig.fetchAndActivate().addOnCompleteListener(new OnCompleteListener<Boolean>() {
            @Override
            public void onComplete(@NonNull Task<Boolean> task) {
                if (task.isSuccessful()){
                    final String newVersionCode = remoteConfig.getString("newVersionCode");
                    if (Integer.parseInt(newVersionCode) > getCurrentVersionCode()){
                        showUpdateDialogBox();

                    }
                }
            }
        });

    }

Outside On Create

    private int getCurrentVersionCode(){
        PackageInfo packageInfo = null;
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(),0);

        }catch (Exception e){
            Log.d("myApp",e.getMessage());
        }
        return  packageInfo.versionCode;
    }

    private void showUpdateDialogBox() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.drawable.update)
                .setTitle(getString(R.string.new_update))
                .setMessage(getString(R.string.new_update_text))
                .setCancelable(false)
                .setPositiveButton(Html.fromHtml("<font color='#14192c'>"+"Update"+"</font>"), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        try {
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.playStoreLink))));
                        }catch (Exception e){
                            Toast.makeText(MainActivity.this, "Something Wrong ! Please try again...", Toast.LENGTH_SHORT).show();
                        }

                    }
                }).show();
    }