a195-flutter-fundamental-labs icon indicating copy to clipboard operation
a195-flutter-fundamental-labs copied to clipboard

🐛 The named parameter 'onDidReceiveLocalNotification' isn't defined in DarwinInitializationSettings

Open RulyOctareza opened this issue 9 months ago • 1 comments

I encountered an issue when implementing flutter_local_notifications on iOS. The error occurs when initializing DarwinInitializationSettings with the parameter onDidReceiveLocalNotification.

The named parameter 'onDidReceiveLocalNotification' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onDidReceiveLocalNotification'.

Steps to Reproduce:

  • Add flutter_local_notifications to pubspec.yaml.
  • Initialize DarwinInitializationSettings with onDidReceiveLocalNotification.
  • Run the app on an iOS device.
  • Observe the compilation error.

Expected Behavior: The onDidReceiveLocalNotification parameter should either be supported or clearly documented as removed in the latest version. If removed, alternative methods should be provided in the documentation.

Actual Behavior: The parameter onDidReceiveLocalNotification is not recognized, causing a compilation error.

Proposed Solution: Update the documentation to clarify that onDidReceiveLocalNotification has been removed. Suggest using onDidReceiveNotificationResponse instead in the flutterLocalNotificationsPlugin.initialize() method. Environment: Flutter Version: 3.24.5 Plugin Version: flutter_local_notifications: ^18.0.1 Platform: iOS iOS Version: 18.0

For now, I replaced:

final initializationSettingsDarwin = DarwinInitializationSettings(
  requestAlertPermission: false,
  requestBadgePermission: false,
  requestSoundPermission: false,
  onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {
    didReceiveLocalNotificationStream.add(
      ReceivedNotification(id: id, title: title, body: body, payload: payload),
    );
  },
);

with:

final initializationSettingsDarwin = DarwinInitializationSettings(
  requestAlertPermission: false,
  requestBadgePermission: false,
  requestSoundPermission: false,
);

and then handled notification with:

await flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveNotificationResponse: (notificationResponse) {
    final payload = notificationResponse.payload;
    if (payload != null && payload.isNotEmpty) {
      selectNotificationStream.add(payload);
    }
  },
);

RulyOctareza avatar Feb 05 '25 06:02 RulyOctareza