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

Flutter中如何在flutter_local_notifications显示应用自定义图标?

解决Flutter Local Notifications在Android上不显示自定义图标的问题

问题分析

使用flutter_local_notifications发送本地通知功能正常,但通知栏无法显示自定义图标,核心原因多为图标资源不符合Android通知规范或代码配置存在遗漏。

解决方案

1. 准备合规的通知图标

Android通知图标必须满足以下要求:

  • 背景完全透明
  • 采用单色设计(推荐白色,系统会根据通知栏主题自动适配颜色)
  • 将图标文件(如命名为ic_notification)放置在res/drawable-xxxhdpi、res/drawable-xxhdpi等对应分辨率的资源目录下(或mipmap目录),确保不同设备都能适配显示

2. 修正代码中的图标配置

  • 普通通知图标调整:将AndroidNotificationDetails中的icon字段改为图标名称(无需添加路径前缀):
await flutterLocalNotificationsPlugin.show(
  notificationId,
  'FitJerk',
  todayQuote,
  const NotificationDetails(
    android: AndroidNotificationDetails(
      notificationChannelId,
      'MY FOREGROUND SERVICE',
      icon: 'ic_notification', // 替换为你的通知图标名称
      ongoing: false,
    ),
  ),
);
  • 前台服务通知图标补充:在AndroidConfiguration中添加icon配置,前台服务的通知同样需要指定自定义图标:
await service.configure(
  androidConfiguration: AndroidConfiguration(
    onStart: onStart,
    autoStart: true,
    isForegroundMode: true,
    notificationChannelId: notificationChannelId,
    initialNotificationTitle: 'FitJerk',
    initialNotificationContent: 'Welcome to FitJerk',
    foregroundServiceNotificationId: notificationId,
    icon: 'ic_notification', // 添加此行配置前台服务通知图标
  ), 
  iosConfiguration: IosConfiguration(),
);

3. 清理通知通道缓存

若修改后图标仍不显示,需清除旧的通知通道缓存:

  • 卸载应用后重新安装
  • 或在系统设置的「应用通知」中找到fit_jerk通道,删除后重启应用

关键注意点

  • 禁止直接使用应用启动图标(ic_launcher)作为通知图标,启动图标多带彩色背景,不符合Android通知规范,会被系统替换为默认图标
  • 确保所有分辨率资源目录下都放置了对应尺寸的通知图标,避免不同设备显示异常

内容的提问来源于stack exchange,提问作者Kafiul Islam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 21:50:31