Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ dependencies {
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.ai)
implementation(libs.datastore)
implementation(libs.firebase.messaging)

implementation(libs.adaptive.navigation3)
implementation(libs.navigation3.runtime)
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
android:value="" />
</service>

<service android:name=".fcm.MessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.KeyboardShortcutGroup
import android.view.KeyboardShortcutInfo
Expand All @@ -30,8 +31,10 @@ import androidx.activity.enableEdgeToEdge
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.glance.appwidget.updateAll
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.samples.socialite.ui.Main
import com.google.android.samples.socialite.widget.SociaLiteAppWidget
import com.google.firebase.messaging.FirebaseMessaging
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

Expand All @@ -40,6 +43,7 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
initializeFcm()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false
}
Expand All @@ -55,6 +59,25 @@ class MainActivity : ComponentActivity() {
}
}

/**
* Sets up Firebase Cloud Messaging (FCM) for push notifications.
* FCM enables cross device message delivery and versatile message delivery.
* See https://firebase.google.com/docs/cloud-messaging/android/get-started.
*/
private fun initializeFcm() {
FirebaseMessaging.getInstance().token.addOnCompleteListener(
OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("FCM", "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
} // Get new FCM registration token
val token = task.result
// Log token, for testing purposes only.
// Log.d("FCM", "FCM message token $token")
},
)
}

private fun extractAppArgs(intent: Intent?): AppArgs? {
if (intent == null) return null
return AppArgs.ShortcutParams.tryFrom(intent) ?: AppArgs.LaunchParams.tryFrom(intent)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.samples.socialite.fcm

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MessagingService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
// Token used for device targeting.
// See https://firebase.google.com/docs/cloud-messaging/android/get-started#access-fcm-registration-token
super.onNewToken(token)
}

override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)

// Handle data payload
if (remoteMessage.data.isNotEmpty()) {
// Log.d("FCM", "Message data payload: ${remoteMessage.data}")
}

// Handle notification payload
remoteMessage.notification?.let {
// Log.d("FCM", "Message Notification Body: ${it.body}")
// Trigger local notification here
}
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins {
alias(libs.plugins.spotless) apply false
alias(libs.plugins.hilt) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.google.gms.google.services) apply false
}

subprojects {
Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ core = "1.16.0"
core-performance = "1.0.0"
core-splashscreen = "1.0.1"
espresso = "3.6.1"
firebaseBoM = "33.14.0"
googleGmsGoogleServices = "4.4.2"
firebaseBoM = "34.10.0"
googleGmsGoogleServices = "4.4.4"
download = "5.6.0"
graphics = "1.0.1"
hilt = "2.56.1"
Expand Down Expand Up @@ -145,6 +145,8 @@ generativeai = { group = "com.google.ai.client.generativeai", name = "generative
datastore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore" }
firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.ref = "firebaseBoM" }
firebase-ai = { group = "com.google.firebase", name = "firebase-ai" }
firebase-analytics = { group = "com.google.firebase", name = "firebase-analytics" }
firebase-messaging = {group = "com.google.firebase", name = "firebase-messaging" }
vision-common = { group = "com.google.mlkit", name = "vision-common", version.ref = "visionCommon" }


Expand Down
Loading