You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
I am trying to log a custom Analytics event from onMessageReceived in my FirebaseMessagingService when a background notification is delivered. However, the custom event is never showing up in Firebase DebugView or the Analytics dashboard. I suspect that onMessageReceived is not being called in the background or that the payload might not be set up correctly. I’d like help diagnosing why the custom event is not appearing.
Step 1: Describe your environment
Android device: Pixel 5 (example)
Android OS version: 12 (example)
Google Play Services version: 22.12.13 (example)
Firebase/Play Services SDK version:
Firebase Messaging: 23.0.0
Firebase Analytics: 21.2.0
(examples—replace with your actual versions)
Step 2: Describe the problem
Steps to reproduce
Create a subclass of FirebaseMessagingService (e.g., MyFirebaseMessagingService) that overrides onMessageReceived.
In onMessageReceived, log a custom event using FirebaseAnalytics.logEvent(...) when a data payload is present.
Send an FCM message from the Firebase Console or a server with both notification and data payloads.
Put the app in the background.
Wait for the notification to arrive, then check Firebase DebugView for the custom event.
Observed Results
The onMessageReceived method seems to run when the app is in the foreground (and the custom event appears in DebugView).
When the app is in the background, the custom event (notification_received_background) never appears in DebugView or in the Analytics dashboard.
It appears that onMessageReceived is not being triggered, or the event is not being uploaded in the background.
Expected Results
Even if the notification is received while the app is in the background, the custom event (notification_received_background) should be logged and appear in DebugView (after the app comes to the foreground to upload events).
Relevant Code
packagecom.example.myapplicationimportandroid.app.NotificationChannelimportandroid.app.NotificationManagerimportandroid.app.PendingIntentimportandroid.content.Contextimportandroid.content.Intentimportandroid.os.Buildimportandroid.os.Bundleimportandroid.util.Logimportandroidx.core.app.NotificationCompatimportcom.google.firebase.analytics.FirebaseAnalyticsimportcom.google.firebase.analytics.ktx.analyticsimportcom.google.firebase.ktx.Firebaseimportcom.google.firebase.messaging.FirebaseMessagingServiceimportcom.google.firebase.messaging.RemoteMessageimportcom.example.myapplication.extension.isAppInForegroundclassMyFirebaseMessagingService : FirebaseMessagingService() {
privateval firebaseAnalytics:FirebaseAnalytics by lazy {
Firebase.analytics
}
overridefunonMessageReceived(remoteMessage:RemoteMessage) {
// Log data payloadif (remoteMessage.data.isNotEmpty()) {
Log.d("FirebaseMessagingService", "Data payload: ${remoteMessage.data}")
// Log custom event
logEventToFirebase("notification_received_background", "Android", "google.com")
}
val isInForeground = applicationContext.isAppInForeground()
if (isInForeground) {
logEventToFirebase("notification_user_foreground", "Android", "google.com")
}
// Show a local notification
showNotification(remoteMessage, "Android", "google.com")
}
overridefunonNewToken(token:String) {
super.onNewToken(token)
Log.d("FCM", "New FCM Token: $token")
}
privatefunshowNotification(
remoteMessage:RemoteMessage,
operatorName:String,
subDomain:String
) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) asNotificationManagerval channelId ="default_channel"if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
val channel =NotificationChannel(
channelId,
"Default Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}
val tapIntent =Intent(this, MainActivity::class.java).apply {
flags =Intent.FLAG_ACTIVITY_NEW_TASKorIntent.FLAG_ACTIVITY_CLEAR_TASK
putExtra("operator_name", operatorName)
putExtra("sub_domain", subDomain)
}
val pendingTapIntent =PendingIntent.getActivity(
this, 0, tapIntent, PendingIntent.FLAG_IMMUTABLE
)
val dismissIntent =Intent(this, NotificationDismissReceiver::class.java)
val pendingDismissIntent =PendingIntent.getBroadcast(
this, 1, dismissIntent, PendingIntent.FLAG_IMMUTABLE
)
val builder =NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(remoteMessage.data["title"] ?:"Title")
.setContentText(remoteMessage.data["body"] ?:"Body")
.setContentIntent(pendingTapIntent)
.setDeleteIntent(pendingDismissIntent)
.setAutoCancel(true)
notificationManager.notify(0, builder.build())
}
privatefunlogEventToFirebase(eventName:String, operatorName:String, subDomain:String) {
val params =Bundle().apply {
putString("operator_name", operatorName)
putString("sub_domain", subDomain)
}
firebaseAnalytics.logEvent(eventName, params)
}
}
The text was updated successfully, but these errors were encountered:
Description
I am trying to log a custom Analytics event from
onMessageReceived
in myFirebaseMessagingService
when a background notification is delivered. However, the custom event is never showing up in Firebase DebugView or the Analytics dashboard. I suspect thatonMessageReceived
is not being called in the background or that the payload might not be set up correctly. I’d like help diagnosing why the custom event is not appearing.Step 1: Describe your environment
(examples—replace with your actual versions)
Step 2: Describe the problem
Steps to reproduce
FirebaseMessagingService
(e.g.,MyFirebaseMessagingService
) that overridesonMessageReceived
.onMessageReceived
, log a custom event usingFirebaseAnalytics.logEvent(...)
when a data payload is present.notification
anddata
payloads.Observed Results
onMessageReceived
method seems to run when the app is in the foreground (and the custom event appears in DebugView).notification_received_background
) never appears in DebugView or in the Analytics dashboard.onMessageReceived
is not being triggered, or the event is not being uploaded in the background.Expected Results
notification_received_background
) should be logged and appear in DebugView (after the app comes to the foreground to upload events).Relevant Code
The text was updated successfully, but these errors were encountered: