Skip to content

Commit

Permalink
Fix PN sending for sama-sama flows
Browse files Browse the repository at this point in the history
  • Loading branch information
balysv committed Nov 4, 2021
1 parent 775369d commit d40b96e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CommsEventConsumer(
} else if (meetingRecipient is UserRecipient) {
val receiver = receiverCommsUser(event.actorId, confirmedMeeting.initiatorId, meetingRecipient.recipientId)
?: return
val sender = event.actorId?.let { commsUserRepository.find(it) }
val sender = senderCommsUser(event.actorId, confirmedMeeting.initiatorId, meetingRecipient.recipientId)
?: return
if (sender.userId == receiver.userId) {
return
Expand Down Expand Up @@ -129,4 +129,16 @@ class CommsEventConsumer(
}
}?.let { commsUserRepository.find(it) }
}

private fun senderCommsUser(actorId: UserId?, initiatorId: UserId, recipientId: UserId): CommsUser? {
return when (actorId) {
null -> recipientId
initiatorId -> initiatorId
recipientId -> recipientId
else -> {
logger.warn("Could not determine comms recipient id")
null
}
}?.let { commsUserRepository.find(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FirebaseNotificationSender(private val deviceRegistrationService: UserDevi
return
}

response.firebaseDeviceRegistrations.forEach {
response.firebaseDeviceRegistrations.forEach { (deviceId, registrationToken) ->
val message = Message.builder()
.setNotification(
com.google.firebase.messaging.Notification.builder()
Expand All @@ -33,22 +33,24 @@ class FirebaseNotificationSender(private val deviceRegistrationService: UserDevi
.build()
)
.putAllData(notification.additionalData)
.setToken(it.registrationToken)
.setToken(registrationToken)
.build()
try {
FirebaseMessaging.getInstance().send(message)
logger.debug("Notifications sent to User#${receiverUserId.id} Device#${it.deviceId}")
logger.debug("Notifications sent to User#${receiverUserId.id} Device#${deviceId}")
} catch (e: Exception) {
if (e is FirebaseMessagingException) {
// Remove unregistered devices
if (e.messagingErrorCode == MessagingErrorCode.UNREGISTERED) {
kotlin.runCatching {
deviceRegistrationService.unregister(UnregisterDeviceCommand(it.deviceId))
deviceRegistrationService.unregister(UnregisterDeviceCommand(deviceId))
}.onFailure {
logger.error("Error unregistering device#${deviceId}", it)
}
}
}
val code = if (e is FirebaseMessagingException) e.messagingErrorCode.toString() else "OTHER"
logger.warn("Could not send Firebase notification to User#${receiverUserId.id} Device#${it.deviceId}: $code", e)
logger.warn("Could not send Firebase notification to User#${receiverUserId.id} Device#${deviceId}: $code", e)
}
}
}
Expand Down

0 comments on commit d40b96e

Please sign in to comment.