mirror of
https://github.com/binwiederhier/ntfy-android.git
synced 2024-05-20 20:42:31 +12:00
37 lines
1.1 KiB
Kotlin
37 lines
1.1 KiB
Kotlin
package io.heckel.ntfy.firebase
|
|
|
|
import com.google.firebase.messaging.FirebaseMessaging
|
|
import io.heckel.ntfy.util.Log
|
|
|
|
class FirebaseMessenger {
|
|
fun subscribe(topic: String) {
|
|
val firebase = maybeInstance() ?: return
|
|
firebase
|
|
.subscribeToTopic(topic)
|
|
.addOnCompleteListener {
|
|
Log.d(TAG, "Subscribing to topic $topic complete: result=${it.result}, exception=${it.exception}, successful=${it.isSuccessful}")
|
|
}
|
|
.addOnFailureListener { e ->
|
|
Log.e(TAG, "Subscribing to topic $topic failed: ${e.message}", e)
|
|
}
|
|
}
|
|
|
|
fun unsubscribe(topic: String) {
|
|
val firebase = maybeInstance() ?: return
|
|
firebase.unsubscribeFromTopic(topic)
|
|
}
|
|
|
|
private fun maybeInstance(): FirebaseMessaging? {
|
|
return try {
|
|
FirebaseMessaging.getInstance()
|
|
} catch (e: Exception) {
|
|
Log.e(TAG, "Firebase instance unavailable: ${e.message}", e)
|
|
null
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "NtfyFirebase"
|
|
}
|
|
}
|