我正在使用flutter应用程序设置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,但仍无法正常工作
谢谢
答案 0 :(得分:1)
已通过将flutter本地通知插件更新为最新版本并从设备中删除该应用程序并运行flutter clean来解决了
答案 1 :(得分:0)
请在 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])
}
}