如何使用Firebase来更新你的Android应用

huangapple go评论60阅读模式
英文:

How to use firebase to update your Android App

问题

我看到有一种使用Firebase远程配置更新应用的方法。一种名为“强制更新”通知的方式。如果有人能为我解释一下,那将非常棒。

英文:

I've seen that there are ways to update an app with Firebase Remote Config. Some sort of "Force Update" Notification. If anyone can explain it to me, that would be great.

答案1

得分: 1

如何使用Firebase来更新您的Android应用程序?

有多种方式可以更新Android应用程序。第一种方法是将数据存储在数据库中。Firebase有两个实时数据库,Cloud Firestore实时数据库。您可以根据您的应用程序用例选择其中一个。为此,我建议您查看以下资源:

至于远程配置(Remote Config),请注意现在您可以实时传播远程配置更新。也就是说,没有必要强制任何操作。因此,我强烈建议您查看这一点。

英文:

> How to use Firebase to update your Android App?

There are multiple ways in which you can update an Android app. The first one would be to store data in a database. Firebase has two real-time databases, Cloud Firestore and the Realtime Database. You can one or the other, according to the use case of your app. For that I recommend you check the following resource:

When it comes to Remote Config, please notice that nowadays you can propagate Remote Config updates in real-time. That being said, there is no need to force anything. So I highly recommend that a look at that.

答案2

得分: 0

要实现在一个简单情况下进行强制更新的想法如下:

  1. 使用 Firebase 远程配置发送您想要强制应用程序使用的版本号。
  2. 然后将远程版本与本地应用程序版本进行比较。
  3. 如果存在不匹配,然后显示一个永久对话框(不可取消),其中包含一个按钮,当用户点击该按钮时,将打开应用程序在 Play 商店中的页面。

以下是用于使用远程配置进行强制更新的小类示例:

class ForceUpdateChecker(private val context: Context, private val onUpdateNeededListener: OnUpdateNeededListener?) {
    interface OnUpdateNeededListener {
        fun onUpdateNeeded(updateUrl: String?)
    }

    fun check() {
        val remoteConfig = FirebaseRemoteConfig.getInstance()
        if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
            val currentVersion = remoteConfig.getString(KEY_CURRENT_VERSION)
            val appVersion = getAppVersion(context)
            val updateUrl = remoteConfig.getString(KEY_UPDATE_URL)
            if (!TextUtils.equals(currentVersion, appVersion) && onUpdateNeededListener != null) {
                onUpdateNeededListener.onUpdateNeeded(updateUrl)
            }
        }
    }

    private fun getAppVersion(context: Context): String {
        var result = ""
        try {
            result = context.packageManager.getPackageInfo(context.packageName, 0).versionName
            result = result.replace("[a-zA-Z]|-".toRegex(), "")
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e(TAG, e.message!!)
        }
        return result
    }

    class Builder(private val context: Context) {
        private var onUpdateNeededListener: OnUpdateNeededListener? = null
        fun onUpdateNeeded(onUpdateNeededListener: OnUpdateNeededListener?): Builder {
            this.onUpdateNeededListener = onUpdateNeededListener
            return this
        }

        fun build(): ForceUpdateChecker {
            return ForceUpdateChecker(context, onUpdateNeededListener)
        }

        fun check(): ForceUpdateChecker {
            val forceUpdateChecker = build()
            forceUpdateChecker.check()
            return forceUpdateChecker
        }
    }

    companion object {
        private val TAG = ForceUpdateChecker::class.java.simpleName
        const val KEY_UPDATE_REQUIRED = "force_update_required"
        const val KEY_CURRENT_VERSION = "force_update_current_version"
        const val KEY_UPDATE_URL = "force_update_store_url"
        fun with(context: Context): Builder {
            return Builder(context)
        }
    }
}

在 BaseActivity 中(或从您的着陆页,只要不是在启动屏幕中)像这样调用它:

ForceUpdateChecker.with(this).onUpdateNeeded(this).check()

在应用程序的创建中添加以下内容:

val firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
// 设置应用内默认值
val remoteConfigDefaults: MutableMap<String, Any> = HashMap()
remoteConfigDefaults[ForceUpdateChecker.KEY_UPDATE_REQUIRED] = false
remoteConfigDefaults[ForceUpdateChecker.KEY_CURRENT_VERSION] = "1.0"
remoteConfigDefaults[ForceUpdateChecker.KEY_UPDATE_URL] = "https://play.google.com/store/apps/details?id=com.com.classified.pems"
firebaseRemoteConfig.setDefaultsAsync(remoteConfigDefaults)
firebaseRemoteConfig.fetch(60) // 每分钟获取一次
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "远程配置已获取。")
            firebaseRemoteConfig.fetchAndActivate()
        }
    }

这段代码通过 Firebase 远程配置检查强制更新,并在需要时触发更新操作。

英文:

For Force update in a simple case the idea is

  1. with firebase remort config sends the version number which you want for your application to be forced

  2. then compare remort version with the local application version

  3. if there is a mismatch then show a permanent dialog (cancelable=false) with a button when the user clicks on that button to open the application in the play store .

Check out this Small Class created for force update with remort config

class ForceUpdateChecker(private val context: Context, private val onUpdateNeededListener: OnUpdateNeededListener?) {
interface OnUpdateNeededListener {
fun onUpdateNeeded(updateUrl: String?)
}
fun check() {
val remoteConfig = FirebaseRemoteConfig.getInstance()
if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
val currentVersion = remoteConfig.getString(KEY_CURRENT_VERSION)
val appVersion = getAppVersion(context)
val updateUrl = remoteConfig.getString(KEY_UPDATE_URL)
if (!TextUtils.equals(currentVersion, appVersion)
&amp;&amp; onUpdateNeededListener != null
) {
onUpdateNeededListener.onUpdateNeeded(updateUrl)
}
}
}
private fun getAppVersion(context: Context): String {
var result = &quot;&quot;
try {
result = context.packageManager
.getPackageInfo(context.packageName, 0).versionName
result = result.replace(&quot;[a-zA-Z]|-&quot;.toRegex(), &quot;&quot;)
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, e.message!!)
}
return result
}
class Builder(private val context: Context) {
private var onUpdateNeededListener: OnUpdateNeededListener? = null
fun onUpdateNeeded(onUpdateNeededListener: OnUpdateNeededListener?): Builder {
this.onUpdateNeededListener = onUpdateNeededListener
return this
}
fun build(): ForceUpdateChecker {
return ForceUpdateChecker(context, onUpdateNeededListener)
}
fun check(): ForceUpdateChecker {
val forceUpdateChecker = build()
forceUpdateChecker.check()
return forceUpdateChecker
}
}
companion object {
private val TAG = ForceUpdateChecker::class.java.simpleName
const val KEY_UPDATE_REQUIRED = &quot;force_update_required&quot;
const val KEY_CURRENT_VERSION = &quot;force_update_current_version&quot;
const val KEY_UPDATE_URL = &quot;force_update_store_url&quot;
fun with(context: Context): Builder {
return Builder(context)
}
}}

Call this like this in baseActivity (or from your landing page just not in splash screen)

ForceUpdateChecker.with(this).onUpdateNeeded(this).check();

In application on create add this

val firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
// set in-app defaults
val remoteConfigDefaults: MutableMap&lt;String, Any&gt; = HashMap()
remoteConfigDefaults[ForceUpdateChecker.KEY_UPDATE_REQUIRED] = false
remoteConfigDefaults[ForceUpdateChecker.KEY_CURRENT_VERSION] = &quot;1.0&quot;
remoteConfigDefaults[ForceUpdateChecker.KEY_UPDATE_URL] =
&quot;https://play.google.com/store/apps/details?id=com.com.classified.pems&quot;
firebaseRemoteConfig.setDefaultsAsync(remoteConfigDefaults)
firebaseRemoteConfig.fetch(60) // fetch every minutes
.addOnCompleteListener { task -&gt;
if (task.isSuccessful) {
Log.d(TAG, &quot;remote config is fetched.&quot;)
firebaseRemoteConfig.fetchAndActivate()
}
}

huangapple
  • 本文由 发表于 2023年2月18日 01:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定