如何实现FCM通知自动取消?仅接收消息不显示通知栏弹窗
Hey, I’ve dealt with this exact headache before—nothing’s more confusing than seeing messages hit your app but never pop up in the notification tray! Let’s walk through the most reliable fixes and workarounds, since the official docs don’t always highlight these edge cases:
1. Verify Your Message Payload Type
FCM has two core message types, and this is the #1 culprit for missing notifications:
- Notification messages: Handled automatically by the system—they’ll show up in the tray without extra code.
- Data messages: Delivered directly to your app’s logic, and won’t trigger a system notification unless you manually create one.
If you’re sending only a data payload, you need to add a notification section to your request. Example valid payload:
{ "to": "YOUR_DEVICE_TOKEN", "notification": { "title": "Alert!", "body": "You’ve got a new message" }, "data": { "custom_key": "custom_value" } }
2. Double-Check Device Notification Permissions
It sounds obvious, but it’s easy to overlook:
- Android: Go to Settings > Apps > [Your App] > Notifications, and ensure "Allow notifications" is toggled on. Also check that your notification channel isn’t muted or set to low priority.
- iOS: Go to Settings > [Your App] > Notifications, and confirm "Allow Notifications" is enabled, along with the alert styles you want.
3. Fix Android Notification Channel Configuration (API 26+)
Starting with Android Oreo, you must create a valid notification channel and reference it in your FCM payload. If the channel doesn’t exist or has invalid settings, notifications won’t show:
Client-side channel setup (Kotlin example):
val channel = NotificationChannel( "critical_alerts", // Match this ID in your FCM payload "Critical Alerts", NotificationManager.IMPORTANCE_HIGH // Ensure this is set to HIGH or MAX for pop-ups ).apply { description = "Urgent updates from the app" enableVibration(true) } val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel)
Add channel ID to your FCM payload:
{ "to": "YOUR_DEVICE_TOKEN", "notification": { "title": "Alert!", "body": "You’ve got a new message" }, "android": { "notification": { "channel_id": "critical_alerts" } } }
4. Check iOS APNs & Background Settings
For iOS, make sure:
- Your APNs certificates are correctly configured in the Firebase Console.
- Your app has the "Remote notifications" background mode enabled in Xcode (Project > Target > Signing & Capabilities > Background Modes).
- Your payload includes the
apssection with proper alert/sound settings:
{ "to": "YOUR_DEVICE_TOKEN", "notification": { "title": "Alert!", "body": "You’ve got a new message" }, "aps": { "alert": { "title": "Alert!", "body": "You’ve got a new message" }, "sound": "default" } }
5. Rule Out Client-Side Message Interception
If you’ve implemented onMessageReceived (Android) or didReceiveRemoteNotification (iOS), make sure you’re not accidentally suppressing notifications:
- On Android, when handling data messages, you need to explicitly build and show a notification using
NotificationManager. - On iOS, avoid returning
UIBackgroundFetchResult.noDataif you want the system to display the notification.
6. Bypass Device Battery Optimization (Android)
Many OEMs (like Samsung, Xiaomi, Huawei) have aggressive battery optimization that blocks background notifications. To fix this:
- Go to Settings > Battery > Battery Optimization.
- Find your app and set it to "Not optimized".
- Also, ensure the app is allowed to run in the background (Settings > Apps > [Your App] > Battery > Background activity).
If none of these work, check the Firebase Console’s message delivery logs for errors, or use adb logcat (Android) to debug notification events in real time.
内容的提问来源于stack exchange,提问作者JimJay Lee

