Show local notification when the app is opened in iOS

822

Add this code to your project :

await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
Share:
822
iDecode
Author by

iDecode

Updated on December 01, 2022

Comments

  • iDecode
    iDecode over 1 year

    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',
      );
    }
    
  • iDecode
    iDecode over 2 years
    Thanks for your answer but did you try flutter_local_notifications plugin yourself because docs mentions that the notification can be displayed whilst the app is running in the foreground once it is properly configured. But I don't know how to configure it.
  • iDecode
    iDecode about 2 years
    I already added that.
  • iDecode
    iDecode about 2 years
    I'm using Objective C, can you post the equivalent code for it?
  • Noha El Sheweikh
    Noha El Sheweikh about 2 years
    I edit my answer , please check hope it works for you
  • iDecode
    iDecode about 2 years
    Thanks, I'll give it a try in a day or so and will let you know.