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

add in-app update feature #61

Open
panwar2001 opened this issue Sep 21, 2024 · 1 comment
Open

add in-app update feature #61

panwar2001 opened this issue Sep 21, 2024 · 1 comment

Comments

@panwar2001
Copy link
Owner

panwar2001 commented Sep 21, 2024

  • In-app updates are not compatible with apps that use APK expansion files (.obb files).
  • when app is opened , if new version is available then a dialog appear with title "Update Avaliable" with description "Would you like to update app?" having two options "yes" and "no". Yes button will update and restart the app without losing the app data. No button will let user continue with the activity without initating update.
@panwar2001
Copy link
Owner Author

val appUpdateManager = AppUpdateManagerFactory.create(context)

// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo

// 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
if(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Request the update.
}
else if(appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
{
// Request the update.
}
}
}
}

request_update(){
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// an activity result launcher registered via registerForActivityResult
registerForActivityResult(StartIntentSenderForResult()) { result: ActivityResult ->
// handle callback
if (result.resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
log("Update flow failed! Result code: " + result.resultCode);
// If the update is canceled or fails,
// you can request to start the update again.
}
},
// Or pass 'AppUpdateType.FLEXIBLE' to newBuilder() for
// flexible updates.
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())
}

// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all app entry points.
override fun onResume() {
super.onResume()

appUpdateManager
    .appUpdateInfo
    .addOnSuccessListener { appUpdateInfo ->
        ...
        // If the update is downloaded but not installed,
        // notify the user to complete the update.
        if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
            popupSnackbarForCompleteUpdate()
        }
    }

appUpdateManager
.appUpdateInfo
.addOnSuccessListener { appUpdateInfo ->
...
if (appUpdateInfo.updateAvailability()
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
) {
// If an in-app update is already running, resume the update.
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
activityResultLauncher,
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())
}
}
}

// Displays the snackbar notification and call to action.
fun popupSnackbarForCompleteUpdate() {
Snackbar.make(
findViewById(R.id.activity_main_layout),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE
).apply {
setAction("RESTART") { appUpdateManager.completeUpdate() }
setActionTextColor(resources.getColor(R.color.snackbar_action_text_color))
show()
}
}

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

1 participant