我正在try 使用最新版本Ffltter_LOCAL_NOTIFICATIONS:^13.0.0将本地通知包括在我的Ffltter项目中,但出现以下错误:

参数类型‘Future Function(字符串?)’不能赋值给参数类型‘void Function(NotificationResponse)?

onDidReceiveNotificationResponse : onSelectNotification

这是我的班级:

class LocalNotificationService {
  LocalNotificationService();

  final _localNotificationService = FlutterLocalNotificationsPlugin();

  final BehaviorSubject<String?> onNotificationClick = BehaviorSubject();

  Future<void> initialize() async {
    tz.initializeTimeZones();
    const AndroidInitializationSettings androidInitializationSettings =
        AndroidInitializationSettings('@drawable/ic_stat_android');

    DarwinInitializationSettings iosInitializationSettings =
    DarwinInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      onDidReceiveLocalNotification: onDidReceiveLocalNotification,
    );

    final InitializationSettings settings = InitializationSettings(
      android: androidInitializationSettings,
      iOS: iosInitializationSettings,
    );

    await _localNotificationService.initialize(
      settings,
      onDidReceiveNotificationResponse : onSelectNotification, //here is the error
    );
  }

  Future<NotificationDetails> _notificationDetails() async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails('channel_id', 'channel_name',
            channelDescription: 'description',
            importance: Importance.max,
            priority: Priority.max,
            playSound: true);

    const DarwinNotificationDetails iosNotificationDetails =
    DarwinNotificationDetails();

    return const NotificationDetails(
      android: androidNotificationDetails,
      iOS: iosNotificationDetails,
    );
  }

  Future<void> showNotification({
    required int id,
    required String title,
    required String body,
  }) async {
    final details = await _notificationDetails();
    await _localNotificationService.show(id, title, body, details);
  }

  Future<void> showScheduledNotification(
      {required int id,
      required String title,
      required String body,
      required int seconds}) async {
    final details = await _notificationDetails();
    await _localNotificationService.zonedSchedule(
      id,
      title,
      body,
      tz.TZDateTime.from(
        DateTime.now().add(Duration(seconds: seconds)),
        tz.local,
      ),
      details,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

  Future<void> showNotificationWithPayload(
      {required int id,
      required String title,
      required String body,
      required String payload}) async {
    final details = await _notificationDetails();
    await _localNotificationService.show(id, title, body, details,
        payload: payload);
  }

  void onDidReceiveLocalNotification(
      int id, String? title, String? body, String? payload) {
    print('id $id');
  }

  Future<void> onSelectNotification(String? payload) async {
    print('payload $payload');
    if (payload != null && payload.isNotEmpty) {
      onNotificationClick.add(payload);
    }
  }
}

我错过了什么?

推荐答案

onDidReceiveNotificationResponse在回调时提供NotificationResponse.你可以得到payload个赞

  onDidReceiveNotificationResponse: (details) =>
      onSelectNotification(details.payload),

  Future<void> onSelectNotification(NotificationResponse? response) async {
    print('payload $response');
    if (response?.payload != null && response!.payload!.isNotEmpty) {
      onNotificationClick.add(response.payload);
    }
  }

而这将被用于onDidReceiveNotificationResponse : onSelectNotification.

Flutter相关问答推荐

如何在Flutter的easy_translation包中链接嵌套的链接翻译?

无法在GroupButton内部正确呈现

Flutter 中不存在URI的目标

我不能通过第5步了解Flutter 翼Firebase教程

Flutter AppBar Animation反向调试

如何从DART中的事件列表中获取即将到来的日期

如何让经过Firebase身份验证的用户能够将Zip文件上载到Firebase云存储?

我怎样才能在Ffltter中显示html布局链接?

如何在不注销的情况下刷新Firebase身份验证用户

为什么我的Flutter 动画过渡没有发生?

当 Dart SDK 版本范围不匹配时,为什么依赖解析器不会抛出错误?

NotifierProvider 每次更新都会重新构建并且值不更新

如果有空间则居中CustomScrollView

使用堆栈的自定义按钮

如何在android中 Select 任何文件/文件路径 - Flutter

当我使用 Confetti Widget 时,Flutter Android 和 IOS 应用程序崩溃

如何验证非表单字段?

Getx Flutter 在更新值时抛出空错误

从物理设备 Flutter 中移除 USB 后启动画面不可见?

在 Flutter 应用程序中全局使用 assets 文件夹