Flutter本地通知插件:createNotificationChannel函数不起作用



我正试图用flutter_local_notifications: ^5.0.0+4创建一个android通知通道说起来,它看起来不起作用。

我用getNotificationChannels写了一个代码来验证通道是否成功创建

请看一下我的代码

Future<void> _getNotificationChannels() async {
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications', // description
importance: Importance.max,
));
final Widget notificationChannelsDialogContent =
await _getNotificationChannelsDialogContent();
await showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
content: notificationChannelsDialogContent,
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
),
);
}
Future<Widget> _getNotificationChannelsDialogContent() async {
try {
final List<AndroidNotificationChannel>? channels =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getNotificationChannels();
return Container(
width: double.maxFinite,
child: ListView(
children: <Widget>[
const Text(
'Notifications Channels',
style: TextStyle(fontWeight: FontWeight.bold),
),
const Divider(color: Colors.black),
if (channels?.isEmpty ?? true)
const Text('No notification channels')
else
for (AndroidNotificationChannel channel in channels!)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('id: ${channel.id}n'
'name: ${channel.name}n'
'description: ${channel.description}n'
'groupId: ${channel.groupId}n'
'importance: ${channel.importance.value}n'
'playSound: ${channel.playSound}n'
'sound: ${channel.sound?.sound}n'
'enableVibration: ${channel.enableVibration}n'
'vibrationPattern: ${channel.vibrationPattern}n'
'showBadge: ${channel.showBadge}n'
'enableLights: ${channel.enableLights}n'
'ledColor: ${channel.ledColor}n'),
const Divider(color: Colors.black),
],
),
],
),
);
} on PlatformException catch (error) {
return Text(
'Error calling "getNotificationChannels"n'
'code: ${error.code}n'
'message: ${error.message}',
);
}
}

如您所见,我使用createNotificationChannel创建通知通道,然后使用getNotificationChannels查看通道是否成功创建。

我总是";没有通知通道";作为对话框中的结果。我不知道我的代码出了什么问题

感谢您的帮助

问题是我用于测试的Android版本,通知渠道是一个特定于Android 8或更新版本的概念,这就是为什么API文档声明创建渠道的方法仅适用于Android 的这些版本

相关内容

  • 没有找到相关文章

最新更新