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

使用flutter_local_notifications遇无效通知(无有效小图标)错误求助

解决Flutter本地通知“无效小图标”异常

我在Flutter中使用flutter_local_notifications插件实现简单通知功能,点击触发通知时抛出了无效小图标的异常,相关代码和问题细节如下:

源码

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class MyScreen extends StatefulWidget {
  const MyScreen({Key? key}) : super(key: key);

  @override
  State<MyScreen> createState() => _MyScreenState();
}

class _MyScreenState extends State<MyScreen> {

  FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
  var initializationSettings;

  @override
  void initState() {
    super.initState();
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    initializePlatformSpecifics();
  }

  initializePlatformSpecifics() {
    var initializationSettingsAndroid = AndroidInitializationSettings('assets/logo128.ico');
    initializationSettings =
        InitializationSettings(android: initializationSettingsAndroid);
  }

  Future<void> showNotification() async {
    var androidChannelSpecifics = AndroidNotificationDetails(
      'CHANNEL_ID',
      'CHANNEL_NAME',
      //"CHANNEL_DESCRIPTION",
      importance: Importance.max,
      priority: Priority.high,
      playSound: true,
      timeoutAfter: 5000,
      styleInformation: DefaultStyleInformation(true, true),
    );
    var platformChannelSpecifics =
        NotificationDetails(android: androidChannelSpecifics);
    await flutterLocalNotificationsPlugin!.show(
      0, // Notification ID
      'Test Title', // Notification Title
      'Test Body', // Notification Body, set as null to remove the body
      platformChannelSpecifics,
      payload: 'New Payload', // Notification Payload
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 239, 241, 249),
      body: GestureDetector(
        onTap: () {
          showNotification();
        },
        child: Text(
          "Show Notification!",
          style: TextStyle(fontSize: 40, backgroundColor: Colors.redAccent),
        ),
      ),
    );
  }
}

触发的异常

Exception has occurred.
PlatformException (PlatformException(error,
Invalid notification (no valid small icon):
Notification(channel=CHANNEL_ID shortcut=null contentView=null
vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000
vis=PRIVATE), null, java.lang.IllegalArgumentException: Invalid
notification (no valid small icon): Notification(channel=CHANNEL_ID
shortcut=null contentView=null vibrate=null sound=null defaults=0x0
flags=0x10 color=0x00000000 vis=PRIVATE)

已尝试的无效方案

  • AndroidInitializationSettings('app_icon')
  • AndroidInitializationSettings('android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png')
  • AndroidInitializationSettings('@mipmap/ic_launcher')

解决方案

问题核心

Android对通知图标有严格要求:必须是透明背景的单色图标,且资源必须放置在Android项目的res/mipmap或res/drawable目录下,同时初始化流程需完整执行、通知配置需明确关联图标。

具体步骤

  1. 准备合规图标

    • 制作透明背景的单色通知图标,尺寸对应Android规范:24x24(mdpi)、36x36(hdpi)、48x48(xhdpi)、72x72(xxhdpi)、96x96(xxxhdpi)
    • 将不同尺寸的图标分别放入android/app/src/main/res/mipmap-mdpi/、mipmap-hdpi/等对应目录,统一命名为ic_notification.png(可自定义)
  2. 修正初始化代码
    初始化时仅使用图标文件名(不带路径和后缀),且必须调用initialize方法完成插件初始化:

    void initializePlatformSpecifics() {
      var initializationSettingsAndroid = AndroidInitializationSettings('ic_notification');
      initializationSettings = InitializationSettings(android: initializationSettingsAndroid);
      // 补充初始化调用,之前代码遗漏此步骤
      flutterLocalNotificationsPlugin!.initialize(initializationSettings);
    }
    
  3. 在通知配置中指定图标
    在AndroidNotificationDetails中明确设置通知小图标:

    var androidChannelSpecifics = AndroidNotificationDetails(
      'CHANNEL_ID',
      'CHANNEL_NAME',
      channelDescription: "CHANNEL_DESCRIPTION",
      importance: Importance.max,
      priority: Priority.high,
      playSound: true,
      timeoutAfter: 5000,
      styleInformation: DefaultStyleInformation(true, true),
      icon: 'ic_notification', // 指定通知小图标
    );
    
  4. 清理缓存并重启项目
    执行flutter clean命令清除缓存,然后重新运行项目,确保资源生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 18:30:13