【问题标题】:Flutter Local Notifications Plugin: createNotificationChannel function not workingFlutter 本地通知插件:createNotificationChannel 功能不起作用
【发布时间】:2021-07-30 12:22:39
【问题描述】:

我正在尝试使用 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 查看频道是否创建成功。

我总是在对话框中收到“无通知渠道”。 我不知道我的代码有什么问题,

感谢您的帮助

【问题讨论】:

    标签: flutter flutter-local-notification


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-20
      • 2015-08-03
      • 2018-06-19
      • 1970-01-01
      • 2021-10-16
      • 2020-07-17
      • 2013-05-29
      • 2011-06-29
      相关资源
      最近更新 更多