Update the way we handle push notifications (#1061)

This commit is contained in:
David Perez
2024-02-23 17:12:02 -06:00
committed by Álison Fernandes
parent 40dddf017d
commit 3febae577a
5 changed files with 113 additions and 115 deletions

View File

@@ -41,7 +41,6 @@ class AuthRequestNotificationManagerImpl(
@SuppressLint("MissingPermission")
private fun handlePasswordlessRequestData(data: PasswordlessRequestData) {
val notificationManager = NotificationManagerCompat.from(context)
if (notificationManager.areNotificationsEnabled(NOTIFICATION_CHANNEL_ID)) return
// Construct the channel, calling this more than once is safe
notificationManager.createNotificationChannel(
NotificationChannelCompat
@@ -52,6 +51,7 @@ class AuthRequestNotificationManagerImpl(
.setName(context.getString(R.string.pending_log_in_requests))
.build(),
)
if (!notificationManager.areNotificationsEnabled(NOTIFICATION_CHANNEL_ID)) return
// Create the notification
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentIntent(createContentIntent(data))

View File

@@ -27,7 +27,6 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import java.time.Clock
import java.time.ZoneOffset
import java.time.ZonedDateTime
@@ -128,7 +127,7 @@ class PushManagerImpl @Inject constructor(
-> {
if (settingsDiskSource.getApprovePasswordlessLoginsEnabled(userId) == true) {
val payload: NotificationPayload.PasswordlessRequestNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(string = notification.payload)
mutablePasswordlessRequestSharedFlow.tryEmit(
PasswordlessRequestData(
loginRequestId = payload.id,
@@ -140,7 +139,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.LOG_OUT -> {
val payload: NotificationPayload.UserNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
mutableLogoutSharedFlow.tryEmit(
NotificationLogoutData(payload.userId),
)
@@ -150,7 +149,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_CIPHER_UPDATE,
-> {
val payload: NotificationPayload.SyncCipherNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncCipherUpsertSharedFlow.tryEmit(
SyncCipherUpsertData(
@@ -167,7 +166,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_LOGIN_DELETE,
-> {
val payload: NotificationPayload.SyncCipherNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncCipherDeleteSharedFlow.tryEmit(
SyncCipherDeleteData(payload.id),
@@ -185,7 +184,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_FOLDER_UPDATE,
-> {
val payload: NotificationPayload.SyncFolderNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncFolderUpsertSharedFlow.tryEmit(
SyncFolderUpsertData(
@@ -198,7 +197,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_FOLDER_DELETE -> {
val payload: NotificationPayload.SyncFolderNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncFolderDeleteSharedFlow.tryEmit(
@@ -215,7 +214,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_SEND_UPDATE,
-> {
val payload: NotificationPayload.SyncSendNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncSendUpsertSharedFlow.tryEmit(
SyncSendUpsertData(
@@ -228,7 +227,7 @@ class PushManagerImpl @Inject constructor(
NotificationType.SYNC_SEND_DELETE -> {
val payload: NotificationPayload.SyncSendNotification =
json.decodeFromJsonElement(notification.payload)
json.decodeFromString(notification.payload)
if (!isLoggedIn(userId) || !payload.userMatchesNotification(userId)) return
mutableSyncSendDeleteSharedFlow.tryEmit(
SyncSendDeleteData(payload.id),

View File

@@ -2,19 +2,18 @@ package com.x8bit.bitwarden.data.platform.manager.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
/**
* Represents a Bitwarden push notification.
*
* @property contextId The context ID. This is mainly used to check if the push notification
* originated from this app.
* @property notificationType The type of notication.
* @property notificationType The type of notification.
* @property payload Data associated with the push notification.
*/
@Serializable
data class BitwardenNotification(
@SerialName("contextId") val contextId: String,
@SerialName("contextId") val contextId: String?,
@SerialName("type") val notificationType: NotificationType,
@SerialName("payload") val payload: JsonElement,
@SerialName("payload") val payload: String,
)

View File

@@ -20,12 +20,12 @@ sealed class NotificationPayload {
*/
@Serializable
data class SyncCipherNotification(
@SerialName("id") val id: String,
@SerialName("userId") override val userId: String?,
@SerialName("organizationId") val organizationId: String?,
@SerialName("collectionIds") val collectionIds: List<String>?,
@SerialName("Id") val id: String,
@SerialName("UserId") override val userId: String?,
@SerialName("OrganizationId") val organizationId: String?,
@SerialName("CollectionIds") val collectionIds: List<String>?,
@Contextual
@SerialName("revisionDate") val revisionDate: ZonedDateTime,
@SerialName("RevisionDate") val revisionDate: ZonedDateTime,
) : NotificationPayload()
/**
@@ -33,10 +33,10 @@ sealed class NotificationPayload {
*/
@Serializable
data class SyncFolderNotification(
@SerialName("id") val id: String,
@SerialName("userId") override val userId: String,
@SerialName("Id") val id: String,
@SerialName("UserId") override val userId: String,
@Contextual
@SerialName("revisionDate") val revisionDate: ZonedDateTime,
@SerialName("RevisionDate") val revisionDate: ZonedDateTime,
) : NotificationPayload()
/**
@@ -44,9 +44,9 @@ sealed class NotificationPayload {
*/
@Serializable
data class UserNotification(
@SerialName("userId") override val userId: String,
@SerialName("UserId") override val userId: String,
@Contextual
@SerialName("date") val date: ZonedDateTime,
@SerialName("Date") val date: ZonedDateTime,
) : NotificationPayload()
/**
@@ -54,10 +54,10 @@ sealed class NotificationPayload {
*/
@Serializable
data class SyncSendNotification(
@SerialName("id") val id: String,
@SerialName("userId") override val userId: String,
@SerialName("Id") val id: String,
@SerialName("UserId") override val userId: String,
@Contextual
@SerialName("revisionDate") val revisionDate: ZonedDateTime,
@SerialName("RevisionDate") val revisionDate: ZonedDateTime,
) : NotificationPayload()
/**
@@ -65,7 +65,7 @@ sealed class NotificationPayload {
*/
@Serializable
data class PasswordlessRequestNotification(
@SerialName("userId") override val userId: String,
@SerialName("id") val id: String,
@SerialName("UserId") override val userId: String,
@SerialName("Id") val id: String,
) : NotificationPayload()
}