Android FCM推送通知图标在Oreo设备后台仅显示白圈问题求助
解决Oreo设备FCM后台推送通知图标变白的问题
我之前也碰到过这个一模一样的问题——Oreo(Android 8.0+)上应用前台时FCM通知图标正常,后台就变成白色圆形,折腾了好一阵才找到根源,主要是Oreo引入的通知渠道机制和通知图标规范在搞鬼,给你几个关键解决方案:
1. 先确认通知图标符合Oreo规范
Oreo及以上系统对通知图标有强制要求:必须是透明背景、仅白色前景的图标,任何非白色的部分都会被系统自动转为白色,甚至直接显示默认的白色圆形。
- 建议用Android Studio的
Vector Asset工具创建通知图标,选择“Notification Icon”类型,确保生成的图标只有白色图形+透明背景。 - 检查你的
@drawable/ic_stat_name是否符合这个要求,别用带彩色或不透明背景的图标。
2. 必须正确配置Oreo通知渠道
Oreo开始要求所有通知都归属到某个通知渠道,后台推送时FCM会使用默认渠道(或你创建的渠道),如果渠道没指定图标,就会覆盖Manifest里的默认配置,显示白色圆形。
代码示例(创建带图标的通知渠道)
Kotlin:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelId = "app_default_channel" // 自定义渠道ID,要和FCM推送时指定的一致 val channelName = "默认通知" val channelDesc = "应用所有通知的默认渠道" val importance = NotificationManager.IMPORTANCE_DEFAULT val notificationChannel = NotificationChannel(channelId, channelName, importance).apply { description = channelDesc setSmallIcon(R.drawable.ic_stat_name) // 关键:给渠道设置和Manifest一致的图标 setShowBadge(true) // 可选:设置通知声音、震动等 } val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(notificationChannel) }
Java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "app_default_channel"; CharSequence channelName = "默认通知"; String channelDesc = "应用所有通知的默认渠道"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance); notificationChannel.setDescription(channelDesc); notificationChannel.setSmallIcon(R.drawable.ic_stat_name); // 关键设置图标 notificationChannel.setShowBadge(true); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(notificationChannel); }
注意:
- 这段代码要在应用启动时执行(比如
Application类的onCreate里),确保渠道在推送前就创建好。 - 如果是通过Firebase控制台发送通知,要在“高级选项”里指定这个
channelId;如果是后端API发送,要在通知 payload 里加上"channel_id": "app_default_channel"字段。
3. 检查Manifest配置的正确性
确保你的meta-data标签是放在<application>标签内部,而不是某个<activity>里:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- FCM默认通知图标配置 --> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_stat_name" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/notcolor" /> <!-- 自定义Firebase消息服务(如果有的话) --> <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <!-- 其他Activity等配置 --> </application>
4. 自定义消息接收时的处理(如果用数据消息)
如果你的FCM推送是数据消息(不是通知消息),需要在MyFirebaseMessagingService的onMessageReceived里手动构建通知,这时候一定要记得设置setSmallIcon:
override fun onMessageReceived(remoteMessage: RemoteMessage) { super.onMessageReceived(remoteMessage) // 构建自定义通知 val notificationBuilder = NotificationCompat.Builder(this, "app_default_channel") .setSmallIcon(R.drawable.ic_stat_name) // 必须设置,否则后台会显示白图标 .setContentTitle(remoteMessage.data["title"]) .setContentText(remoteMessage.data["body"]) .setColor(ContextCompat.getColor(this, R.color.notcolor)) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_DEFAULT) val notificationManager = NotificationManagerCompat.from(this) notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build()) }
按这几步排查下来,应该就能解决Oreo上后台通知图标变白的问题了。
内容的提问来源于stack exchange,提问作者Abhishek c
相关产品推荐
相关产品推荐

