You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改flutter_local_notification通知的背景颜色?

解决flutter_local_notification通知背景色不生效的问题

你遇到的问题核心在于通知通道一旦创建就无法修改属性,再加上colorized通知有版本兼容性要求,按以下步骤排查解决:

1. 确认Android版本兼容性

带背景色的colorized通知仅支持Android 8.0(API 26)及以上版本,先确保测试设备满足这个要求。

2. 重置通知通道(关键操作)

如果之前已经用同一个CHANNEL_ID创建过通知通道,旧通道的属性会被系统保留,后续代码修改不会生效。解决方式二选一:

  • 直接卸载应用后重装,让系统重新创建符合新配置的通道
  • 在代码中主动删除旧通道再创建新通知:
    // 初始化flutterLocalNotificationsPlugin后执行
    await flutterLocalNotificationsPlugin.deleteNotificationChannel('CHANNEL_ID');
    // 之后再调用show方法显示通知,系统会自动创建新通道
    

3. 代码细节确认

你的代码已设置colorized: true,但可以再检查以下细节:

  • 确保颜色值为不透明的ARGB格式(比如Color.fromARGB(255, 135, 206, 250),第一个参数设为255表示完全不透明)
  • 确认importance为Importance.max或Importance.high(你的代码已满足)

调整后的完整代码示例:

AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
  'CHANNEL_ID',
  'CHANNEL_NAME',
  channelDescription: 'CHANNEL_DESCRIPTION',
  importance: Importance.max,
  priority: Priority.high,
  icon: 'logo',
  sound: const RawResourceAndroidNotificationSound('notification_sound'),
  styleInformation: const DefaultStyleInformation(true, true),
  playSound: true,
  // 使用不透明浅蓝色作为背景色
  color: const Color.fromARGB(255, 135, 206, 250),
  largeIcon: const DrawableResourceAndroidBitmap('logo'),
  // 必须开启此参数才能让背景色生效
  colorized: true,
);

// 显示通知
await flutterLocalNotificationsPlugin.show(
  0,
  '通知标题',
  '通知内容',
  NotificationDetails(android: androidNotificationDetails),
);

额外提示

  • 通知图标建议使用白色纯色图标,符合Material Design规范,在彩色背景上显示更清晰
  • 部分定制化Android系统可能对通知样式有特殊处理,若以上步骤无效,可尝试添加category: NotificationCategory.message这类属性测试

内容的提问来源于stack exchange,提问作者Sania Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 05:35:10