-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add UnifiedPush support with PushProvider abstraction #6599
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
Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
46530c8
Add org.unifiedpush.android.connector to dependencies
lone-faerie eea01c1
Add UnifiedPush distributor setting
lone-faerie e8e97b0
Add UnifiedPush service
lone-faerie 8c5a7a2
Fix minimal dependencies
lone-faerie cfd9fa2
Fix memory leak
lone-faerie 6b761a0
Use UnifiedPush public key as push token
lone-faerie a540d10
Remove unused imports
lone-faerie 50f2320
Register UnifiedPush on launch
lone-faerie e64c776
Show toast when UnifiedPush is first enabled
lone-faerie f7306e2
Code refactor
lone-faerie 0fb7e06
Fix wear build
lone-faerie 6e8d346
Lint code
lone-faerie 6108ffb
Don't register default distributor on reregistration
lone-faerie 8ca1c72
Fix linting
lone-faerie 8edda09
Refactor: add generic PushProvider interface for FCM/UnifiedPush/WebS…
sk7n4k3d bfa64c2
Add unit tests for PushProvider interface and UnifiedPush message par…
sk7n4k3d 7b3022a
Wire PushProviderManager into LaunchActivity, LaunchPresenter, and Se…
sk7n4k3d 873d387
Fix push_encrypt logic, FCM isActive check, resync duplication, and e…
sk7n4k3d b832294
Fix compilation and tests for upstream compatibility
sk7n4k3d c95440d
Fix ktlint formatting errors
sk7n4k3d 585c80e
Exclude protobuf-java from UnifiedPush connector in full flavor
sk7n4k3d 9733eff
Update dependency lockfiles after protobuf exclusion
sk7n4k3d 8ea839e
Fix compilation errors for upstream API compatibility
sk7n4k3d 6f5208f
Fix import ordering in UnifiedPushReceiver
sk7n4k3d 62a1a12
Update lint baselines for UnifiedPushReceiver and ObsoleteSdkInt
sk7n4k3d 21e5e3e
Remove hardcoded priority, let user choose push provider
aaronstealth 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import io.homeassistant.companion.android.common.data.prefs.PrefsRepository | ||
| 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 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 prefsRepository: PrefsRepository, | ||
| 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) { | ||
| Timber.e(e, "FCM is not available") | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override suspend fun isActive(): Boolean { | ||
| // FCM is active only when UnifiedPush is not enabled and a valid token exists. | ||
| if (prefsRepository.isUnifiedPushEnabled()) return false | ||
| return try { | ||
| val token = messagingTokenProvider() | ||
| !token.isBlank() | ||
| } catch (e: Exception) { | ||
| 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) { | ||
| 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" | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
| 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, UnifiedPush, and WebSocket providers. | ||
| */ | ||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class PushProviderModule { | ||
|
|
||
| @Binds | ||
| @IntoSet | ||
| abstract fun bindFcmPushProvider(provider: FcmPushProvider): PushProvider | ||
|
|
||
| @Binds | ||
| @IntoSet | ||
| abstract fun bindUnifiedPushProvider(provider: UnifiedPushProvider): PushProvider | ||
|
|
||
| @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
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
57 changes: 57 additions & 0 deletions
57
app/src/main/kotlin/io/homeassistant/companion/android/push/UnifiedPushProvider.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,57 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import android.content.Context | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import io.homeassistant.companion.android.common.data.prefs.PrefsRepository | ||
| import io.homeassistant.companion.android.common.push.PushProvider | ||
| import io.homeassistant.companion.android.common.push.PushRegistrationResult | ||
| import io.homeassistant.companion.android.unifiedpush.UnifiedPushManager | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import org.unifiedpush.android.connector.UnifiedPush | ||
| import timber.log.Timber | ||
|
|
||
| /** | ||
| * Push provider implementation backed by UnifiedPush. | ||
| * | ||
| * UnifiedPush allows receiving push notifications via a user-chosen distributor app | ||
| * (e.g. ntfy, NextPush) without relying on Google's FCM infrastructure. | ||
| * | ||
| */ | ||
| @Singleton | ||
| class UnifiedPushProvider @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| private val prefsRepository: PrefsRepository, | ||
| private val unifiedPushManager: UnifiedPushManager, | ||
| ) : PushProvider { | ||
|
|
||
| override val name: String = NAME | ||
|
|
||
| override suspend fun isAvailable(): Boolean { | ||
| val distributors = UnifiedPush.getDistributors(context) | ||
| return distributors.isNotEmpty() | ||
| } | ||
|
|
||
| override suspend fun isActive(): Boolean = prefsRepository.isUnifiedPushEnabled() | ||
|
|
||
| override suspend fun register(): PushRegistrationResult? { | ||
| val distributor = UnifiedPush.getAckDistributor(context) | ||
| if (distributor == null) { | ||
| Timber.d("No UnifiedPush distributor acknowledged") | ||
| return null | ||
| } | ||
| // Registration happens asynchronously via UnifiedPushReceiver. | ||
| // The actual PushRegistrationResult will be created when onNewEndpoint is called. | ||
| UnifiedPushManager.register(context) | ||
| return null // Async - result delivered via UnifiedPushReceiver.onNewEndpoint | ||
| } | ||
|
|
||
| override suspend fun unregister() { | ||
| UnifiedPushManager.unregister(context) | ||
| prefsRepository.setUnifiedPushEnabled(false) | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "UnifiedPush" | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
| package io.homeassistant.companion.android.push | ||
|
|
||
| import android.content.Context | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| 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( | ||
| @ApplicationContext private val context: Context, | ||
| private val serverManager: ServerManager, | ||
| private val settingsDao: SettingsDao, | ||
| ) : 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" | ||
| } | ||
| } |
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.