Osama
Osama

Reputation: 533

flutter local notifications not working on IOS

I am setting up fcm with a flutter app and using local notifications to show notifications when the app is in foreground .

in android it works perfectly with no errors whatsoever . But on IOS local notifications are not working . its not showing an error or anything but when the app is in the foreground its now showing the notification banner at all

this is the setup for local notification :

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));

Then there is the showNotification function which is this :

 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); }

i have searched through the internet and found out i need to use this

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

and i added it to AppDelegate.swift but still not working

thanks

Upvotes: 5

Views: 6650

Answers (2)

Mohi Dev
Mohi Dev

Reputation: 3416

please try this in appDelegate file

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])
    }
}

Upvotes: 2

Osama
Osama

Reputation: 533

Solved by updating flutter local notification plugin to the latest version and delete the app from the device and run flutter clean and thats it

Upvotes: 3

Related Questions