I have an android app in the market and I\'ve noticed that it can take quite a while for the app to be updated when I release a new version.
What I was hoping is so
What we have used do this is as following ..
I send a cloud (having new app version in it) to app and handled it in app. While handling this cloud i check weather my current version and version in cloud are different than i show a pop-up to user periodically to update app from Google play..
I did it using Firebase Remote config. Here is my method which is called one time-
private void checkAndShowUpdateAvailableAlert() {
try {
String VERSION = "version";
String NEW_FEATURES = "newFeatures";
if (singleton.isUpdateAvailable()) {
FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG)
.build();
firebaseRemoteConfig.setConfigSettings(configSettings);
Map<String, Object> defaultValueHashMap = new HashMap<>();
defaultValueHashMap.put(VERSION, BuildConfig.VERSION_CODE);
defaultValueHashMap.put(NEW_FEATURES, "");
firebaseRemoteConfig.setDefaults(defaultValueHashMap);
long cacheExpiration = 3600; // 1 hour in seconds.
if (firebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
firebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// showing update alert only one time
singleton.setUpdateAvailable(false);
firebaseRemoteConfig.activateFetched();
long remoteVersionCode = firebaseRemoteConfig.getLong(VERSION);
String newFeatures = firebaseRemoteConfig.getString(NEW_FEATURES);
Log.d(TAG, "Remote version: " + remoteVersionCode
+ ", New Features: " + newFeatures);
if (remoteVersionCode > BuildConfig.VERSION_CODE
&& newFeatures != null
&& !newFeatures.isEmpty()) {
contextUtility.showUpdateAlert(newFeatures);
}
} else {
Log.e(TAG, "Remote config fetch failed");
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
Steps-
I maintain two key value pairs in my firebase project-
1. newFeatures and
2. version
version is actually versionCode (integer) which is in sync with my latest build versionCode. When I release any new build, i update this value from firebase console.
In app, I check for this value (one time) and if it is greater, I show update alert to user. newFeatures is an additional key to display what's new
to user.
To check full source code- https://github.com/varunon9/SaathMeTravel
Sincerely, I think it's simply not worth the effort. My first recommendation is to forget it, as the Play Store will take care of the update notification.
If you really want to dedicate your time and effort, check this:
There is no API or service by when you can check with Google Play what the latest version of your app is.
Instead, you should maintain the latest version code on your server, and have your app check it periodically against its own version code. If the version code is higher on the server, then your app needs to be updated and you can tell the user accordingly.
It might be useful for someone else. I tried this way
First create a class having couple of methods to launch play store and get app version code and version information this way
public class CheckForUpdate {
public static final String ACTION_APP_VERSION_CHECK="app-version-check";
public static void launchPlayStoreApp(Context context)
{
final String appPackageName = context.getPackageName(); // getPackageName() from Context or Activity object
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
public static int getRemoteVersionNumber(Context context)
{
int versionCode=0;
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
versionCode=pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
Second create another util class having sharedpreference methods to save and retrieve version code this way
public class PreferenceUtils {
// this is for version code
private final String APP_VERSION_CODE = "APP_VERSION_CODE";
private SharedPreferences sharedPreferencesAppVersionCode;
private SharedPreferences.Editor editorAppVersionCode;
private static Context mContext;
public PreferenceUtils(Context context)
{
this.mContext=context;
// this is for app versioncode
sharedPreferencesAppVersionCode=mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
editorAppVersionCode=sharedPreferencesAppVersionCode.edit();
}
public void createAppVersionCode(int versionCode) {
editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
editorAppVersionCode.apply();
}
public int getAppVersionCode()
{
return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // as default version code is 0
}
}
Finally you can use in your launcher activity or any other activity from where you want show alert dialog box to user to update app if its updated.
public class DashboardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...........
//check whether app is first time launched
AppLaunchChecker.onActivityCreate(this);
alertAppUpdate();
}
Implement alertAppUpdate() method this way
private void alertAppUpdate()
{
int remoteVersionCode=CheckForUpdate.getRemoteVersionNumber(this);
PreferenceUtils preferenceUtils=new PreferenceUtils(this);
if(AppLaunchChecker.hasStartedFromLauncher(this))
{
preferenceUtils.createAppVersionCode(remoteVersionCode);
Log.i("First time","First time app is launched");
}
int existingVersionCode= preferenceUtils.getAppVersionCode();
if(remoteVersionCode>existingVersionCode)
{
/*
**
* app is updated, alert user to update app from playstore
* if app is updated then only save the version code in preferenceUtils
*
*/
AlertDialog.Builder dialogBuilder=AlertDialogBox.getAlertDialogBuilder(this,"Update available","Do you want to update your app now?");
dialogBuilder.setPositiveButton("Update Now", (dialogInterface, i) -> {
CheckForUpdate.launchPlayStoreApp(this);
Log.i("app update service","app is needed to update");
preferenceUtils.createAppVersionCode(remoteVersionCode);
});
dialogBuilder.setNegativeButton("Later",(dialogInterface,i)->{
});
dialogBuilder.show();
}
}
If any errors just let me know . Thank you.
You can do that with this new Android Official API https://developer.android.com/guide/app-bundle/in-app-updates