3

EDIT: This issue is fixed on the latest flutter and firebase version.

I'm listening to Firebase FCM data type messages and on receiving FCM messages, I intended to show a local notification from within the app.

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
        LocalNotificationService.showNotoficationWithDefaultSound("onmessage");
      },
      onResume: (Map<String, dynamic> message) async {
        LocalNotificationService.showNotoficationWithDefaultSound("onresume");
        print('on resume $message');
      },
      onLaunch: (Map<String, dynamic> message) async {
        LocalNotificationService.showNotoficationWithDefaultSound("onlaunch");
        print('on launch $message');
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );


/// Yes. This is a global function
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'chanID_007', 'chanName_kk107', 'This is my channel');
  var iosPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(
      androidPlatformChannelSpecifics, iosPlatformChannelSpecifics);
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  var initializationSettingsAndroid =
      new AndroidInitializationSettings('@mipmap/ic_launcher');
  var initializationSettingsIos = new IOSInitializationSettings();
  var initializationSettings = new InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIos);
  flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
  flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: onSelectNotification);


 await flutterLocalNotificationsPlugin.show(
  0, 'Tada!!', 'test', platformChannelSpecifics,
  payload: 'payload#7');
  print("background mssg");
}

But showing the local notification with flutter_local_notification plugin throws an error

Unhandled Exception: MissingPluginException(No implementation found for method initialize on channel dexterous.com/flutter/local_notifications)

What am I missing here?

krishnakumarcn
  • 3,959
  • 6
  • 39
  • 69
  • Found a solution here. Need to register the plugin in Application class on android. https://stackoverflow.com/a/59836752/5546443 – krishnakumarcn Jan 23 '20 at 05:57
  • Does this answer your question? [Flutter: Push notifications even if the app is closed](https://stackoverflow.com/questions/53572110/flutter-push-notifications-even-if-the-app-is-closed) – Omatt May 17 '21 at 13:31

1 Answers1

1

Before adding any code you should give a try to run flutter clean.
This error happened to me when i add a new package and perform a hot reload / restart without a full rebuild.

If it has nothing to do with this, you are simply missing the cannel creation part i think. I have made this code working fine for me, the await part is what you need.

Future<void> ensureInitialized() async {

    flutterLocalNotificationsPlugin.initialize(InitializationSettings(
        android: AndroidInitializationSettings("ic_notification"), iOS: IOSInitializationSettings()),
    );

    if (Platform.isAndroid) {

      final AndroidNotificationChannel channel = AndroidNotificationChannel(
        'high_importance_channel', // id
        'High Importance Notifications', // title
        'This channel is used for important notifications.', // description
        importance: Importance.max,
      );
      
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
    }
  }
Tom3652
  • 2,540
  • 3
  • 19
  • 45