Flutter local notification unable to showup

Viewed 58

I am trying to show local notification using flutter_local_notifications: ^9.7.0 package. So far when I try in ios simulator uusing iphone 13 pro max.. it works fina, but when I debug in iphone 8, notification unable to show.. here is part of the code AppDelegate.swift:

....

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }
....
....
....
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

and the code to show notif:

Future<void> showNotifications() async {
    await flutterLocalNotificationsPlugin.show(
      21,
      "Title",
      "Subtitle",
      NotificationDetails(android: _androidNotificationDetails),
    );
  }

Future<void> init() async {
    final AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    final IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings(
            requestSoundPermission: false,
            requestBadgePermission: false,
            requestAlertPermission: false,
           );

    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: initializationSettingsAndroid,
            iOS: initializationSettingsIOS,
            macOS: null);

    tz.initializeTimeZones();

    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: selectNotification);
  }

I read the documentation (https://pub.dev/packages/flutter_local_notifications)in IOS setup section, it is stated that:

For older versions of iOS, you need to handle the callback as part of specifying the method that should be fired to the onDidReceiveLocalNotification argument when creating an instance IOSInitializationSettings object that is passed to the function for initializing the plugin.

but I don't know how to configure it, is there a way to do that so that the notification can show up also in iphone 8?

1 Answers

Like documentation says pass callback when creating settings.

final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings(
  requestSoundPermission: false,
  requestBadgePermission: false,
  requestAlertPermission: false,
  onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);

...

void onDidReceiveLocalNotification(int id, String title, String body, String payload) async {
...
}
Related