dart 错误:未定义命名参数“onSelectNotification”

clj7thdc  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(169)

我将flutter_local_notifications更新到12.0.3并显示此错误:
错误:未定义命名参数'onSelectNotification'。(位于[罗森] lib\services\notification\notification_service.dart的未定义命名参数:33)
这是文件的来源:

import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:rosen/models/models.dart';
import 'package:rosen/screens/screens.dart' show LeaderBoardScreen;
import 'package:rosen/utils/logger.dart';

class NotificationService extends GetxService {
  final _notifications = FlutterLocalNotificationsPlugin();

  @override
  void onInit() {
    _initNotifications();
    super.onInit();
  }

  Future<void> _initNotifications() async {
    const androidInitializationSettings =
        AndroidInitializationSettings('@drawable/app_notification_icon');
    const iosInitializationSettings = DarwinInitializationSettings(
      requestAlertPermission: false,
      requestSoundPermission: false,
      requestBadgePermission: false,
    );
    const InitializationSettings initializationSettings =
        InitializationSettings(
            android: androidInitializationSettings,
            iOS: iosInitializationSettings);
     _notifications.initialize(initializationSettings,
        onSelectNotification: (payload) {
      if (payload != null) {
        final QuizPaperModel quizPaperModel =
            QuizPaperModel.fromJson(json.decode(payload));
        Get.toNamed(LeaderBoardScreen.routeName, arguments: quizPaperModel);
        //MyApp.navigatorKey.currentState!.pushNamed(LeaderBoardScreen.routeName, arguments:quizPaperModel);
      }
    });
  }

  Future<void> showQuizCompletedNotification(
      {required int id,
      String? title,
      String? body,
      String? imageUrl,
      String? payload}) async {
    BigPictureStyleInformation? bigPictureStyleInformation;
    String? largeIconPath;

    if (imageUrl != null) {
      largeIconPath = await _downloadAndSaveFile(imageUrl, 'largeIcon');
      final String? bigPicturePath =
          await _downloadAndSaveFile(imageUrl, 'bigPicture');

      if (bigPicturePath != null) {
        bigPictureStyleInformation = BigPictureStyleInformation(
            FilePathAndroidBitmap(bigPicturePath),
            hideExpandedLargeIcon: true,
            contentTitle: '<b>$title</b>',
            htmlFormatContentTitle: true,
            summaryText: '<b>$body</b>',
            htmlFormatSummaryText: true);
      }
    }

    _notifications.show(
        id,
        title,
        body,
        NotificationDetails(
            android: AndroidNotificationDetails('quizcomplete', 'quizcomplete',
                channelDescription: 'Open leaderboard',
                importance: Importance.max,
                largeIcon: FilePathAndroidBitmap(largeIconPath!),
                styleInformation: bigPictureStyleInformation,
                priority: Priority.max),
            iOS: const DarwinNotificationDetails(
                presentAlert: true, presentBadge: true, presentSound: true)),
        payload: payload);
  }

  Future<String?> _downloadAndSaveFile(String url, String fileName) async {
    try {
      final Directory directory = await getApplicationDocumentsDirectory();
      final String filePath = '${directory.path}/$fileName';
      final http.Response response = await http.get(Uri.parse(url));
      final File file = File(filePath);
      await file.writeAsBytes(response.bodyBytes);
      return filePath;
    } catch (e) {
      AppLogger.e(e);
    }
    return null;
  }
}

我尝试定义参数,但由于仍在学习Flutter而失败
我怎样才能解决这个问题?

rqqzpn5f

rqqzpn5f1#

10.0.0版本起:onSelectNotification已更改为onDidReceiveNotificationResponse,如www.example.com中所述ChangeLog.md:
onDidReceiveNotificationResponse:仅在应用运行时调用。这适用于用户选择了通知或通知操作的情况。这将替换以前存在的onSelectNotification回调。

nbnkbykc

nbnkbykc2#

您必须使用onDidReceiveNotificationResponse

相关问题