简体   繁体   English

Flutter 本地通知无法显示

[英]Flutter local notification unable to showup

I am trying to show local notification using flutter_local_notifications: ^9.7.0 package.我正在尝试使用flutter_local_notifications: ^9.7.0包显示本地通知。 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:到目前为止,当我在使用 iphone 13 pro max 的 ios 模拟器中尝试时,它可以正常工作,但是当我在 iphone 8 中调试时,无法显示通知。这是代码 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:我在 IOS 设置部分阅读了文档( https://pub.dev/packages/flutter_local_notifications ),其中指出:

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?但我不知道如何配置它,有没有办法做到这一点,以便通知也可以显示在 iphone 8 中?

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 {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM