找不到 AwesomeNotifications().actionStream

bla*_*y99 2 flutter awesome-notifications

我想在我的颤动通知中添加一个按钮。按钮已显示,但我无法捕捉到该操作。

AwesomeNotifications().createNotification(
    content: NotificationContent(
        id: 888,
        channelKey: 'NetVideo',
        title: 'NetVideo',
        body: 'Server in ascolto'),
    actionButtons: <NotificationActionButton>[
      NotificationActionButton(key: 'yes', label: 'Yes'),
    ],
  );
Run Code Online (Sandbox Code Playgroud)

AwesomeNotifications().actionStream 仍然存在吗?

pow*_*rus 5

根据文件

所有流(createdStream、displayedStream、actionStream 和 DismissedStream)均被全局静态方法替换。您必须用静态和全局方法替换旧的流方法,换句话说,它们必须是静态Future并使用 async/await,并且您必须使用@pragma("vm:entry-point")来保留 dart 寻址。

换句话说,您应该创建一个通知监听器

class NotificationController {

  /// Use this method to detect when a new notification or a schedule is created
  @pragma("vm:entry-point")
  static Future <void> onNotificationCreatedMethod(ReceivedNotification receivedNotification) async {
    // Your code goes here
  }

  /// Use this method to detect every time that a new notification is displayed
  @pragma("vm:entry-point")
  static Future <void> onNotificationDisplayedMethod(ReceivedNotification receivedNotification) async {
    // Your code goes here
  }

  /// Use this method to detect if the user dismissed a notification
  @pragma("vm:entry-point")
  static Future <void> onDismissActionReceivedMethod(ReceivedAction receivedAction) async {
    // Your code goes here
  }

  /// 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

    // Navigate into pages, avoiding to open the notification details page over another details page already opened
    MyApp.navigatorKey.currentState?.pushNamedAndRemoveUntil('/notification-page',
            (route) => (route.settings.name != '/notification-page') || route.isFirst,
        arguments: receivedAction);
  }
}
Run Code Online (Sandbox Code Playgroud)

并在库内注册

AwesomeNotifications().setListeners(
    onActionReceivedMethod: (ReceivedAction receivedAction){
        NotificationController.onActionReceivedMethod(context, receivedAction);
    },
    onNotificationCreatedMethod: (ReceivedNotification receivedNotification){
        NotificationController.onNotificationCreatedMethod(context, receivedNotification);
    },
    onNotificationDisplayedMethod: (ReceivedNotification receivedNotification){
        NotificationController.onNotificationDisplayedMethod(context, receivedNotification);
    },
    onDismissActionReceivedMethod: (ReceivedAction receivedAction){
        NotificationController.onDismissActionReceivedMethod(context, receivedAction);
    },
);
Run Code Online (Sandbox Code Playgroud)