-
Notifications
You must be signed in to change notification settings - Fork 4
Add FCM push notification support #122
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
Merged
malkoG
merged 10 commits into
hackers-pub:main
from
dalinaum:feat/fcm-push-notifications
Apr 25, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dae3f28
Add FCM push notification support
dalinaum 3b75e10
Merge branch 'main' into feat/fcm-push-notifications
dalinaum b087143
Fix AndroidManifest.xml after merge
dalinaum ecbc8f4
Address Copilot review feedback on FCM flow
dalinaum d5a22b2
Add FcmTokenManager unit tests
dalinaum 74bb06f
Cancel HackersPubMessagingService scope on destroy
dalinaum 5da821f
Use a monochrome notification icon and localize the title
dalinaum e1d34ae
Roll back FCM token registration if session ended mid-flight
dalinaum 8f2eca9
Document Apollo response testing pattern in CONVENTION
dalinaum ed0d546
Document merge-conflict pitfalls in AGENTS
dalinaum 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
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
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
115 changes: 115 additions & 0 deletions
115
app/src/main/java/pub/hackers/android/data/messaging/FcmTokenManager.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,115 @@ | ||
| package pub.hackers.android.data.messaging | ||
|
|
||
| import android.util.Log | ||
| import com.apollographql.apollo.ApolloClient | ||
| import com.google.firebase.messaging.FirebaseMessaging | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.tasks.await | ||
| import pub.hackers.android.data.local.SessionManager | ||
| import pub.hackers.android.graphql.RegisterFcmDeviceTokenMutation | ||
| import pub.hackers.android.graphql.UnregisterFcmDeviceTokenMutation | ||
| import pub.hackers.android.graphql.type.RegisterFcmDeviceTokenInput | ||
| import pub.hackers.android.graphql.type.UnregisterFcmDeviceTokenInput | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class FcmTokenManager @Inject constructor( | ||
| private val apolloClient: ApolloClient, | ||
| private val sessionManager: SessionManager, | ||
| ) { | ||
| companion object { | ||
| private const val TAG = "FcmTokenManager" | ||
| } | ||
|
|
||
| suspend fun registerCurrentToken() { | ||
| val isLoggedIn = sessionManager.isLoggedIn.first() | ||
| if (!isLoggedIn) return | ||
|
|
||
| val token = try { | ||
| FirebaseMessaging.getInstance().token.await() | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to get FCM token", e) | ||
| return | ||
| } | ||
| registerToken(token) | ||
| } | ||
|
|
||
| suspend fun registerToken(token: String) { | ||
| val isLoggedIn = sessionManager.isLoggedIn.first() | ||
| if (!isLoggedIn) return | ||
|
|
||
| try { | ||
| val response = apolloClient.mutation( | ||
| RegisterFcmDeviceTokenMutation( | ||
| RegisterFcmDeviceTokenInput(deviceToken = token) | ||
| ) | ||
| ).execute() | ||
|
|
||
| if (response.hasErrors()) { | ||
| Log.w(TAG, "FCM token registration returned errors: ${response.errors}") | ||
| return | ||
| } | ||
|
|
||
| val result = response.data?.registerFcmDeviceToken | ||
| when { | ||
| result?.onRegisterFcmDeviceTokenPayload != null -> { | ||
| Log.d(TAG, "FCM token registered") | ||
| if (!sessionManager.isLoggedIn.first()) { | ||
| Log.w(TAG, "Session ended during registration; rolling back") | ||
| unregisterToken(token) | ||
| } | ||
| } | ||
| result?.onRegisterFcmDeviceTokenFailedError != null -> | ||
| Log.w(TAG, "FCM token registration failed: ${result.onRegisterFcmDeviceTokenFailedError.message}") | ||
| result?.onInvalidInputError != null -> | ||
| Log.w(TAG, "FCM token registration invalid input: ${result.onInvalidInputError.inputPath}") | ||
| result?.onNotAuthenticatedError != null -> | ||
| Log.w(TAG, "FCM token registration not authenticated") | ||
| else -> | ||
| Log.w(TAG, "FCM token registration unexpected result") | ||
| } | ||
|
dahlia marked this conversation as resolved.
|
||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to register FCM token", e) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| suspend fun unregisterCurrentToken() { | ||
| val token = try { | ||
| FirebaseMessaging.getInstance().token.await() | ||
| } catch (e: Exception) { | ||
|
dahlia marked this conversation as resolved.
|
||
| Log.w(TAG, "Failed to get FCM token", e) | ||
| return | ||
| } | ||
| unregisterToken(token) | ||
| } | ||
|
|
||
| suspend fun unregisterToken(token: String) { | ||
| try { | ||
| val response = apolloClient.mutation( | ||
| UnregisterFcmDeviceTokenMutation( | ||
| UnregisterFcmDeviceTokenInput(deviceToken = token) | ||
| ) | ||
| ).execute() | ||
|
|
||
| if (response.hasErrors()) { | ||
| Log.w(TAG, "FCM token unregistration returned errors: ${response.errors}") | ||
| return | ||
| } | ||
|
|
||
| val result = response.data?.unregisterFcmDeviceToken | ||
| when { | ||
| result?.onUnregisterFcmDeviceTokenPayload != null -> | ||
| Log.d(TAG, "FCM token unregistered") | ||
| result?.onInvalidInputError != null -> | ||
| Log.w(TAG, "FCM token unregistration invalid input: ${result.onInvalidInputError.inputPath}") | ||
| result?.onNotAuthenticatedError != null -> | ||
| Log.w(TAG, "FCM token unregistration not authenticated") | ||
| else -> | ||
| Log.w(TAG, "FCM token unregistration unexpected result") | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to unregister FCM token", e) | ||
| } | ||
| } | ||
| } | ||
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.