FCM通知异常:点击单条通知后其余通知消失的问题求助
解决FCM点击单条通知后其他通知消失的问题
这个问题的核心原因是通知的唯一标识重复、分组策略配置不当,或者跳转逻辑导致通知栈被清除,以下是具体的解决步骤:
为每条通知分配唯一的
notificationId
客户端显示通知时,必须确保每条通知使用独一无二的ID,避免点击一条时清除所有同ID的通知。可以直接用推送内容里的帖子ID作为notificationId,或者用时间戳、哈希值生成唯一标识:// 从FCM的data字段提取帖子唯一ID val postId = remoteMessage.data["post_id"] ?: System.currentTimeMillis().toString() val notificationId = postId.toIntOrNull() ?: postId.hashCode() // 调用NotificationManager显示通知 val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(notificationId, notificationBuilder.build())调整通知分组策略
如果你的通知使用了分组功能,检查是否设置了点击组内单条通知就清除整个组的逻辑。如果不需要分组,直接移除setGroup相关代码;如果需要分组,确保设置正确的告警行为:val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(notificationTitle) .setContentText(notificationContent) .setGroup("APP_POST_NOTIFICATIONS") // 统一分组标识 .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN) // 每条通知独立告警 // 其他通知配置(图标、优先级等)修正跳转PendingIntent的配置
检查跳转页面的PendingIntent标志,避免因启动模式导致通知被意外清除。推荐使用FLAG_IMMUTABLE保证安全性,同时配合合适的Activity启动标志:val intent = Intent(this, PostDetailActivity::class.java) intent.putExtra("target_post_id", postId) // 避免清除现有任务栈,保持通知栏状态 intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP val pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) notificationBuilder.setContentIntent(pendingIntent)检查FCM消息的
collapse_key设置
如果后端发送FCM时设置了相同的collapse_key,系统会自动折叠同key的通知。确保每条通知的collapse_key唯一,或者直接不设置该字段(默认情况下不会折叠通知)。
内容的提问来源于stack exchange,提问作者Ralf Schneider
相关产品推荐
相关产品推荐

