File tree Expand file tree Collapse file tree 6 files changed +97
-3
lines changed
androidMain/kotlin/dev/gitlive/firebase/messaging
commonMain/kotlin/dev/gitlive/firebase/messaging
iosMain/kotlin/dev/gitlive/firebase/messaging
jsMain/kotlin/dev/gitlive/firebase/messaging
jvmMain/kotlin/dev/gitlive/firebase/messaging Expand file tree Collapse file tree 6 files changed +97
-3
lines changed Original file line number Diff line number Diff line change 22package dev.gitlive.firebase.messaging
33
44import dev.gitlive.firebase.Firebase
5+ import kotlinx.coroutines.tasks.await
56
67actual val Firebase .messaging: FirebaseMessaging
78 get() = FirebaseMessaging (com.google.firebase.messaging.FirebaseMessaging .getInstance())
89
910actual class FirebaseMessaging (val android : com.google.firebase.messaging.FirebaseMessaging ) {
11+ actual fun subscribeToTopic (topic : String ) {
12+ android.subscribeToTopic(topic)
13+ }
1014
15+ actual fun unsubscribeFromTopic (topic : String ) {
16+ android.unsubscribeFromTopic(topic)
17+ }
18+
19+ actual suspend fun getToken (): String = android.token.await()
1120}
Original file line number Diff line number Diff line change @@ -6,4 +6,22 @@ import dev.gitlive.firebase.FirebaseApp
66/* * Returns the [FirebaseMessaging] instance of the default [FirebaseApp]. */
77expect val Firebase .messaging: FirebaseMessaging
88
9- expect class FirebaseMessaging
9+ expect class FirebaseMessaging {
10+ /* *
11+ * Subscribe to a topic.
12+ * @param topic The topic to subscribe to.
13+ */
14+ fun subscribeToTopic (topic : String )
15+
16+ /* *
17+ * Unsubscribe from a topic.
18+ * @param topic The topic to unsubscribe from.
19+ */
20+ fun unsubscribeFromTopic (topic : String )
21+
22+ /* *
23+ * Get FCM token for client
24+ * @return [String] FCM token
25+ */
26+ suspend fun getToken (): String
27+ }
Original file line number Diff line number Diff line change @@ -2,10 +2,44 @@ package dev.gitlive.firebase.messaging
22
33import cocoapods.FirebaseMessaging.FIRMessaging
44import dev.gitlive.firebase.Firebase
5+ import kotlinx.coroutines.CompletableDeferred
6+ import platform.Foundation.NSError
57
68actual val Firebase .messaging: FirebaseMessaging
79 get() = FirebaseMessaging (FIRMessaging .messaging())
810
911actual class FirebaseMessaging (val ios : FIRMessaging ) {
12+ actual fun subscribeToTopic (topic : String ) {
13+ ios.subscribeToTopic(topic)
14+ }
1015
16+ actual fun unsubscribeFromTopic (topic : String ) {
17+ ios.unsubscribeFromTopic(topic)
18+ }
19+
20+ actual suspend fun getToken (): String = awaitResult { ios.tokenWithCompletion(it) }
21+ }
22+
23+ suspend inline fun <T > T.await (function : T .(callback: (NSError ? ) -> Unit ) -> Unit ) {
24+ val job = CompletableDeferred <Unit >()
25+ function { error ->
26+ if (error == null ) {
27+ job.complete(Unit )
28+ } else {
29+ job.completeExceptionally(Exception (error.toString()))
30+ }
31+ }
32+ job.await()
33+ }
34+
35+ suspend inline fun <T , reified R > T.awaitResult (function : T .(callback: (R ? , NSError ? ) -> Unit ) -> Unit ): R {
36+ val job = CompletableDeferred <R ?>()
37+ function { result, error ->
38+ if (error == null ) {
39+ job.complete(result)
40+ } else {
41+ job.completeExceptionally(Exception (error.toString()))
42+ }
43+ }
44+ return job.await() as R
1145}
Original file line number Diff line number Diff line change 11package dev.gitlive.firebase.messaging.externals
22
33import dev.gitlive.firebase.externals.FirebaseApp
4+ import kotlin.js.Promise
45
56external fun getMessaging (
67 app : FirebaseApp ? = definedExternally,
78): Messaging
89
9- external interface Messaging
10+ external fun getToken (messaging : Messaging = definedExternally, options : dynamic = definedExternally): Promise <String >
11+
12+ external interface Messaging {
13+ fun subscribeToTopic (tokens : Array <String >, topic : String ): Promise <Unit >
14+ fun unsubscribeFromTopic (tokens : Array <String >, topic : String ): Promise <Unit >
15+ }
Original file line number Diff line number Diff line change @@ -3,10 +3,25 @@ package dev.gitlive.firebase.messaging
33import dev.gitlive.firebase.Firebase
44import dev.gitlive.firebase.messaging.externals.Messaging
55import dev.gitlive.firebase.messaging.externals.getMessaging
6+ import kotlinx.coroutines.GlobalScope
7+ import kotlinx.coroutines.await
8+ import kotlinx.coroutines.launch
69
710actual val Firebase .messaging: FirebaseMessaging
811 get() = FirebaseMessaging (getMessaging())
912
1013actual class FirebaseMessaging (val js : Messaging ) {
14+ actual fun subscribeToTopic (topic : String ) {
15+ GlobalScope .launch {
16+ js.subscribeToTopic(arrayOf(getToken()), topic)
17+ }
18+ }
1119
20+ actual fun unsubscribeFromTopic (topic : String ) {
21+ GlobalScope .launch {
22+ js.unsubscribeFromTopic(arrayOf(getToken()), topic)
23+ }
24+ }
25+
26+ actual suspend fun getToken (): String = dev.gitlive.firebase.messaging.externals.getToken(js).await()
1227}
Original file line number Diff line number Diff line change @@ -6,4 +6,16 @@ import dev.gitlive.firebase.Firebase
66actual val Firebase .messaging: FirebaseMessaging
77 get() = TODO (" Not yet implemented" )
88
9- actual class FirebaseMessaging
9+ actual class FirebaseMessaging {
10+ actual fun subscribeToTopic (topic : String ) {
11+ TODO (" Not yet implemented" )
12+ }
13+
14+ actual fun unsubscribeFromTopic (topic : String ) {
15+ TODO (" Not yet implemented" )
16+ }
17+
18+ actual suspend fun getToken (): String {
19+ TODO (" Not yet implemented" )
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments