Skip to Content
Notifications

Firebase Cloud Messaging

Firebase Cloud Messaging (FCM) is a cross-platform cloud service for messages and notifications offered by Google. It provides a reliable and efficient way to send messages from your server to mobile applications (Android, iOS) and web applications. FCM acts as a middleman, facilitating communication between your server and client applications.

Usage

Dependencies

Within your app module’s build.gradle.kts file, incorporate the following dependencies:

implementation(project(":core:common")) implementation(project(":notifications:fcm"))

Example

The rememberPermissionRequester function, readily available within your composable functions, streamlines the process of requesting permissions. This function simplifies the process of requesting permissions and handling the user’s response.

Simply attach this function to a button action and define a callback to manage success or error scenarios.

val permissionRequester = rememberPermissionRequester(object: PermissionCallback { override fun onPermissionGranted() { // Permission granted } override fun onPermissionDenied() { // Permission denied } })

Now you can request your permission, for example the POST_NOTIFICATIONS permission which is required for Firebase Cloud Messaging, like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { permissionRequester.requestPermission(android.Manifest.permission.POST_NOTIFICATIONS) }

Your notification token will automatically be sent to Firebase when the permission is granted. This makes it possible for your app to receive notifications.

You can also manually handle multiple events like: onNewToken, onMessageReceived, onMessageSent, etc… by using the AppFirebaseMessagingService class. For this to work however you must define it in your AndroidManifest.xml file in the :notifications:fcm module:

<!-- (optional) Default Icon if none is given --> <!-- <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_launcher_foreground" /> --> <!-- (optional) Default Color if none is given --> <!-- <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@android:color/black" /> --> <!-- (optional) Default Channel if none is given --> <!-- <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" /> -->

More information on Firebase Cloud Messaging can be found in the official documentation.