android - Icon问题localNotificationsPlugin,如何正确添加app Icon
问题描述
我已经在颤振应用程序中实现了本地推送通知。这个插件一开始运行顺畅。当我运行flutter clean
并卸载 android Emulator 上的应用程序以使用flutter run
. 这样做后,应用程序在启动时崩溃。
在调试模式下运行应用程序时,显示错误发生在await localNotificationsPlugin.initialize
. 调试器显示下面的语句。
Exception has occurred.
PlatformException (PlatformException(INVALID_ICON, The resource ic_launcher could not be found. Please make sure it has been added as a drawable resource to your Android head project., null))
下面的代码显示了我是如何实现 localNotificationsPlugin 的。
FlutterLocalNotificationsPlugin localNotificationsPlugin =
FlutterLocalNotificationsPlugin();
initializeNotifications() async {
var initAndroid = AndroidInitializationSettings('ic_launcher');
var initIOS = IOSInitializationSettings();
var initSettings = InitializationSettings(initAndroid, initIOS);
await localNotificationsPlugin.initialize(
initSettings,
onSelectNotification: gotToNotificationsPage,
);
}
@override
void initState() {
super.initState();
initializeNotifications();
showNotification();
}
Future singleNotification(
DateTime datetime, String message, String subtext, int hashcode,
{String sound}) async {
var androidChannel = AndroidNotificationDetails(
'channel-id',
'channel-name',
'channel-description',
importance: Importance.Max,
priority: Priority.Max,
);
var iosChannel = IOSNotificationDetails();
var platformChannel = NotificationDetails(androidChannel, iosChannel);
localNotificationsPlugin.schedule(
hashcode, message, subtext, datetime, platformChannel,
payload: hashcode.toString());
}
showNotification() async {
DateTime now = DateTime.now().toUtc().add(
Duration(seconds: 5),
);
await singleNotification(
now,
'Notification',
'This is a notification',
98123871,
);
}
Future gotToNotificationsPage(String payload) {
return Navigator.pushNamed(context, '/notifications');
}
请注意,问题出在await localNotificationsPlugin.initialize
我未能正确分配应用程序图标。谢谢你。
解决方案
要设置自定义图标,您可以在里面添加一个 app_icon.png android/app/src/res/drawable
,然后使用
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');`
否则,如果您想使用默认应用程序图标,您可以在 AndroidInitializationSettings 上设置应用程序图标@mipmap/ic_launcher
如果您不确定从哪里开始,可以尝试GitHub 上插件提供的示例。
推荐阅读
- excel - Excel数组公式对超过目标单元格的第一个匹配项进行hlookup
- reactjs - 我们如何确保其他
组件作为一个可折叠的 组件得到扩展 - 反应材料ui组件?
- django - 使用 Zappa 部署 Django 应用程序时导入错误?
- java - 如何在 JavaFX 桌面应用程序中查看 PDF 而无需实际将 PDF 下载到客户端计算机?
- sql - 内部连接性能问题或条件
- python - 如何在python中获取这个正则表达式
- three.js - 如何使用 Object3D 的本地旋转设置 Three.js 平面旋转?
- cesium - 如果地球失去焦点,Cesium 会重绘实体
- node.js - 即使安装了@types/node,Typescript 也找不到模块“fs”
- ios - Google 我的地图图钉在 Android 上显示,但在 iOS 上不显示