如何在不将应用程序置于前台的情况下消除颤动中的本地通知

puz*_*led 5 android-notifications flutter flutter-local-notification

我正在使用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),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

通知怎么也可以被驳回?之前和之后截屏

由于“标记为已读”的按钮类型是 ActionButtonType.DisabledAction,它正确地不会将应用程序带到前台(要求之一),但“解除”的第二个要求(即,通知被清除,没有发生,并且通知是入住,如何同时清除通知?

Eis*_*hon 1

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