【问题标题】:flutter local notifications not working on IOS颤动的本地通知在IOS上不起作用
【发布时间】:2020-09-22 23:14:46
【问题描述】:

我正在使用颤振应用程序设置 fcm,并在应用程序处于前台时使用本地通知来显示通知。

在 android 中它可以完美运行,没有任何错误。但在 IOS 本地通知不起作用。它没有显示错误或任何内容,但当应用程序处于前台时,它现在完全显示通知横幅

这是本地通知的设置:

var android = AndroidInitializationSettings('mipmap/ic_launcher');
var ios = IOSInitializationSettings();
var platform = new InitializationSettings(android, ios);
flutterLocalNotificationsPlugin.initialize(platform);
_firebaseMessaging.requestNotificationPermissions(
 const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false));

然后是 showNotification 函数:

 showNotification(Map<String, dynamic> message) async {
var android = new AndroidNotificationDetails(
    'IMPORTANT1', 'SHOW BANNER', 'ALWAYS SHOWS BANNER',
    importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
var ios = new IOSNotificationDetails();
var platform = NotificationDetails(android, ios);
await flutterLocalNotificationsPlugin.show(
    0,
    message['notification']['title'],
    message['notification']['body'],
    platform); }

我在网上搜索了一下,发现我需要使用这个

if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

我将它添加到 AppDelegate.swift 但仍然无法正常工作

谢谢

【问题讨论】:

    标签: flutter localnotification


    【解决方案1】:

    通过将flutter本地通知插件更新到最新版本并从设备中删除应用程序并运行flutter clean来解决

    【讨论】:

      【解决方案2】:

      请在 appDelegate 文件中试试这个

      import UIKit
      import Flutter
      import UserNotifications
      
      @UIApplicationMain
      @objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
        override func application(
          _ application: UIApplication,
          didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
        ) -> Bool {
          GeneratedPluginRegistrant.register(with: self)
          // set the delegate in didFinishLaunchingWithOptions
          UNUserNotificationCenter.current().delegate = self
          return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
          // This method will be called when app received push notifications in foreground
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
          {
              completionHandler([.alert, .badge, .sound])
          }
      }
      

      【讨论】:

      • “AppDelegate”与协议“UNUserNotificationCenterDelegate”的冗余一致性显示此错误
      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2021-10-31
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多