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 2
2
package dev.gitlive.firebase.messaging
3
3
4
4
import dev.gitlive.firebase.Firebase
5
+ import kotlinx.coroutines.tasks.await
5
6
6
7
actual val Firebase .messaging: FirebaseMessaging
7
8
get() = FirebaseMessaging (com.google.firebase.messaging.FirebaseMessaging .getInstance())
8
9
9
10
actual class FirebaseMessaging (val android : com.google.firebase.messaging.FirebaseMessaging ) {
11
+ actual fun subscribeToTopic (topic : String ) {
12
+ android.subscribeToTopic(topic)
13
+ }
10
14
15
+ actual fun unsubscribeFromTopic (topic : String ) {
16
+ android.unsubscribeFromTopic(topic)
17
+ }
18
+
19
+ actual suspend fun getToken (): String = android.token.await()
11
20
}
Original file line number Diff line number Diff line change @@ -6,4 +6,22 @@ import dev.gitlive.firebase.FirebaseApp
6
6
/* * Returns the [FirebaseMessaging] instance of the default [FirebaseApp]. */
7
7
expect val Firebase .messaging: FirebaseMessaging
8
8
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
2
2
3
3
import cocoapods.FirebaseMessaging.FIRMessaging
4
4
import dev.gitlive.firebase.Firebase
5
+ import kotlinx.coroutines.CompletableDeferred
6
+ import platform.Foundation.NSError
5
7
6
8
actual val Firebase .messaging: FirebaseMessaging
7
9
get() = FirebaseMessaging (FIRMessaging .messaging())
8
10
9
11
actual class FirebaseMessaging (val ios : FIRMessaging ) {
12
+ actual fun subscribeToTopic (topic : String ) {
13
+ ios.subscribeToTopic(topic)
14
+ }
10
15
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
11
45
}
Original file line number Diff line number Diff line change 1
1
package dev.gitlive.firebase.messaging.externals
2
2
3
3
import dev.gitlive.firebase.externals.FirebaseApp
4
+ import kotlin.js.Promise
4
5
5
6
external fun getMessaging (
6
7
app : FirebaseApp ? = definedExternally,
7
8
): Messaging
8
9
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
3
3
import dev.gitlive.firebase.Firebase
4
4
import dev.gitlive.firebase.messaging.externals.Messaging
5
5
import dev.gitlive.firebase.messaging.externals.getMessaging
6
+ import kotlinx.coroutines.GlobalScope
7
+ import kotlinx.coroutines.await
8
+ import kotlinx.coroutines.launch
6
9
7
10
actual val Firebase .messaging: FirebaseMessaging
8
11
get() = FirebaseMessaging (getMessaging())
9
12
10
13
actual class FirebaseMessaging (val js : Messaging ) {
14
+ actual fun subscribeToTopic (topic : String ) {
15
+ GlobalScope .launch {
16
+ js.subscribeToTopic(arrayOf(getToken()), topic)
17
+ }
18
+ }
11
19
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()
12
27
}
Original file line number Diff line number Diff line change @@ -6,4 +6,16 @@ import dev.gitlive.firebase.Firebase
6
6
actual val Firebase .messaging: FirebaseMessaging
7
7
get() = TODO (" Not yet implemented" )
8
8
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