我想在 flutter 中显示一个简单的通知。这是我的源代码:
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class MyScreen extends StatefulWidget {
const MyScreen({Key? key}) : super(key: key);
@override
State<MyScreen> createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> {
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
var initializationSettings;
@override
void initState() {
super.initState();
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
initializePlatformSpecifics();
}
initializePlatformSpecifics() {
var initializationSettingsAndroid = AndroidInitializationSettings('assets/logo128.ico');
initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
}
Future<void> showNotification() async {
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID',
'CHANNEL_NAME',
//"CHANNEL_DESCRIPTION",
importance: Importance.max,
priority: Priority.high,
playSound: true,
timeoutAfter: 5000,
styleInformation: DefaultStyleInformation(true, true),
);
var platformChannelSpecifics =
NotificationDetails(android: androidChannelSpecifics);
await flutterLocalNotificationsPlugin!.show(
0, // Notification ID
'Test Title', // Notification Title
'Test Body', // Notification Body, set as null to remove the body
platformChannelSpecifics,
payload: 'New Payload', // Notification Payload
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 239, 241, 249),
body: GestureDetector(
onTap: () {
showNotification();
},
child: Text(
"Show Notification!",
style: TextStyle(fontSize: 40, backgroundColor: Colors.redAccent),
),
),
);
}
}
但是点击“显示通知!”后出现此错误:
发生异常。
平台异常(平台异常(错误, 无效通知(无有效小图标): 通知(通道=CHANNEL_ID 快捷方式=null contentView=null 振动=空声音=空默认= 0x0 标志= 0x10 颜色= 0x00000000 vis=PRIVATE), null, java.lang.IllegalArgumentException: 无效 通知(无有效小图标):通知(channel=CHANNEL_ID 快捷方式=空 contentView=空 振动=空 声音=空 默认=0x0 标志=0x10 颜色=0x00000000 vis=私人)
请帮助我。
我也使用了这些命令,但问题没有解决:
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
var initializationSettingsAndroid = AndroidInitializationSettings('android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png');
var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
这是我的 vs 代码截图:
问题是
FlutterLocalNotificationsPlugin
的初始化函数没有被调用。尝试这样的事情:
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
late final InitializationSettings initializationSettings;
_initializeFlutterLocalNotifications() async {
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
const initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
await flutterLocalNotificationsPlugin?.initialize(
const InitializationSettings(
android: initializationSettingsAndroid,
),
);
}
@override
void initState() {
super.initState();
_initializeFlutterLocalNotifications();
}
我记得有一个问题,因为我如何通过
defaultIcon
路径,并通过 '@mipmap/ic_launcher'
解决了问题。