Migrate all channel Id to remove pending channel

This commit is contained in:
sim 2024-12-03 12:45:04 +00:00
parent 0ea06fe62e
commit 075383470e
3 changed files with 62 additions and 1 deletions

View File

@ -37,6 +37,6 @@ object LocalNotification {
}
fun getNotificationChannelId(context: Context, title: String): String {
return "${context.getString(R.string.app_name)}.Push.$title"
return "${context.packageName}.Push.$title"
}
}

View File

@ -0,0 +1,59 @@
package org.unifiedpush.distributor.nextpush
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import org.unifiedpush.distributor.nextpush.utils.TAG
class Migrations(val context: Context) {
private val sharedPreferences = context.getSharedPreferences(Store.PREF_NAME, Context.MODE_PRIVATE)
fun run() {
val currentVersion = sharedPreferences.getInt(PREF_MIGRATIONS_LEVEL, 0)
if (currentVersion == LAST_VERSION) return
Log.d(TAG, "Migration from $currentVersion to $LAST_VERSION")
if (currentVersion < 20000) {
Migration020000.run(context)
}
sharedPreferences.edit().putInt(PREF_MIGRATIONS_LEVEL, LAST_VERSION).apply()
}
interface Migration {
fun run(context: Context)
}
/**
* Migration from 0.x.x to 2.0.0
*/
object Migration020000: Migration {
override fun run(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
removeOldOsNotificationChannel(context)
}
}
/**
* Migration to remove old notifications channel, they will be recreated
* once a notification arrives. We didn't remove channel Id when app were
* deleted. In the same time, we changed the channel ID: they were using
* the app name, and we move to packageId
*/
@RequiresApi(Build.VERSION_CODES.O)
fun removeOldOsNotificationChannel(context: Context) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val oldPrefix = context.getString(R.string.app_name)
notificationManager.notificationChannels.forEach {
if (it.id.startsWith(oldPrefix)) {
notificationManager.deleteNotificationChannel(it.id)
}
}
}
}
companion object {
// 2.0.0
private const val LAST_VERSION = 20000
private const val PREF_MIGRATIONS_LEVEL = "migrations.level"
}
}

View File

@ -14,6 +14,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.unifiedpush.distributor.nextpush.EventBus
import org.unifiedpush.distributor.nextpush.Migrations
import org.unifiedpush.distributor.nextpush.account.AccountFactory
import org.unifiedpush.distributor.nextpush.activities.MainActivity.Companion.goToMainActivity
import org.unifiedpush.distributor.nextpush.activities.ui.StartUi
@ -35,6 +36,7 @@ class StartActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Migrations(this).run()
if (AccountFactory.getAccount(this)?.connected == true) {
goToMainActivity(this)