Skip to content

Commit

Permalink
Fix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed May 24, 2024
1 parent cb3d455 commit 7903eb9
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/main/kotlin/fr/shikkanime/utils/FirebaseNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.messaging.AndroidConfig
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
import fr.shikkanime.dtos.variants.EpisodeVariantDto
import fr.shikkanime.services.MemberService
Expand Down Expand Up @@ -44,19 +44,21 @@ object FirebaseNotification {
.setPriority(AndroidConfig.Priority.HIGH)
.build()

val tokens = mutableSetOf("global")
memberService.findAllByAnimeUUID(episodeDto.mapping.anime.uuid!!).forEach { tokens.add(it.uuid!!.toString()) }
// Chunked tokens to avoid the 500 tokens limit (due to the limit of the Firebase API)
val chunkedTokens = tokens.chunked(500)
val topics = mutableSetOf("global")
memberService.findAllByAnimeUUID(episodeDto.mapping.anime.uuid!!).forEach { topics.add(it.uuid!!.toString()) }
// Chunked topics to avoid the 500 topics limit (due to the limit of the Firebase API)
val chunkedTopics = topics.chunked(500)

chunkedTokens.forEach {
chunkedTopics.forEach {
FirebaseMessaging.getInstance()
.sendEachForMulticast(
MulticastMessage.builder()
.addAllTokens(it)
.setNotification(notification)
.setAndroidConfig(androidConfig)
.build()
.sendEach(
it.map { topic ->
Message.builder()
.setTopic(topic)
.setNotification(notification)
.setAndroidConfig(androidConfig)
.build()
}
)
}
}
Expand Down

0 comments on commit 7903eb9

Please sign in to comment.