camilleB
camilleB

Reputation: 2101

Flutter: How to execute an action only when we click on the notification using local_notification?

I'm displaying notifications using local_notification, and I would like to execute an action (for example, open the message of the new signal in a new window) when I click on the notification. I tried to use

onNotificationClick: new NotificationAction(actionText: "Open",
                callback: openSignal(signalEventFromCloud[i]),
                payload: "Open signal")

and the call back function is:

openSignal(Signal signal) {
    Navigator.push(
      context,
      new MaterialPageRoute(
        builder: (context) => new DetailScreen(signal: signal),
      ),
    );
  }

This method call the callback function directly when the notification is displayed but I want to execute the callback function only if the user click on the notification.

There is a way to do that?

Upvotes: 1

Views: 3801

Answers (1)

Richard Heap
Richard Heap

Reputation: 51751

callback needs to be a function, so change your code to read

onNotificationClick: new NotificationAction(actionText: "Open",
                callback: openSignal,
                payload: "Open signal")

otherwise openSignal gets called as the Widget is built, rather than as a result of the click.

However, you need to change openSignal to accept one String parameter

  openSignal(String signal) {
    Navigator.push(
      context,
      new MaterialPageRoute(
        builder: (context) => new DetailScreen(),
      ),
    );
  }

it's not clear what your parameter is for, but callback takes one String

Upvotes: 2

Related Questions