-
-
Notifications
You must be signed in to change notification settings - Fork 966
Add generic PushProvider abstraction for notification delivery #6619
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
Draft
sk7n4k3d
wants to merge
5
commits into
home-assistant:main
Choose a base branch
from
sk7n4k3d:push-provider-abstraction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
09b6499
Add generic PushProvider abstraction for notification delivery
sk7n4k3d e669f38
Address Copilot review feedback on push provider abstraction
sk7n4k3d 8d12372
Fix Copilot review feedback on push provider abstraction
sk7n4k3d 85fc6bd
Fix remaining Copilot review issues: ktlint formatting and KDoc align…
sk7n4k3d 54b225f
fix: use named parameters in wear DeviceRegistration calls
sk7n4k3d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
app/src/full/kotlin/io/homeassistant/companion/android/push/FcmPushProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import io.homeassistant.companion.android.common.push.PushProvider | ||
| import io.homeassistant.companion.android.common.push.PushRegistrationResult | ||
| import io.homeassistant.companion.android.common.util.MessagingTokenProvider | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
| import timber.log.Timber | ||
|
|
||
| /** | ||
| * Push provider implementation backed by Firebase Cloud Messaging. | ||
| * | ||
| * Only available in the "full" build flavor. | ||
| */ | ||
| @Singleton | ||
| class FcmPushProvider @Inject constructor(private val messagingTokenProvider: MessagingTokenProvider) : PushProvider { | ||
|
|
||
| override val name: String = NAME | ||
|
|
||
| override suspend fun isAvailable(): Boolean { | ||
| return try { | ||
| val token = messagingTokenProvider() | ||
| !token.isBlank() | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| Timber.e(e, "FCM is not available") | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override suspend fun isActive(): Boolean { | ||
| return try { | ||
| val token = messagingTokenProvider() | ||
| !token.isBlank() | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override suspend fun register(): PushRegistrationResult? { | ||
| return try { | ||
| val token = messagingTokenProvider() | ||
| if (token.isBlank()) { | ||
| Timber.w("FCM token is blank") | ||
| null | ||
| } else { | ||
| PushRegistrationResult( | ||
| pushToken = token.value, | ||
| pushUrl = "", // Empty URL means use built-in push URL | ||
| encrypt = false, | ||
| ) | ||
| } | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| Timber.e(e, "Failed to register FCM") | ||
| null | ||
| } | ||
| } | ||
|
|
||
| override suspend fun unregister() { | ||
| // FCM doesn't need explicit unregistration in this context. | ||
| // Token invalidation is handled by Firebase automatically. | ||
| Timber.d("FCM unregister called (no-op)") | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "FCM" | ||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
app/src/full/kotlin/io/homeassistant/companion/android/push/PushProviderModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import dagger.multibindings.IntoSet | ||
| import io.homeassistant.companion.android.common.push.PushProvider | ||
|
|
||
| /** | ||
| * Dagger module that provides push provider implementations for the full flavor. | ||
| * Includes FCM and WebSocket providers. | ||
| */ | ||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class PushProviderModule { | ||
|
|
||
| @Binds | ||
| @IntoSet | ||
| abstract fun bindFcmPushProvider(provider: FcmPushProvider): PushProvider | ||
|
|
||
| @Binds | ||
| @IntoSet | ||
| abstract fun bindWebSocketPushProvider(provider: WebSocketPushProvider): PushProvider | ||
| } |
54 changes: 54 additions & 0 deletions
54
app/src/main/kotlin/io/homeassistant/companion/android/push/WebSocketPushProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
| import io.homeassistant.companion.android.common.push.PushProvider | ||
| import io.homeassistant.companion.android.common.push.PushRegistrationResult | ||
| import io.homeassistant.companion.android.database.settings.SettingsDao | ||
| import io.homeassistant.companion.android.database.settings.WebsocketSetting | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import timber.log.Timber | ||
|
|
||
| /** | ||
| * Push provider implementation backed by a persistent WebSocket connection. | ||
| * | ||
| * This is always available and uses a persistent connection. | ||
| * Used by the minimal flavor when no other provider is selected. | ||
| */ | ||
| @Singleton | ||
| class WebSocketPushProvider @Inject constructor( | ||
| private val serverManager: ServerManager, | ||
| private val settingsDao: SettingsDao, | ||
|
sk7n4k3d marked this conversation as resolved.
|
||
| ) : PushProvider { | ||
|
|
||
| override val name: String = NAME | ||
|
|
||
| override val requiresPersistentConnection: Boolean = true | ||
|
|
||
| override suspend fun isAvailable(): Boolean = true | ||
|
|
||
| override suspend fun isActive(): Boolean { | ||
| if (!serverManager.isRegistered()) return false | ||
| return serverManager.servers().any { server -> | ||
| val setting = settingsDao.get(server.id)?.websocketSetting | ||
| setting != null && setting != WebsocketSetting.NEVER | ||
| } | ||
| } | ||
|
|
||
| override suspend fun register(): PushRegistrationResult { | ||
| Timber.d("WebSocket push provider registered (persistent connection mode)") | ||
| return PushRegistrationResult( | ||
| pushToken = "", | ||
| pushUrl = null, | ||
| encrypt = false, | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun unregister() { | ||
| Timber.d("WebSocket push provider unregistered") | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "WebSocket" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/minimal/kotlin/io/homeassistant/companion/android/push/PushProviderModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import dagger.multibindings.IntoSet | ||
| import io.homeassistant.companion.android.common.push.PushProvider | ||
|
|
||
| /** | ||
| * Dagger module that provides push provider implementations for the minimal flavor. | ||
| * Includes WebSocket provider only (no FCM). | ||
| */ | ||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class PushProviderModule { | ||
|
|
||
| @Binds | ||
| @IntoSet | ||
| abstract fun bindWebSocketPushProvider(provider: WebSocketPushProvider): PushProvider | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.