5

I am using awesome_notifications and want to dismiss as well as not bring the app in foreground when the 'Mark as read' is clicked in the notification.

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 screenshot

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 ?

puzzled
  • 509
  • 1
  • 5
  • 18
  • why click on dismiss rather just swapping the notification? – RusJaI Jul 16 '21 at 09:54
  • 1
    @RusJ swapping is to ignore the notification, when an action( such as **MARK AS READ**) is clicked, it is supposed to do an operation as well as take away the notification. – puzzled Jul 25 '21 at 21:44

2 Answers2

1

As per documentation, your code is right. ActionButtonType.DisabledAction should remove the notification from the tray. 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. One more thing, AwesomeNotifications().dismiss(id) removes the notification. But Awesome Notification doesn't support custom action. So, I think you have to wait for the fix.

Eishon
  • 1,274
  • 1
  • 9
  • 17
0

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

or Follow this link can help About Handle Notification

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 21:37