-
-
Notifications
You must be signed in to change notification settings - Fork 816
Introduce PushProvider interface for push notification providers #5301
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
6262f96
59c74ef
85ff4b5
649d788
8e31d9b
f91721a
e013074
9a3ddf3
2b66420
6a5af75
030365d
fb64593
7fbecca
cf9cf0d
652093e
ab18e63
96e77d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.homeassistant.companion.android.notifications | ||
|
||
import android.content.Context | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import io.homeassistant.companion.android.common.notifications.PushProvider | ||
import io.homeassistant.companion.android.database.settings.PushProviderSetting | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.coroutines.tasks.await | ||
import timber.log.Timber | ||
|
||
class FirebasePushProvider @Inject constructor( | ||
private val messagingManager: MessagingManager | ||
) : PushProvider { | ||
|
||
companion object { | ||
const val SOURCE = "FCM" | ||
} | ||
|
||
override val setting = PushProviderSetting.FCM | ||
|
||
override fun isAvailable(context: Context): Boolean = true | ||
|
||
private var token: String? = null | ||
private val tokenMutex = Mutex() | ||
|
||
suspend fun setToken(token: String) = tokenMutex.withLock { | ||
this.token = token | ||
} | ||
|
||
override suspend fun getToken(): String { | ||
return tokenMutex.withLock { token } ?: try { | ||
FirebaseMessaging.getInstance().token.await() | ||
} catch (e: Exception) { | ||
Timber.e(e, "Issue getting token") | ||
"" | ||
} | ||
} | ||
|
||
override fun onMessage(context: Context, notificationData: Map<String, String>) { | ||
messagingManager.handleMessage(notificationData, SOURCE) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.homeassistant.companion.android.notifications | ||
|
||
import io.homeassistant.companion.android.common.notifications.PushProvider | ||
import javax.inject.Inject | ||
|
||
class PushManager @Inject constructor( | ||
val providers: Map<Class<*>, @JvmSuppressWildcards PushProvider> | ||
) { | ||
|
||
val defaultProvider: PushProvider get() { | ||
return providers[FirebasePushProvider::class.java]!! | ||
} | ||
|
||
/** | ||
* Get the push url for the default push provider. | ||
|
||
*/ | ||
suspend fun getToken(): String { | ||
return defaultProvider.getToken() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.homeassistant.companion.android.notifications | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.multibindings.ClassKey | ||
import dagger.multibindings.IntoMap | ||
import io.homeassistant.companion.android.common.notifications.PushProvider | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class PushModule { | ||
@Binds | ||
@Singleton | ||
@IntoMap | ||
@ClassKey(FirebasePushProvider::class) | ||
abstract fun bindFirebasePushProvider(firebasePushProvider: FirebasePushProvider): PushProvider | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,22 +3,24 @@ package io.homeassistant.companion.android.launch | |
import io.homeassistant.companion.android.BuildConfig | ||
import io.homeassistant.companion.android.common.data.integration.DeviceRegistration | ||
import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
import io.homeassistant.companion.android.notifications.PushManager | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
class LaunchPresenterImpl @Inject constructor( | ||
view: LaunchView, | ||
serverManager: ServerManager | ||
) : LaunchPresenterBase(view, serverManager) { | ||
serverManager: ServerManager, | ||
pushManager: PushManager | ||
) : LaunchPresenterBase(view, serverManager, pushManager) { | ||
|
||
override fun resyncRegistration() { | ||
if (!serverManager.isRegistered()) return | ||
serverManager.defaultServers.forEach { | ||
ioScope.launch { | ||
try { | ||
serverManager.integrationRepository(it.id).updateRegistration( | ||
DeviceRegistration( | ||
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})" | ||
appVersion = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})" | ||
) | ||
) | ||
serverManager.integrationRepository(it.id).getConfig() // Update cached data | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.homeassistant.companion.android.notifications | ||
|
||
import android.content.Context | ||
import io.homeassistant.companion.android.common.notifications.PushProvider | ||
import io.homeassistant.companion.android.database.settings.PushProviderSetting | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
class FirebasePushProvider @Inject constructor() : PushProvider { | ||
|
||
// Firebase Cloud Messaging depends on Google Play Services, | ||
// and as a result FCM is not supported with the minimal flavor | ||
|
||
companion object { | ||
const val SOURCE = "FCM" | ||
} | ||
|
||
override val setting = PushProviderSetting.FCM | ||
|
||
override fun isAvailable(context: Context): Boolean = false | ||
|
||
override fun isEnabled(context: Context): Boolean = false | ||
|
||
override fun isEnabled(context: Context, serverId: Int): Boolean = false | ||
|
||
override fun getEnabledServers(context: Context): Set<Int> = emptySet() | ||
|
||
override suspend fun getToken(): String = "" | ||
|
||
override fun onMessage(context: Context, notificationData: Map<String, String>) {} | ||
|
||
override suspend fun updateRegistration(context: Context, coroutineScope: CoroutineScope) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add documentation about the purpose of this class.