微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不将应用程序置于前台的情况下关闭 Flutter 中的本地通知

如何解决如何在不将应用程序置于前台的情况下关闭 Flutter 中的本地通知

我正在使用 awesome_notifications 并希望在点击通知中的“标记为已读”时关闭以及不将应用置于前台 .

应用正确未将应用置于前台,但通知不会被关闭

下面是代码

import 'package:awesome_notifications/awesome_notifications.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  AwesomeNotifications().initialize(
      null,[
        NotificationChannel(
            channelKey: 'basic_channel',channelName: 'Basic notifications',channelDescription: 'Notification channel for basic tests',// defaultColor: Color(0xFF9D50DD),// ledColor: Colors.white
      )
      ],debug: false);

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',theme: ThemeData(
        primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key,required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });

    AwesomeNotifications().createNotification(
        content: NotificationContent(
            id: 10,channelKey: 'basic_channel',title: 'Simple Notification',body: 'Simple body',payload: {'uuid': 'user-profile-uuid'}),actionButtons: [
          NotificationActionButton(
              key: 'READ',label: 'Mark as read',autoCancel: true,buttonType: ActionButtonType.disabledAction),NotificationActionButton(
              key: 'PROFILE',label: 'Show Profile',autoCancel: false,enabled: true,buttonType: ActionButtonType.Default)
        ]);
  }

  @override
  void initState() {
    super.initState();

    AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
      if (!isAllowed) {
        AwesomeNotifications().requestPermissionToSendNotifications();
      }
    });

    AwesomeNotifications().actionStream.listen((receivednotification) {
      debugPrint(
          "  actionStream received " + receivednotification.toString());
    });

    AwesomeNotifications().createdStream.listen((receivednotification) {
      debugPrint(
          "  createdStream received " + receivednotification.toString());
    });

    AwesomeNotifications().displayedStream.listen((receivednotification) {
      debugPrint(
          "  displayedStream received " + receivednotification.toString());
    });

    AwesomeNotifications().dismissedStream.listen((receivednotification) {
      debugPrint(
          "  dismissedStream received " + receivednotification.toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //
        title: Text(widget.title),body: Center(
        //
        child: Column(
          //
          mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
            Text(
              'You have pushed the button this many times:',Text(
              '$_counter',style: Theme.of(context).textTheme.headline4,],floatingActionButton: FloatingActionButton(
        onpressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),);
  }
}

通知如何也可以被驳回?

screenshot

之前和之后

作为“标记为已读”的 buttonType 是 ActionButtonType.disabledAction,它正确地不会将应用程序置于前台(要求之一),但“解除”的第二个要求(即通知被清除,未发生)和通知保持。如何同时清除通知

解决方法

根据文档,您的代码是正确的。 ActionButtonType.DisabledAction 应从托盘中删除通知。但它没有(即使在示例应用程序中)。我认为这是一个错误。您可以创建 GitHub 问题并请求修复。还有一件事,AwesomeNotifications().dismiss(id) 删除通知。但是 Awesome Notification 不支持自定义操作。所以,我认为你必须等待修复。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。