web-dev-qa-db-fra.com

Flutter: la notification de premier plan ne fonctionne pas dans la messagerie Firebase

J'ai besoin d'afficher les notifications Firebase lorsque l'application est au premier plan en utilisant la notification locale mais qu'elle ne fonctionne pas.

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin=new FlutterLocalNotificationsPlugin();

  static  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  static StreamController<Map<String, dynamic>> _onMessageStreamController =
  StreamController.broadcast();
  static StreamController<Map<String, dynamic>> _streamController =
  StreamController.broadcast();
  static final Stream<Map<String, dynamic>> onFcmMessage =
      _streamController.stream;

  @override
  void initState() {
    super.initState();

    var Android=AndroidInitializationSettings('mipmap/ic_launcher.png');
    var ios=IOSInitializationSettings();
    var platform=new  InitializationSettings(Android,ios);
    flutterLocalNotificationsPlugin.initialize(platform);

    firebaseCloudMessaging_Listeners();
  }

Voici le code Firebase

 void firebaseCloudMessaging_Listeners() {

if (Platform.isIOS) iOS_Permission();

_firebaseMessaging.getToken().then((token) {
  print("FCM TOKEN--" + token);
});
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print('on message $message');
    showNotification(message);
  },
  onResume: (Map<String, dynamic> message) async {
    print('on resume $message');
  },
  onLaunch: (Map<String, dynamic> message) async {
    print('on launch $message');
  },
);


}

Il s'agit de la méthode showNotification

 void showNotification(Map<String, dynamic> msg) async{
    print(msg);
    var Android = new AndroidNotificationDetails(
        'my_package', 'my_organization', 'notification_channel', importance: Importance.Max, priority: Priority.High);
    var iOS = new IOSNotificationDetails();
    var platform=new NotificationDetails(Android, iOS);
    await flutterLocalNotificationsPlugin.show(
      0,'My title', 'This is my custom Notification', platform,);
  }

et réponse Firebase

{notification: {titre: titre du test, corps: texte de notification du test}, données: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, nom de la commande: farhana}}

13
Farhana

Problème: Voir la notification push lorsque l'application est au premier plan.

Solution: J'utilisais le plugin firebase_message et j'ai pu voir la notification push pendant que l'application est au premier plan en apportant ces quelques modifications dans le fichier iOS AppDelegate.Swift de mon projet flutter dans le projet flutter.

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

Remarque: Cela fonctionne également lors de l'utilisation avec le plugin flutter_local_notification mais avec un problème que onSelectNotification ne fonctionne pas en raison des modifications apportées ci-dessus.

1
Usman