如何修改flutter_background_service初始化时的图标?
如何修改flutter_background_service初始化时的图标?
Android 平台修改步骤
准备自定义图标
将符合Android通知规范的透明图标(建议准备24x24、48x48、72x72等不同dpi适配版本)放到Android项目的android/app/src/main/res/mipmap-*或drawable-*目录下,比如命名为ic_notification。修改本地通知初始化的图标
在flutterLocalNotificationsPlugin.initialize的AndroidInitializationSettings中替换默认图标路径:AndroidInitializationSettings('@mipmap/ic_notification'), // 替换为你的图标资源路径配置后台服务的通知图标
在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 平台修改步骤
添加通知图标资源
在Xcode中打开iOS项目,将符合iOS规范的通知图标(无圆角、透明背景)添加到Assets.xcassets中,创建名为NotificationIcon的图标集并导入对应尺寸的图片。配置通知初始化参数
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
相关产品推荐
相关产品推荐

