
Firebase Admin SDK for service-account credentials, OAuth2 token caching, JWT auth, and FCM messaging with rich notifications, topics/conditions, plus custom HTTP and crypto support.
A Firebase Admin SDK for Kotlin Multiplatform.
Add the dependency to your build.gradle.kts:
dependencies {
// Core module (required for credentials and authentication)
implementation("me.nathanfallet.flareon:core:0.1.2")
// Messaging module (for Firebase Cloud Messaging)
implementation("me.nathanfallet.flareon:messaging:0.1.2")
}Flareon supports all Kotlin Multiplatform targets:
First, obtain a service account JSON file from your Firebase Console:
import me.nathanfallet.flareon.core.credentials.GoogleCredentials
// Load from JSON string (in production, load the string from a secure location like file or environment variable)
val serviceAccountJson = """
{
"type": "service_account",
"project_id": "your-project-id",
"private_key": "-----BEGIN PRIVATE KEY-----\n...",
"client_email": "firebase-adminsdk@your-project.iam.gserviceaccount.com",
"token_uri": "https://oauth2.googleapis.com/token"
}
""".trimIndent()
val credentials = GoogleCredentials.fromJson(serviceAccountJson)import me.nathanfallet.flareon.messaging.FcmService
import me.nathanfallet.flareon.messaging.models.*
// Initialize the service
val messaging = FcmService(credentials)
// Send a simple notification
val response = messaging.sendNotification(
token = "device-registration-token",
title = "Hello from Flareon!",
body = "This is a test notification"
)
println("Message sent: ${response.name}")messaging.sendNotification(
token = "device-token",
title = "New Message",
body = "You have a new message",
data = mapOf(
"userId" to "12345",
"messageId" to "msg_001",
"type" to "chat"
)
)val message = FcmMessage(
topic = "news",
notification = FcmNotification(
title = "Breaking News",
body = "Check out the latest updates",
image = "https://example.com/image.png"
)
)
messaging.sendMessage(message)val message = FcmMessage(
token = "device-token",
notification = FcmNotification(
title = "Platform-Specific Notification",
body = "This notification has custom settings per platform"
),
// Android-specific options
android = AndroidConfig(
priority = AndroidPriority.HIGH,
ttl = "3600s",
notification = AndroidNotification(
channelId = "important_updates",
color = "#FF5722",
sound = "default",
notificationPriority = AndroidNotificationPriority.HIGH,
sticky = true
)
),
// iOS-specific options
apns = ApnsConfig(
payload = ApnsPayload(
aps = Aps(
alert = ApsAlert(
title = "iOS Notification",
body = "Custom alert for iOS",
sound = "default"
),
badge = 1
)
)
),
// Web Push options
webpush = WebpushConfig(
notification = WebpushNotification(
title = "Web Notification",
body = "Custom notification for web",
icon = "https://example.com/icon.png",
badge = "https://example.com/badge.png"
)
)
)
messaging.sendMessage(message)val tokens = listOf(
"token1",
"token2",
"token3"
)
val results = messaging.sendNotificationToMultiple(
tokens = tokens,
title = "Bulk Notification",
body = "Sent to multiple devices"
)
// Handle results
results.forEachIndexed { index, result ->
result.onSuccess { response ->
println("Message ${index + 1} sent: ${response.name}")
}.onFailure { error ->
println("Message ${index + 1} failed: ${error.message}")
}
}val message = FcmMessage(
// Target users subscribed to both 'sports' and 'cricket' topics
condition = "'sports' in topics && 'cricket' in topics",
notification = FcmNotification(
title = "Cricket Match Update",
body = "Your team is winning!"
)
)
messaging.sendMessage(message)The core module provides authentication and credential management:
fromJson) or from the Google Cloud
metadata server (fromMetadataServer)Example:
import me.nathanfallet.flareon.core.credentials.GoogleCredentials
val credentials = GoogleCredentials.fromJson(serviceAccountJson)
val accessToken = credentials.getAccessToken() // Automatically cached and refreshedThe messaging module provides Firebase Cloud Messaging (FCM) functionality:
Example:
import me.nathanfallet.flareon.messaging.FcmService
val messaging = FcmService(credentials)
messaging.sendNotification(
token = "device-token",
title = "Hello",
body = "World"
)On Cloud Run, Compute Engine or GKE, the workload already runs under a service account. Read its tokens from the metadata server instead of shipping a service account JSON key:
val credentials = GoogleCredentials.fromMetadataServer()
val messaging = FcmService(credentials) // project ID read from the metadata serverGrant the service account only what it needs (for FCM, the Firebase Cloud Messaging API Admin role or the
cloudmessaging.messages.create permission). The metadata server is plain HTTP and only reachable from inside Google
Cloud; pass baseUrl to point at an emulator in tests, and scopes to request specific OAuth2 scopes.
You can provide your own Ktor HTTP client for advanced configuration:
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
val customClient = HttpClient {
install(Logging) {
level = LogLevel.ALL
}
}
val credentials = GoogleCredentials.fromJson(
serviceAccountJson,
httpClient = customClient
)
val messaging = FcmService(
credentials = credentials,
httpClient = customClient
)For advanced use cases, you can provide a custom cryptography provider:
import dev.whyoleg.cryptography.CryptographyProvider
val customProvider = CryptographyProvider.Default
val credentials = GoogleCredentials.fromJson(
serviceAccountJson,
cryptographyProvider = customProvider
)A Firebase Admin SDK for Kotlin Multiplatform.
Add the dependency to your build.gradle.kts:
dependencies {
// Core module (required for credentials and authentication)
implementation("me.nathanfallet.flareon:core:0.1.2")
// Messaging module (for Firebase Cloud Messaging)
implementation("me.nathanfallet.flareon:messaging:0.1.2")
}Flareon supports all Kotlin Multiplatform targets:
First, obtain a service account JSON file from your Firebase Console:
import me.nathanfallet.flareon.core.credentials.GoogleCredentials
// Load from JSON string (in production, load the string from a secure location like file or environment variable)
val serviceAccountJson = """
{
"type": "service_account",
"project_id": "your-project-id",
"private_key": "-----BEGIN PRIVATE KEY-----\n...",
"client_email": "firebase-adminsdk@your-project.iam.gserviceaccount.com",
"token_uri": "https://oauth2.googleapis.com/token"
}
""".trimIndent()
val credentials = GoogleCredentials.fromJson(serviceAccountJson)import me.nathanfallet.flareon.messaging.FcmService
import me.nathanfallet.flareon.messaging.models.*
// Initialize the service
val messaging = FcmService(credentials)
// Send a simple notification
val response = messaging.sendNotification(
token = "device-registration-token",
title = "Hello from Flareon!",
body = "This is a test notification"
)
println("Message sent: ${response.name}")messaging.sendNotification(
token = "device-token",
title = "New Message",
body = "You have a new message",
data = mapOf(
"userId" to "12345",
"messageId" to "msg_001",
"type" to "chat"
)
)val message = FcmMessage(
topic = "news",
notification = FcmNotification(
title = "Breaking News",
body = "Check out the latest updates",
image = "https://example.com/image.png"
)
)
messaging.sendMessage(message)val message = FcmMessage(
token = "device-token",
notification = FcmNotification(
title = "Platform-Specific Notification",
body = "This notification has custom settings per platform"
),
// Android-specific options
android = AndroidConfig(
priority = AndroidPriority.HIGH,
ttl = "3600s",
notification = AndroidNotification(
channelId = "important_updates",
color = "#FF5722",
sound = "default",
notificationPriority = AndroidNotificationPriority.HIGH,
sticky = true
)
),
// iOS-specific options
apns = ApnsConfig(
payload = ApnsPayload(
aps = Aps(
alert = ApsAlert(
title = "iOS Notification",
body = "Custom alert for iOS",
sound = "default"
),
badge = 1
)
)
),
// Web Push options
webpush = WebpushConfig(
notification = WebpushNotification(
title = "Web Notification",
body = "Custom notification for web",
icon = "https://example.com/icon.png",
badge = "https://example.com/badge.png"
)
)
)
messaging.sendMessage(message)val tokens = listOf(
"token1",
"token2",
"token3"
)
val results = messaging.sendNotificationToMultiple(
tokens = tokens,
title = "Bulk Notification",
body = "Sent to multiple devices"
)
// Handle results
results.forEachIndexed { index, result ->
result.onSuccess { response ->
println("Message ${index + 1} sent: ${response.name}")
}.onFailure { error ->
println("Message ${index + 1} failed: ${error.message}")
}
}val message = FcmMessage(
// Target users subscribed to both 'sports' and 'cricket' topics
condition = "'sports' in topics && 'cricket' in topics",
notification = FcmNotification(
title = "Cricket Match Update",
body = "Your team is winning!"
)
)
messaging.sendMessage(message)The core module provides authentication and credential management:
fromJson) or from the Google Cloud
metadata server (fromMetadataServer)Example:
import me.nathanfallet.flareon.core.credentials.GoogleCredentials
val credentials = GoogleCredentials.fromJson(serviceAccountJson)
val accessToken = credentials.getAccessToken() // Automatically cached and refreshedThe messaging module provides Firebase Cloud Messaging (FCM) functionality:
Example:
import me.nathanfallet.flareon.messaging.FcmService
val messaging = FcmService(credentials)
messaging.sendNotification(
token = "device-token",
title = "Hello",
body = "World"
)On Cloud Run, Compute Engine or GKE, the workload already runs under a service account. Read its tokens from the metadata server instead of shipping a service account JSON key:
val credentials = GoogleCredentials.fromMetadataServer()
val messaging = FcmService(credentials) // project ID read from the metadata serverGrant the service account only what it needs (for FCM, the Firebase Cloud Messaging API Admin role or the
cloudmessaging.messages.create permission). The metadata server is plain HTTP and only reachable from inside Google
Cloud; pass baseUrl to point at an emulator in tests, and scopes to request specific OAuth2 scopes.
You can provide your own Ktor HTTP client for advanced configuration:
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
val customClient = HttpClient {
install(Logging) {
level = LogLevel.ALL
}
}
val credentials = GoogleCredentials.fromJson(
serviceAccountJson,
httpClient = customClient
)
val messaging = FcmService(
credentials = credentials,
httpClient = customClient
)For advanced use cases, you can provide a custom cryptography provider:
import dev.whyoleg.cryptography.CryptographyProvider
val customProvider = CryptographyProvider.Default
val credentials = GoogleCredentials.fromJson(
serviceAccountJson,
cryptographyProvider = customProvider
)