FCM background notifications do not trigger custom Analytics event
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 领域
- mobile-dev
调研方向
从 MyFirebaseMessagingService.onMessageReceived 开始,比较复现步骤中描述的 notification 和 data payload 行为。检查 Firebase Messaging 后台传递和 Analytics 上传文档,然后验证应用返回前台后报告的事件是否出现。完成的要求是确认原因,或得到一个更清晰、可复现的 quickstart 问题。
由索引模型根据 Issue 内容生成。
描述
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 overridesonMessageReceived. - In
onMessageReceived, log a custom event usingFirebaseAnalytics.logEvent(...)when a data payload is present. - Send an FCM message from the Firebase Console or a server with both
notificationanddatapayloads. - Put the app in the background.
- Wait for the notification to arrive, then check Firebase DebugView for the custom event.
Observed Results
- The
onMessageReceivedmethod 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
onMessageReceivedis 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
package com.example.myapplication
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.example.myapplication.extension.isAppInForeground
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val firebaseAnalytics: FirebaseAnalytics by lazy {
Firebase.analytics
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Log data payload
if (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")
}
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d("FCM", "New FCM Token: $token")
}
private fun showNotification(
remoteMessage: RemoteMessage,
operatorName: String,
subDomain: String
) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val 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_TASK or Intent.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())
}
private fun logEventToFirebase(eventName: String, operatorName: String, subDomain: String) {
val params = Bundle().apply {
putString("operator_name", operatorName)
putString("sub_domain", subDomain)
}
firebaseAnalytics.logEvent(eventName, params)
}
}
- 主要语言
- Kotlin
- 星标
- 9.3k
- 派生
- 7.4k
- 平均合并
- 7 天 9 小时
- 30 天内合并 PR
- 6
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
firebase/quickstart-android 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 35/100
firebase/quickstart-android#2756 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
firebase/quickstart-android#2754 · 5 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 32/100
firebase/quickstart-android#2668 ·
-
难度 4/5 3-5 天 新手友好度 25/100
firebase/quickstart-android#1669 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
firebase/quickstart-android#1580 · 1 条评论 ·
查看 firebase/quickstart-android 的全部 Issue
相似的 Issue
-
android:plan main:plan packages:learn
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 1 天内回复
-
Needs Triage
难度 2/5 1-3 小时 新手友好度 72/100
ankidroid/Anki-Android#23453 ·
维护者通常 1 天内回复
-
afk-ok area:data-quality importer size:S
难度 2/5 1-3 小时 新手友好度 78/100
enorm-labs/event-junkie#5278 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
bazel-contrib/rules_kotlin#1846 ·
维护者通常 2 天内回复