iDecode
iDecode

Reputation: 28996

Show local notification when the app is opened in iOS

I'm using flutter_local_notifications package and it's showing notifications when the app is opened (foreground) in Android but not in the iOS. The docs says:

For iOS 10+, use the presentation options to control the behaviour for when a notification is triggered while the app is in the foreground. The default settings of the plugin will configure these such that a notification will be displayed when the app is in the foreground.

If I understood it right, I don't need to do anything on my own except (correct me if I'm wrong and this is what the question is all about). This is the code I'm using. (Works in both iOS and Android except the foreground part)

Future<void> showNotification() async {
  final notificationPlugin = FlutterLocalNotificationsPlugin();
  const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
  const iOSSettings = IOSInitializationSettings();
  final initializationSettings = InitializationSettings(
    android: androidSettings,
    iOS: iOSSettings,
  );
  await notificationPlugin.initialize(
    initializationSettings,
    onSelectNotification: (payload) async {},
  );

  const androidNotificationDetails = AndroidNotificationDetails(
    'foo_id',
    'Foo Name',
    'Foo description ',
    importance: Importance.max, 
    priority: Priority.defaultPriority,
    showWhen: false,
  );

  await notificationPlugin.show(
    1211, 
    'Notification title',
    'Notification body',
    NotificationDetails(android: androidNotificationDetails),
    payload: 'item x',
  );
}

Upvotes: 4

Views: 1856

Answers (3)

Noha El Sheweikh
Noha El Sheweikh

Reputation: 164

in AppDelegate.swift try add the following :

  • Swift:
    if #available(iOS 10.0, *) {
              UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            }
            func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
                     completionHandler([.alert, .badge, .sound])
                }
  • Objective-C:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0.0")) {
 (void)userNotificationCenter:(UNUserNotificationCenter *)center 
       willPresentNotification:(UNNotification *)notification 
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
}

Upvotes: 3

Lala Naibova
Lala Naibova

Reputation: 443

Add this code to your project :

await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

Upvotes: 1

Saifullah ilyas
Saifullah ilyas

Reputation: 464

its not possible to show local notification when app is in foreground. Alternatively you can use any BannerView package or design your custom BannerView to notify the user that will gives the feel of local notification in iOS. i did not tried with flutter but i am answering this question as an iOS developer.

Upvotes: 0

Related Questions