Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Fix problem when testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Feb 6, 2023
1 parent 13dd70f commit 120b9b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/ziedelth/listeners/EpisodesRelease.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EpisodesRelease : Listener {
private var lastSend = mutableListOf<UUID>()

@EventHandler
suspend fun onEpisodesRelease(event: EpisodesReleaseEvent) {
fun onEpisodesRelease(event: EpisodesReleaseEvent) {
val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR)

if (currentDay != lastDaySend) {
Expand Down
37 changes: 24 additions & 13 deletions src/main/kotlin/fr/ziedelth/utils/Notifications.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ 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.*

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<Connection> = Collections.synchronizedSet(LinkedHashSet())
private var initialized = false

init {
println("Initializing Firebase")
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 120b9b4

Please sign in to comment.