为何Codemagic构建的Android应用通知图标消失?
解决Flutter应用发布后通知图标消失的问题
1. 检查通知图标格式是否符合Android要求
Android通知图标必须是透明背景的单色矢量图(SVG)或PNG,不能包含彩色内容,否则在release构建或部分设备上会被系统替换为默认图标。确保你的ic_stat_awair_android_icons满足:
- 背景完全透明
- 仅使用单一颜色(白色最优,系统会根据主题自动适配色调)
- 尺寸适配Android各dpi规格(mdpi:24x24、hdpi:36x36、xhdpi:48x48、xxhdpi:72x72、xxxhdpi:96x96)
2. 禁用Codemagic的资源压缩优化
Codemagic的release构建默认可能开启资源压缩,导致通知图标被误处理。你可以:
- 在Codemagic的Android构建选项中,关闭
shrinkResources和minifyEnabled;或者在android/app/build.gradle的release代码块中添加配置:
release { shrinkResources false minifyEnabled false // 其他签名相关配置... }
如果需要开启代码混淆,要在proguard-rules.pro中添加规则保护通知图标资源:
-keepresources drawable/ic_stat_awair_android_icons*
3. 确认图标资源的引用路径
确保awesome_notifications初始化时的资源路径正确,release构建中资源路径可能存在差异:
- 验证
ic_stat_awair_android_icons文件确实存在于android/app/src/main/res/drawable-*对应dpi目录下 - 可以尝试改用
'mipmap/ic_stat_awair_android_icons'路径测试
4. 为通知渠道显式指定图标
在创建NotificationChannel时,明确设置渠道的默认图标,避免系统使用默认占位符:
NotificationChannel( channelKey: 'scheduled_channel', channelName: 'awair Notifications', importance: NotificationImportance.High, channelDescription: 'awair Notification Channel', defaultIcon: 'resource://drawable/ic_stat_awair_android_icons', // 新增该行 ), NotificationChannel( channelKey: 'basic_channel', channelName: 'awair basic Notifications', channelDescription: 'awair Notification Channel', defaultIcon: 'resource://drawable/ic_stat_awair_android_icons', // 新增该行 ),
5. 本地验证release构建
先在本地生成release版本的APK/AAB,安装到Pixel 6a测试,定位问题根源:
flutter build appbundle --release
如果本地release构建也出现图标消失问题,说明是资源配置本身的问题;如果本地正常,则重点排查Codemagic的构建环境变量、gradle参数等配置。
内容的提问来源于stack exchange,提问作者Chris Thornham
相关产品推荐
相关产品推荐

