File tree Expand file tree Collapse file tree 7 files changed +95
-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 7 files changed +95
-5
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 @@ -10,4 +10,22 @@ expect val Firebase.messaging: FirebaseMessaging
10
10
* Top level [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
11
11
* singleton that provides methods for subscribing to topics and sending upstream messages.
12
12
*/
13
- expect class FirebaseMessaging
13
+ expect class FirebaseMessaging {
14
+ /* *
15
+ * Subscribe to a topic.
16
+ * @param topic The topic to subscribe to.
17
+ */
18
+ fun subscribeToTopic (topic : String )
19
+
20
+ /* *
21
+ * Unsubscribe from a topic.
22
+ * @param topic The topic to unsubscribe from.
23
+ */
24
+ fun unsubscribeFromTopic (topic : String )
25
+
26
+ /* *
27
+ * Get FCM token for client
28
+ * @return [String] FCM token
29
+ */
30
+ suspend fun getToken (): String
31
+ }
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
10
+ external fun getToken (messaging : Messaging = definedExternally, options : dynamic = definedExternally): Promise <String >
11
+
9
12
external interface Messaging
Original file line number Diff line number Diff line change @@ -3,10 +3,24 @@ 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.await
7
+
6
8
7
9
actual val Firebase .messaging: FirebaseMessaging
8
10
get() = FirebaseMessaging (getMessaging())
9
11
10
12
actual 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
+ }
11
24
25
+ actual suspend fun getToken (): String = dev.gitlive.firebase.messaging.externals.getToken(js).await()
12
26
}
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
+ }
Original file line number Diff line number Diff line change @@ -64,14 +64,14 @@ class FirebaseStorageTest {
64
64
@Test
65
65
fun testUploadShouldNotCrash () = runBlockingTest {
66
66
val data = createTestData()
67
- val ref = storage.reference(" test" ).child(" testFile .txt" )
67
+ val ref = storage.reference(" test" ).child(" testUploadShouldNotCrash .txt" )
68
68
ref.putData(data)
69
69
}
70
70
71
71
@Test
72
72
fun testUploadMetadata () = runBlockingTest {
73
73
val data = createTestData()
74
- val ref = storage.reference(" test" ).child(" testFile .txt" )
74
+ val ref = storage.reference(" test" ).child(" testUploadMetadata .txt" )
75
75
val metadata = storageMetadata {
76
76
contentType = " text/plain"
77
77
}
@@ -87,7 +87,7 @@ class FirebaseStorageTest {
87
87
@Test
88
88
fun testUploadCustomMetadata () = runBlockingTest {
89
89
val data = createTestData()
90
- val ref = storage.reference(" test" ).child(" testFile .txt" )
90
+ val ref = storage.reference(" test" ).child(" testUploadCustomMetadata .txt" )
91
91
val metadata = storageMetadata {
92
92
contentType = " text/plain"
93
93
setCustomMetadata(" key" , " value" )
You can’t perform that action at this time.
0 commit comments