Skip to content

Commit fca6bec

Browse files
committed
Merge branch 'master' of https://github.com/GitLiveApp/firebase-kotlin-sdk into feat/Documentation
2 parents 4c7bb6d + 339dbd6 commit fca6bec

File tree

7 files changed

+95
-5
lines changed
  • firebase-messaging/src
  • firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage

7 files changed

+95
-5
lines changed

firebase-messaging/src/androidMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
package dev.gitlive.firebase.messaging
33

44
import dev.gitlive.firebase.Firebase
5+
import kotlinx.coroutines.tasks.await
56

67
actual val Firebase.messaging: FirebaseMessaging
78
get() = FirebaseMessaging(com.google.firebase.messaging.FirebaseMessaging.getInstance())
89

910
actual 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
}

firebase-messaging/src/commonMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,22 @@ expect val Firebase.messaging: FirebaseMessaging
1010
* Top level [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
1111
* singleton that provides methods for subscribing to topics and sending upstream messages.
1212
*/
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+
}

firebase-messaging/src/iosMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,44 @@ package dev.gitlive.firebase.messaging
22

33
import cocoapods.FirebaseMessaging.FIRMessaging
44
import dev.gitlive.firebase.Firebase
5+
import kotlinx.coroutines.CompletableDeferred
6+
import platform.Foundation.NSError
57

68
actual val Firebase.messaging: FirebaseMessaging
79
get() = FirebaseMessaging(FIRMessaging.messaging())
810

911
actual 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
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package dev.gitlive.firebase.messaging.externals
22

33
import dev.gitlive.firebase.externals.FirebaseApp
4+
import kotlin.js.Promise
45

56
external fun getMessaging(
67
app: FirebaseApp? = definedExternally,
78
): Messaging
89

10+
external fun getToken(messaging: Messaging = definedExternally, options: dynamic = definedExternally): Promise<String>
11+
912
external interface Messaging

firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ package dev.gitlive.firebase.messaging
33
import dev.gitlive.firebase.Firebase
44
import dev.gitlive.firebase.messaging.externals.Messaging
55
import dev.gitlive.firebase.messaging.externals.getMessaging
6+
import kotlinx.coroutines.await
7+
68

79
actual val Firebase.messaging: FirebaseMessaging
810
get() = FirebaseMessaging(getMessaging())
911

1012
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+
}
1124

25+
actual suspend fun getToken(): String = dev.gitlive.firebase.messaging.externals.getToken(js).await()
1226
}

firebase-messaging/src/jvmMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@ import dev.gitlive.firebase.Firebase
66
actual 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+
}

firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage/storage.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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")

0 commit comments

Comments
 (0)