How to add functionality to button click in Awesome Notifications

44 Views Asked by At

This is how I am creating a notification using Awesome Notifications in Flutter:

AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: 1,
    channelKey: 'basic_channel',
    title: "You spent some money?",
    body: "We think, ₹ ${jsonObject['amount']} that was ${jsonObject['type']} to ${jsonObject['vendor']} belongs to this category: ${jsonObject['category']}",
    actionType: ActionType.Default),
  actionButtons: [
    NotificationActionButton(
      key: 'Approve',
      label: 'Approve',
    ),
    NotificationActionButton(
      key: 'Edit',
      label: 'Edit',
    )
  ],
);

I want to add different functionalities to Approve and Edit options. What is the exact syntax to do that?

2

There are 2 best solutions below

0
JAY CHAUHAN On
try {
  var response = await http.get(Uri.parse(url));
  if (Platform.isAndroid) {
    await Permission.storage.request();
  }
  var status = await Permission.storage.status;
  if (!status.isGranted) {
    await Permission.storage.request();
  }
  if (response.statusCode == 200) {
    String fileName = 'certificate_$certificateNumber.pdf';
    const downloadsFolderPath = '/storage/emulated/0/Download/';
    Directory dir = Directory(downloadsFolderPath);
    File file = File('${dir.path}/$fileName');
    OpenFile.open(file.path);

    AwesomeNotifications().createNotification(
      actionButtons: [
        NotificationActionButton(
          key: 'OPEN',
          label: 'Open',
        ),
      ],
      content: NotificationContent(
        payload: {'PDF_Downloader': dir.path, 'file': fileName},
        id: 1,
        channelKey: "PDF_Downloader",
        title: "Certificate Downloaded",
        body: "PDF has been downloaded successfully in Download Folder",
      ),
    );

    await file.writeAsBytes(response.bodyBytes);
    String date = DateTime.now().toString();
    setState(() {
      verificationDate = date;
    });
  }
} catch // ...

This is a example of Downloading a PDF file and showing a notification to PDF Downloaded successfully and give option to open it. So, I've Passed Payload to Notification.

In your case You have to give a Channel Key and You have to create a function for your specific functionality and replace in place of dir.path.

0
Vizz On

Since you have the keys you can use this

AwesomeNotifications().actionStream.listen((receivedAction) {
    if (receivedAction.buttonKey == 'Approve') {
      handleApproveAction();
    } else if (receivedAction.buttonKey == 'Edit') {
      handleEditAction();
    }
  });

You will then need to create these 2 functions with your desired functionalities, if the functionalities are very similar you could create only 1 function and send the receivedAction.buttonKey as a parameter.