From 120b9b4f677d1044e8783d152498f51001c3afde Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Mon, 6 Feb 2023 20:40:50 +0100 Subject: [PATCH] Fix problem when testing --- .../fr/ziedelth/listeners/EpisodesRelease.kt | 2 +- .../kotlin/fr/ziedelth/utils/Notifications.kt | 37 ++++++++++++------- .../ziedelth/utils/plugins/PluginManager.kt | 1 + 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/fr/ziedelth/listeners/EpisodesRelease.kt b/src/main/kotlin/fr/ziedelth/listeners/EpisodesRelease.kt index 575c053..098f357 100644 --- a/src/main/kotlin/fr/ziedelth/listeners/EpisodesRelease.kt +++ b/src/main/kotlin/fr/ziedelth/listeners/EpisodesRelease.kt @@ -11,7 +11,7 @@ class EpisodesRelease : Listener { private var lastSend = mutableListOf() @EventHandler - suspend fun onEpisodesRelease(event: EpisodesReleaseEvent) { + fun onEpisodesRelease(event: EpisodesReleaseEvent) { val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR) if (currentDay != lastDaySend) { diff --git a/src/main/kotlin/fr/ziedelth/utils/Notifications.kt b/src/main/kotlin/fr/ziedelth/utils/Notifications.kt index 5e7af5d..eb9e62a 100644 --- a/src/main/kotlin/fr/ziedelth/utils/Notifications.kt +++ b/src/main/kotlin/fr/ziedelth/utils/Notifications.kt @@ -10,6 +10,7 @@ import com.google.firebase.messaging.Message import com.google.gson.Gson import fr.ziedelth.entities.Notification import io.ktor.websocket.* +import kotlinx.coroutines.runBlocking import java.io.File import java.io.FileInputStream import java.util.* @@ -17,9 +18,14 @@ import java.util.* object Notifications { class Connection(val session: DefaultWebSocketSession) { val id: UUID = UUID.randomUUID() + + suspend fun send(string: String) { + session.send(Frame.Text(string)) + } } val connections: MutableSet = Collections.synchronizedSet(LinkedHashSet()) + private var initialized = false init { println("Initializing Firebase") @@ -29,25 +35,30 @@ object Notifications { FirebaseApp.initializeApp( FirebaseOptions.builder().setCredentials(GoogleCredentials.fromStream(FileInputStream(file))).build() ) + + initialized = true } println("Firebase initialized") } - suspend fun send(title: String? = null, body: String? = null) { - val json = Gson().toJson(Notification(title, body)) - - FirebaseMessaging.getInstance().send( - Message.builder().setAndroidConfig( - AndroidConfig.builder().setNotification( - AndroidNotification.builder() - .setTitle(title) - .setBody(body).build() - ).build() - ).setTopic("all").build() - ) + fun send(title: String? = null, body: String? = null) { + if (initialized) { + FirebaseMessaging.getInstance().send( + Message.builder().setAndroidConfig( + AndroidConfig.builder().setNotification( + AndroidNotification.builder() + .setTitle(title) + .setBody(body).build() + ).build() + ).setTopic("all").build() + ) + } - connections.forEach { it.session.send(Frame.Text(json)) } + runBlocking { + val json = Gson().toJson(Notification(title, body)) + connections.forEach { it.send(json) } + } } fun addConnection(session: DefaultWebSocketSession): Connection { diff --git a/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt b/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt index 77b192e..521028c 100644 --- a/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt +++ b/src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt @@ -34,6 +34,7 @@ object PluginManager { listener::class.java.methods.filter { it.isAnnotationPresent(EventHandler::class.java) && event::class.java == it.parameters[0]?.type } .forEach { method -> println("Calling event ${event::class.java.simpleName} on ${listener::class.java.name}") + println(method.parameters.map { it.type }.joinToString(" -> ")) try { method.invoke(listener, event)