Use default distributor when selecting Instant for the first time

This commit is contained in:
sim
2025-11-05 14:52:52 +01:00
parent cc0b48b20f
commit 1054918471
3 changed files with 30 additions and 4 deletions
@@ -240,7 +240,14 @@ class SimplexApp: Application(), LifecycleEventObserver {
}
if (mode == NotificationsMode.INSTANT) {
CoroutineScope(Dispatchers.Default).launch {
SimplexService.initUnifiedPush(this) {
/**
* If the user re-selects INSTANT, then we don't use default distributor - so it's possible to use
* a non-default distrib
*/
val activity = if (chatModel.controller.appPrefs.notificationsMode.get() != NotificationsMode.INSTANT) {
mainActivity.get()
} else null
SimplexService.initUnifiedPush(activity, this) {
// Change notifications mode only if everything is correctly setup
chatModel.controller.appPrefs.notificationsMode.set(mode)
}
@@ -428,8 +428,11 @@ class SimplexService: Service() {
private fun getPreferences(context: Context): SharedPreferences = context.getSharedPreferences(SHARED_PREFS_ID, Context.MODE_PRIVATE)
suspend fun initUnifiedPush(scope: CoroutineScope, onSuccess: () -> Unit) {
PushManager.initUnifiedPush(androidAppContext, scope, onSuccess)
/**
* @param activity if not null, try default distrib
*/
suspend fun initUnifiedPush(activity: Activity?, scope: CoroutineScope, onSuccess: () -> Unit) {
PushManager.initUnifiedPush(androidAppContext, activity, scope, onSuccess)
}
fun showBackgroundServiceNoticeIfNeeded(showOffAlert: Boolean = true) {
@@ -1,6 +1,7 @@
package chat.simplex.app.platform
import SectionItemView
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
@@ -36,13 +37,28 @@ object PushManager {
* If a single distrib is available, use it
* If many, ask distributor to use with a dialog
* Else alert about missing service
*
* @param activity if not null: try to use default distrib
*/
suspend fun initUnifiedPush(context: Context, scope: CoroutineScope, onSuccess: () -> Unit) {
suspend fun initUnifiedPush(context: Context, activity: Activity?, scope: CoroutineScope, onSuccess: () -> Unit) {
val vapid = getVapidKey(scope)
?: run {
Log.w(TAG, "Tried to init UnifiedPush but couldn't find VAPID key")
return
}
activity?.let {
UnifiedPush.tryUseDefaultDistributor(activity) { success ->
if (success) {
register(context, vapid)
onSuccess()
} else {
initUnifiedPushWithDialogs(context, vapid, onSuccess)
}
}
} ?: initUnifiedPushWithDialogs(context, vapid, onSuccess)
}
private fun initUnifiedPushWithDialogs(context: Context, vapid: String, onSuccess: () -> Unit) {
val distributors = UnifiedPush.getDistributors(context)
when (distributors.size) {
0 -> {