Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: The named parameter 'onSelectNotification' isn't defined

i update flutter_local_notifications into 12.0.3 and show me this error:

error: The named parameter 'onSelectNotification' isn't defined. (undefined_named_parameter at [rosen] lib\services\notification\notification_service.dart:33)

and this is source of file:

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;
  }
}

i try to define parameter but failed because still learning flutter

how i can solve this problem?

like image 982
Dr iTech Avatar asked May 09 '26 18:05

Dr iTech


2 Answers

Since the version 10.0.0: onSelectNotification has been changed to onDidReceiveNotificationResponse as mentioned in the ChangeLog.md:

onDidReceiveNotificationResponse: invoked only when the app is running. This works for when a user has selected a notification or notification action. This replaces the onSelectNotification callback that existed before.

like image 63
Lalit Fauzdar Avatar answered May 12 '26 07:05

Lalit Fauzdar


You have to use onDidReceiveNotificationResponse

like image 29
rimeh123 Avatar answered May 12 '26 06:05

rimeh123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!