Android端点击Firebase通知如何获取标题内容,及访问消息变量?
Firebase Android通知:消息变量访问与点击内容获取方案
一、接收端访问消息变量
分应用前台、后台/杀死两种场景处理:
1. 应用前台时获取
应用在前台时,FirebaseMessagingService的onMessageReceived会直接触发,所有消息数据都能在这里拿到:
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) { // 获取通知标题和内容 val title = remoteMessage.notification?.title val body = remoteMessage.notification?.body // 获取自定义数据字段(发送时需携带data参数) val customData = remoteMessage.data // 这是一个Map<String, String> val customValue = customData["your_custom_key"] // 直接用这些变量做其他业务逻辑,比如存本地、更新UI都可以 } }
提醒:如果是Firebase控制台发通知,得手动添加data字段;用API发送的话,要确保同时带上notification和data字段,或者直接发送纯data消息。
2. 应用后台/被杀死时获取
这种情况下系统会自动弹出通知,onMessageReceived不会触发(纯data消息除外)。要获取数据的话,在通知打开的Activity里读取Intent的extras:
// 目标Activity的onCreate方法中 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_target) intent.extras?.let { extras -> // 获取系统传递的通知标题和内容 val title = extras.getString("google.firebase.notification.title") val body = extras.getString("google.firebase.notification.body") // 获取自定义数据 val customValue = extras.getString("your_custom_key") // 直接处理这些数据即可 } }
如果发送的是纯data消息,不管应用处于什么状态,onMessageReceived都会触发,这时可以自行构建通知,同时直接使用或存储数据。
二、点击通知时获取标题与内容
1. 自定义通知构建(推荐)
自己在onMessageReceived里构建通知,把标题和内容塞进PendingIntent,点击后就能在目标Activity拿到:
override fun onMessageReceived(remoteMessage: RemoteMessage) { val title = remoteMessage.notification?.title ?: "默认标题" val body = remoteMessage.notification?.body ?: "默认内容" // 创建Intent,携带标题和内容 val intent = Intent(this, TargetActivity::class.java).apply { putExtra("NOTIFICATION_TITLE", title) putExtra("NOTIFICATION_BODY", body) flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) // 构建并显示通知 val notificationBuilder = NotificationCompat.Builder(this, YOUR_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true) NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notificationBuilder.build()) }
然后在TargetActivity中读取:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_target) val title = intent.getStringExtra("NOTIFICATION_TITLE") val body = intent.getStringExtra("NOTIFICATION_BODY") // 直接使用这些值即可 }
2. 系统自动通知的内容获取
如果是系统自动弹出的通知(后台/杀死状态),在打开的Activity里通过Intent extras获取系统自带的字段:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) intent.extras?.run { val title = getString("google.firebase.notification.title") val body = getString("google.firebase.notification.body") // 处理数据 } }
要是拿不准字段名,直接打印所有extras的键值对确认:
intent.extras?.keySet()?.forEach { key -> Log.d("NotificationData", "$key: ${intent.extras?.get(key)}") }
内容的提问来源于stack exchange,提问作者Devender kumar Chaursia
相关产品推荐
相关产品推荐

