1

I linked my project with FCM notifications, now I can receive notifications and display them at the front of the app or at the time the user is inside the app on the Android operating system.

But on the iOS system, notifications appear only if the user is outside the application, but if the user is inside the application, notifications do not appear for them.

I previously searched the old topics here and tried all the topics and problems similar to mine, but I did not find a solution to my problem.

I use the following code to deal with notifications:

  final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  new FlutterLocalNotificationsPlugin();
  Future<Map<String, dynamic>> sendAndRetrieveMessage() async {
    await firebaseMessaging.requestNotificationPermissions(
      const IosNotificationSettings(
          sound: true, badge: true, alert: true, provisional: false),
    );
  }

 void showNotification(String title, String body) async {
    await _demoNotification(title, body);
  }
  Future<void> _demoNotification(String title, String body) async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'channel_ID', 'channel name', 'channel description',
        importance: Importance.max,
        playSound: true,
        // sound: 'sound',
       // sound: true,
        showProgress: true,
        priority: Priority.high,
        ticker: 'test ticker');

    var iOSChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        android: androidPlatformChannelSpecifics, iOS: iOSChannelSpecifics);
    await flutterLocalNotificationsPlugin
        .show(0, title, body, platformChannelSpecifics, payload: 'test');
  }


  Future onSelectNotification(String payload) async {
    showDialog(
      context: context,
      builder: (_) {
        return new AlertDialog(
          title: Text("PayLoad"),
          content: Text("Payload : $payload"),
        );
      },
    );
  }




  @override
  void initState() {
    super.initState();
   // AdsInterstitialAd.showInterstitialAd();


    var initializationSettingsAndroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  notification();
}

  notification(){
    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        showNotification(message['notification']['title'], message['notification']['body']);
       // print("onMessage: $message");

      },

      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");

      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");

      },
    );
  }

How can I solve this problem and display in-app notifications for IOS users? Please help me

Marwan
  • 47
  • 6
  • you can take a look at this link if you do not have any bug in your code https://stackoverflow.com/questions/14872088/get-push-notification-while-app-in-foreground-ios it will help you to implement it – Antonin GAVREL Mar 01 '21 at 17:12

1 Answers1

1

Per default iOS does not display messages while you are inside the app (foreground). However you can override this default behavior...

Add this to your AppDelegate.swift:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        
        GeneratedPluginRegistrant.register(with: self)
        
        // although I didn't see this in the official documentation, it didn't work without this line for me...
        application.registerForRemoteNotifications()
        
        if #available(iOS 10.0, *) {
            // register this as the delegate so that the method below gets called...
            UNUserNotificationCenter.current().delegate = self
        }
        
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    // here you can define how a notification should be presented while the app is in foreground (you could for example disable the sound...)
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }
    
}