Add support for the FCMv1 format in push notifications (#1456)

This commit is contained in:
David Perez 2024-06-18 09:10:32 -05:00 committed by Álison Fernandes
parent dc633f0c0a
commit 6fd4be20e3
4 changed files with 679 additions and 44 deletions

View file

@ -65,10 +65,17 @@ interface PushManager {
val syncSendUpsertFlow: Flow<SyncSendUpsertData>
/**
* Handles the necessary steps to take when a push notification with payload [data] is received.
* Handles the necessary steps to take when a push notification with a legacy FCM [data]
* payload is received.
*/
fun onMessageReceived(data: String)
/**
* Handles the necessary steps to take when a push notification with FCM v1 payload is
* received.
*/
fun onMessageReceived(data: Map<String, String>)
/**
* Registers a [token] for the current user with Bitwarden's server if needed.
*/

View file

@ -106,14 +106,34 @@ class PushManagerImpl @Inject constructor(
.launchIn(unconfinedScope)
}
@Suppress("LongMethod", "CyclomaticComplexMethod", "ReturnCount")
override fun onMessageReceived(data: String) {
val notification = try {
json.decodeFromString<BitwardenNotification>(data)
} catch (exception: IllegalArgumentException) {
return
}
onMessageReceived(notification)
}
@Suppress("ReturnCount")
override fun onMessageReceived(data: Map<String, String>) {
val type = data["type"] ?: return
val payload = data["payload"] ?: return
val notificationType = try {
json.decodeFromString<NotificationType>(string = type)
} catch (exception: IllegalArgumentException) {
return
}
val notification = BitwardenNotification(
contextId = data["contextId"],
notificationType = notificationType,
payload = payload,
)
onMessageReceived(notification)
}
@Suppress("LongMethod", "CyclomaticComplexMethod", "ReturnCount")
private fun onMessageReceived(notification: BitwardenNotification) {
if (authDiskSource.uniqueAppId == notification.contextId) return
val userId = activeUserId ?: return

View file

@ -17,8 +17,10 @@ class BitwardenFirebaseMessagingService : FirebaseMessagingService() {
lateinit var pushManager: PushManager
override fun onMessageReceived(message: RemoteMessage) {
val data = message.data["data"] ?: return
pushManager.onMessageReceived(data)
message
.data["data"]
?.let { pushManager.onMessageReceived(it) }
?: pushManager.onMessageReceived(message.data)
}
override fun onNewToken(token: String) {