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

如何修改flutter_background_service初始化时的图标?

如何修改flutter_background_service初始化时的图标?

Android 平台修改步骤

  1. 准备自定义图标
    将符合Android通知规范的透明图标(建议准备24x24、48x48、72x72等不同dpi适配版本)放到Android项目的 android/app/src/main/res/mipmap-* 或 drawable-* 目录下,比如命名为 ic_notification。

  2. 修改本地通知初始化的图标
    在 flutterLocalNotificationsPlugin.initialize 的 AndroidInitializationSettings 中替换默认图标路径:

    AndroidInitializationSettings('@mipmap/ic_notification'), // 替换为你的图标资源路径
    
  3. 配置后台服务的通知图标
    在 AndroidConfiguration 中添加 notificationIcon 参数,指定后台服务前台通知使用的图标:

    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
      notificationChannelId: 'my_app_id',
      initialNotificationTitle: 'Check out',
      initialNotificationContent: 'In Progress . . .',
      foregroundServiceNotificationId: 888,
      notificationIcon: '@mipmap/ic_notification', // 新增此行设置服务图标
    ),
    

iOS 平台修改步骤

  1. 添加通知图标资源
    在Xcode中打开iOS项目,将符合iOS规范的通知图标(无圆角、透明背景)添加到 Assets.xcassets 中,创建名为 NotificationIcon 的图标集并导入对应尺寸的图片。

  2. 配置通知初始化参数
    iOS会自动使用Assets中配置的通知图标,你可按需在 DarwinInitializationSettings 中配置通知行为:

    iOS: DarwinInitializationSettings(
      defaultPresentAlert: true,
      defaultPresentBadge: true,
      defaultPresentSound: true,
    ),
    

修改后的完整代码示例

Future initializeService() async {
  final service = FlutterBackgroundService();

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'my_app_id', // id
    'Check out', // title
    description: 'in Progress',
    importance: Importance.high,
    showBadge: true,
    enableVibration: true,
  );

  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  if (Platform.isIOS || Platform.isAndroid) {
    await flutterLocalNotificationsPlugin.initialize(
      InitializationSettings(
        iOS: DarwinInitializationSettings(
          defaultPresentAlert: true,
          defaultPresentBadge: true,
          defaultPresentSound: true,
        ),
        android: AndroidInitializationSettings('@mipmap/ic_notification'), // 替换图标路径
      ),
    );
  }

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
      notificationChannelId: 'my_app_id',
      initialNotificationTitle: 'Check out',
      initialNotificationContent: 'In Progress . . .',
      foregroundServiceNotificationId: 888,
      notificationIcon: '@mipmap/ic_notification', // 设置后台服务通知图标
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );

  service.startService();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 05:07:15