diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-15 01:32:56 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-15 01:32:56 -0600 |
commit | a646440e5384c5adb9914211666fcc776cf87567 (patch) | |
tree | 2d21f7a4a2da7fc168ba71cf6818952e9336ebd5 /shared | |
parent | a48d8624d2ee876eac1fed2d0c32af7dccb4a195 (diff) | |
download | etbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.tar.gz etbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.tar.bz2 etbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.zip |
FCM token unregisters when logging out
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt index da9b115..3c213d5 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -85,7 +85,7 @@ class SessionController( } private suspend fun registerFcmToken(token: String) { - print("FCM token: $token\n") + print("Register FCM token: $token\n") try { userFlow.value?.let { user -> val attributes = user.attributes.toMutableMap() @@ -109,9 +109,33 @@ class SessionController( } } - fun logout() { + private suspend fun unregisterFcmToken(token: String) { + print("Unregister FCM token: $token\n") + try { + userFlow.value?.let { user -> + val attributes = user.attributes.toMutableMap() + attributes["notificationTokens"].toString().let { tokens -> + if (tokens.contains("$token,")) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace("$token,", "")) + } else if (tokens.contains(token)) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace(token, "")) + } + usersApi.usersIdPut(user.copy( + attributes = attributes + ), user.id!!) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + fun logout(token: String?) { GlobalScope.launch { loginStateFlow.value = LoginState.Loading + token?.let { unregisterFcmToken(it) } sessionApi.sessionDelete() loginStateFlow.value = LoginState.SignOut } |