简体   繁体   English

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

[英]How to dismiss local notification in flutter without bring app in foreground

I am usingawesome_notifications and want to dismiss as well as not bring the app in foreground when the 'Mark as read' is clicked in the notification.我正在使用awesome_notifications并希望在通知中单击“标记为已读”时关闭应用程序,而不是将应用程序置于前台

App correctly not bring the app in foreground , but the notification does not get dismissed.应用程序正确地不会将应用程序置于前台,但不会关闭通知。

Below is the code下面是代码

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),
      ),
    );
  }
}

How can the notification can also be dismissed?怎么通知也可以被驳回? Before and after之前和之后截屏

As buttonType for 'Mark as read' is ActionButtonType.DisabledAction it correctly does not bring the application in the foreground( one of the requirement), but the second requirement of 'dismissing' ( ie. notification being cleared, is not occurring and notification is staying. How the notification be cleared at same time?由于“标记为已读”的 buttonType 是 ActionButtonType.DisabledAction 它正确地不会将应用程序置于前台(要求之一),但“解除”的第二个要求(即通知被清除,没有发生并且通知是停留。如何同时清除通知?

As per documentation, your code is right.根据文档,您的代码是正确的。 ActionButtonType.DisabledAction should remove the notification from the tray. ActionButtonType.DisabledAction应该从托盘中删除通知。 But it don't (Even in the example app).但它没有(即使在示例应用程序中)。 I think it's a bug.我认为这是一个错误。 You can create a GitHub issue and request a fix.您可以创建 GitHub 问题并请求修复。 One more thing, AwesomeNotifications().dismiss(id) removes the notification.还有一件事, AwesomeNotifications().dismiss(id)删除通知。 But Awesome Notification doesn't support custom action.但是 Awesome Notification 不支持自定义操作。 So, I think you have to wait for the fix.所以,我认为你必须等待修复。

据我所知,隐藏通知是不可能的,如果它在前台,至少如果不做一些“技巧”这是不可能的。

i think this code can help you我认为这段代码可以帮助你

int id=Random().nextInt(1000);
await NotificationHandler.flutterLocalNotificationPlugin.show(
    id, title, body, platform,
    payload: 'my payload');
await NotificationHandler.flutterLocalNotificationPlugin.cancel(id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在颤动中将应用程序从后台带到前台 - How to bring an app from background to foreground in flutter 如何在颤振集成测试中将应用程序带到前台 - How to bring app to foreground in flutter integration test 如何在前台接收推送通知时显示颤动本地通知? - How to show flutter local notification while receiving push notification on foreground? 在前台隐藏 flutter 本地通知 - Hide flutter local notification on foreground 当应用程序在 iOS 前台时如何处理 Flutter FCM 推送通知? - How to handle Flutter FCM push notification when app in foreground for iOS? Flutter Firebase 如何控制通知在应用程序处于前台时不显示 - Flutter Firebase how to control notification not to show when the app is in foreground 单击操作按钮后如何在不打开应用程序的情况下关闭通知? - How to dismiss notification after action button clicked without opening the app? 如何将应用程序从后台带到前台 - How to bring an app from background to foreground 我开发了提醒应用程序,因此如何在不使用 firebase 的情况下向 flutter 中的用户发送本地通知 - I develop reminder app so how to send a local notification to user in flutter without using firebase 应用程序处于前台时未收到推送通知 ios flutter - Push Notification not received when app is in foreground ios flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM