arincgurkan
arincgurkan

Reputation: 25

Flutter Awesome Notification couldn't bind a method to action button

I'm developing an app which needs to answer the notifications. I can display the button when I received the notification. However, I couldn't bind any method to it. Here is my code:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message)  async {
  await Firebase.initializeApp();
    AwesomeNotifications().createNotification(
          content: NotificationContent(
              id: 10,
              channelKey: 'basic_channel',
              title: 'Simple Notification',
              body: 'Simple body'),
          actionButtons: [
            NotificationActionButton(
                label: 'TEST',
                enabled: true,
                buttonType: ActionButtonType.Default,
                key: 'test',
                )
          ]);
    print("Background message service");
  }

Thank you for your helps!

Upvotes: 1

Views: 4710

Answers (3)

Anshu79
Anshu79

Reputation: 11

I could not initially find a solution too. After taking a look at the documentation, this is what I came up with.

class NotificationController {
  static ReceivedAction? action;

  static Future<void> initNotifications() async {

    AwesomeNotifications().initialize(null, [
      // add your Notification channels here
    ]);
  }

  static Future<void> initEventListeners() async {
    AwesomeNotifications().setListeners(onActionReceivedMethod: onActionReceivedMethod);
  }

  @pragma("vm:entry-point")
  static Future<void> onActionReceivedMethod(ReceivedAction action) async {
    if (action.buttonKeyPressed == 'key') print("TEST clicked");
  }
}

Now, you can simply add await NotificationController.initNotifications() and await NotificationController.initEventListeners() to the main function of your app.

Upvotes: 1

AlexFlutter
AlexFlutter

Reputation: 123

Update for anyone from the new versions. You can handle clicks on notifications with various other actions, by following the documentation. here's what I did. documentation

class NotificationController {
  /// Use this method to detect when the user taps on a notification or action button
  @pragma("vm:entry-point")
  static Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async {
    // Your code goes here

    /// Handles regular notification taps.
    if(receivedAction.actionType == ActionType.Default){
      if(receivedAction.id == 17897583){
        // do something...
      }
    }
  }
}

Upvotes: 0

Benyamin Beyzaie
Benyamin Beyzaie

Reputation: 573

You should create a event stream and listen to events like that:

    AwesomeNotifications().actionStream.listen((event) {
      print('event received!');
      print(event.toMap().toString());
      // do something based on event...
    });

Upvotes: 2

Related Questions