File tree Expand file tree Collapse file tree 8 files changed +96
-5
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
firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage Expand file tree Collapse file tree 8 files changed +96
-5
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ The following libraries are available for the various Firebase products.
1616
1717| Service or Product | Gradle Dependency | API Coverage |
1818| ---------------------------------------------------------------------------------| :-------------------------------------------------------------------------------------------------------------------------------| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
19+ | [ Analytics] ( https://firebase.google.com/docs/analytics ) | [ ` dev.gitlive:firebase-analytics:1.12.0 ` ] ( https://search.maven.org/artifact/dev.gitlive/firebase-analytics/1.12.0/pom ) | [ ![ 80%] ( https://img.shields.io/badge/-80%25-green?style=flat-square )] ( /firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt ) |
1920| [ Authentication] ( https://firebase.google.com/docs/auth ) | [ ` dev.gitlive:firebase-auth:1.12.0 ` ] ( https://search.maven.org/artifact/dev.gitlive/firebase-auth/1.12.0/pom ) | [ ![ 80%] ( https://img.shields.io/badge/-80%25-green?style=flat-square )] ( /firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt ) |
2021| [ Realtime Database] ( https://firebase.google.com/docs/database ) | [ ` dev.gitlive:firebase-database:1.12.0 ` ] ( https://search.maven.org/artifact/dev.gitlive/firebase-database/1.12.0/pom ) | [ ![ 70%] ( https://img.shields.io/badge/-70%25-orange?style=flat-square )] ( /firebase-database/src/commonMain/kotlin/dev/gitlive/firebase/database/database.kt ) |
2122| [ Cloud Firestore] ( https://firebase.google.com/docs/firestore ) | [ ` dev.gitlive:firebase-firestore:1.12.0 ` ] ( https://search.maven.org/artifact/dev.gitlive/firebase-firestore/1.12.0/pom ) | [ ![ 60%] ( https://img.shields.io/badge/-60%25-orange?style=flat-square )] ( /firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt ) |
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
10+ external fun getToken (messaging : Messaging = definedExternally, options : dynamic = definedExternally): Promise <String >
11+
912external interface Messaging
Original file line number Diff line number Diff line change @@ -3,10 +3,24 @@ 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.await
7+
68
79actual val Firebase .messaging: FirebaseMessaging
810 get() = FirebaseMessaging (getMessaging())
911
1012actual class FirebaseMessaging (val js : Messaging ) {
13+ actual fun subscribeToTopic (topic : String ) {
14+ // This is not supported in the JS SDK
15+ // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging
16+ throw NotImplementedError (" Subscribing to topics is not supported in the JS SDK" )
17+ }
18+
19+ actual fun unsubscribeFromTopic (topic : String ) {
20+ // This is not supported in the JS SDK
21+ // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging
22+ throw NotImplementedError (" Unsubscribing from topics is not supported in the JS SDK" )
23+ }
1124
25+ actual suspend fun getToken (): String = dev.gitlive.firebase.messaging.externals.getToken(js).await()
1226}
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+ }
Original file line number Diff line number Diff line change @@ -64,14 +64,14 @@ class FirebaseStorageTest {
6464 @Test
6565 fun testUploadShouldNotCrash () = runBlockingTest {
6666 val data = createTestData()
67- val ref = storage.reference(" test" ).child(" testFile .txt" )
67+ val ref = storage.reference(" test" ).child(" testUploadShouldNotCrash .txt" )
6868 ref.putData(data)
6969 }
7070
7171 @Test
7272 fun testUploadMetadata () = runBlockingTest {
7373 val data = createTestData()
74- val ref = storage.reference(" test" ).child(" testFile .txt" )
74+ val ref = storage.reference(" test" ).child(" testUploadMetadata .txt" )
7575 val metadata = storageMetadata {
7676 contentType = " text/plain"
7777 }
@@ -87,7 +87,7 @@ class FirebaseStorageTest {
8787 @Test
8888 fun testUploadCustomMetadata () = runBlockingTest {
8989 val data = createTestData()
90- val ref = storage.reference(" test" ).child(" testFile .txt" )
90+ val ref = storage.reference(" test" ).child(" testUploadCustomMetadata .txt" )
9191 val metadata = storageMetadata {
9292 contentType = " text/plain"
9393 setCustomMetadata(" key" , " value" )
You can’t perform that action at this time.
0 commit comments