我试图在运行项目时向我的 flutter 项目添加通知,并向我显示此错误:
E/Android:很棒的通知:无效通知(没有有效的小图标):Notification(pri=2 contentView=com.example.test/0x1090085 vibrate=null sound=content://settings/system/notification_sound tick defaults=0x0 flags =0x11 颜色=0xff000000 vis=私人)(通知线程:58)
主屏幕.dart
import 'package:flutter/material.dart';
import 'package:test/page/notification_service.dart';
class MainScreen extends StatefulWidget {
const MainScreen({Key? key}) : super(key:key);
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
@override
void initState(){
// TODO: implement initState
super.initState();
NotificationService().requestPermission();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: (){
NotificationService().showNotification(
1,
'main_channel',
'Test title',
'Test body',
);
},
child: Container(
height: 30,
width: 70,
color: Colors.blue,
child: Text(
"SHOW NOTIFICATION"
),
),
) ,
GestureDetector(
onTap: () {
NotificationService().showScheduledNotification(
1,
'main_channel',
'Test title',
'Test body',
5,
);
},
child: Container(
height: 30,
width: 70,
color: Colors.red,
child: Text(
"SHOW NOTIFICATION2"
),
),
),
],
),
),
);
}
}
notification_service.dart
import 'package:awesome_notifications/awesome_notifications.dart';
class NotificationService{
static final NotificationService _notificationService = NotificationService._internal();
factory NotificationService(){
return _notificationService;
}
NotificationService._internal();
Future<void> initAwesomeNotification()async{
AwesomeNotifications().initialize(
'resource://drawable/ic_applogo',
[
NotificationChannel(
channelKey: 'main_channel',
channelName: 'main_channel',
channelDescription: 'main_channel notifications',
enableLights: true,
importance: NotificationImportance.Max,
)
],
);
}
Future<void> requestPermission() async{
AwesomeNotifications().isNotificationAllowed().then((allowed){
if(!allowed){
AwesomeNotifications().requestPermissionToSendNotifications();
}
});
}
Future<void> showNotification(int id,String channelKey,String title,String body) async{
AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: channelKey,
title: title,
body: body,
),
);
}
Future<void> showScheduledNotification(int id, String channelKey, String title, String body, int interval) async {
String localTZ = await AwesomeNotifications().getLocalTimeZoneIdentifier();
AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: channelKey,
title: title,
body: body,
),
schedule: NotificationInterval(
interval: interval,
timeZone: localTZ,
repeats: false,
)
);
}
}
徽标可能会导致问题,因此:
尝试将此:
'resource://drawable/ic_applogo',
替换为以下 null,
所以你可以插入另一个图标,至少在Android上我是这样做的:我删除了所有“ic_launcher.png”,因为我没有将它用作我的应用程序图标的默认图标,而恰恰是这样让资源://工作。之后,我重新安装了应用程序并设置了我想使用的另一个图标的目录,例如:resource://drawable/icon_app,然后它就工作了,我没有在lib/assets文件夹中测试它的图标,但它可能也有效。