Skip to content

Commit e7ec7dc

Browse files
authored
Merge pull request #191 from alabiaga/fcm
Changes to enable Firebase cloud messaging (FCM)
2 parents df62288 + 7f09ecc commit e7ec7dc

6 files changed

Lines changed: 79 additions & 2 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ dependencies {
186186
implementation(platform(libs.firebase.bom))
187187
implementation(libs.firebase.ai)
188188
implementation(libs.datastore)
189+
implementation(libs.firebase.messaging)
189190

190191
implementation(libs.adaptive.navigation3)
191192
implementation(libs.navigation3.runtime)

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@
199199
android:value="" />
200200
</service>
201201

202+
<service android:name=".fcm.MessagingService" android:exported="false">
203+
<intent-filter>
204+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
205+
</intent-filter>
206+
</service>
207+
202208
</application>
203209

204210
</manifest>

app/src/main/java/com/google/android/samples/socialite/MainActivity.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Intent
2020
import android.content.pm.PackageManager
2121
import android.os.Build
2222
import android.os.Bundle
23+
import android.util.Log
2324
import android.view.KeyEvent
2425
import android.view.KeyboardShortcutGroup
2526
import android.view.KeyboardShortcutInfo
@@ -30,8 +31,10 @@ import androidx.activity.enableEdgeToEdge
3031
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
3132
import androidx.glance.appwidget.updateAll
3233
import androidx.lifecycle.lifecycleScope
34+
import com.google.android.gms.tasks.OnCompleteListener
3335
import com.google.android.samples.socialite.ui.Main
3436
import com.google.android.samples.socialite.widget.SociaLiteAppWidget
37+
import com.google.firebase.messaging.FirebaseMessaging
3538
import dagger.hilt.android.AndroidEntryPoint
3639
import kotlinx.coroutines.launch
3740

@@ -40,6 +43,7 @@ class MainActivity : ComponentActivity() {
4043
override fun onCreate(savedInstanceState: Bundle?) {
4144
installSplashScreen()
4245
enableEdgeToEdge()
46+
initializeFcm()
4347
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
4448
window.isNavigationBarContrastEnforced = false
4549
}
@@ -55,6 +59,25 @@ class MainActivity : ComponentActivity() {
5559
}
5660
}
5761

62+
/**
63+
* Sets up Firebase Cloud Messaging (FCM) for push notifications.
64+
* FCM enables cross device message delivery and versatile message delivery.
65+
* See https://firebase.google.com/docs/cloud-messaging/android/get-started.
66+
*/
67+
private fun initializeFcm() {
68+
FirebaseMessaging.getInstance().token.addOnCompleteListener(
69+
OnCompleteListener { task ->
70+
if (!task.isSuccessful) {
71+
Log.w("FCM", "Fetching FCM registration token failed", task.exception)
72+
return@OnCompleteListener
73+
} // Get new FCM registration token
74+
val token = task.result
75+
// Log token, for testing purposes only.
76+
// Log.d("FCM", "FCM message token $token")
77+
},
78+
)
79+
}
80+
5881
private fun extractAppArgs(intent: Intent?): AppArgs? {
5982
if (intent == null) return null
6083
return AppArgs.ShortcutParams.tryFrom(intent) ?: AppArgs.LaunchParams.tryFrom(intent)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2026 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.samples.socialite.fcm
18+
19+
import com.google.firebase.messaging.FirebaseMessagingService
20+
import com.google.firebase.messaging.RemoteMessage
21+
22+
class MessagingService : FirebaseMessagingService() {
23+
24+
override fun onNewToken(token: String) {
25+
// Token used for device targeting.
26+
// See https://firebase.google.com/docs/cloud-messaging/android/get-started#access-fcm-registration-token
27+
super.onNewToken(token)
28+
}
29+
30+
override fun onMessageReceived(remoteMessage: RemoteMessage) {
31+
super.onMessageReceived(remoteMessage)
32+
33+
// Handle data payload
34+
if (remoteMessage.data.isNotEmpty()) {
35+
// Log.d("FCM", "Message data payload: ${remoteMessage.data}")
36+
}
37+
38+
// Handle notification payload
39+
remoteMessage.notification?.let {
40+
// Log.d("FCM", "Message Notification Body: ${it.body}")
41+
// Trigger local notification here
42+
}
43+
}
44+
}

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ plugins {
2626
alias(libs.plugins.spotless) apply false
2727
alias(libs.plugins.hilt) apply false
2828
alias(libs.plugins.ksp) apply false
29+
alias(libs.plugins.google.gms.google.services) apply false
2930
}
3031

3132
subprojects {

gradle/libs.versions.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ core = "1.16.0"
3030
core-performance = "1.0.0"
3131
core-splashscreen = "1.0.1"
3232
espresso = "3.6.1"
33-
firebaseBoM = "33.14.0"
34-
googleGmsGoogleServices = "4.4.2"
33+
firebaseBoM = "34.10.0"
34+
googleGmsGoogleServices = "4.4.4"
3535
download = "5.6.0"
3636
graphics = "1.0.1"
3737
hilt = "2.56.1"
@@ -145,6 +145,8 @@ generativeai = { group = "com.google.ai.client.generativeai", name = "generative
145145
datastore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore" }
146146
firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.ref = "firebaseBoM" }
147147
firebase-ai = { group = "com.google.firebase", name = "firebase-ai" }
148+
firebase-analytics = { group = "com.google.firebase", name = "firebase-analytics" }
149+
firebase-messaging = {group = "com.google.firebase", name = "firebase-messaging" }
148150
vision-common = { group = "com.google.mlkit", name = "vision-common", version.ref = "visionCommon" }
149151

150152

0 commit comments

Comments
 (0)