Flutter: Foreground notification is not working in

2019-06-20 08:29发布

问题:

I need to show the notification when the app is on foreground firebase notifications by adding local notification but it is not working .

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

Here is the Firebase Code

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


}

This is showNotification method

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

and Firebase Response

{notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}}

回答1:

There is an active issue logged on GitHub repository for the package regarding the same. Firebase messaging and local notifications won't work together on iOS since you can register only a single delegate for notifications.

Check out: https://github.com/MaikuB/flutter_local_notifications/issues/111

There's also an active flutter issue for the same: https://github.com/flutter/flutter/issues/22099