Flutter:应用程序在后台时无法显示本地通知

3

编辑:此问题已在最新的flutter和firebase版本中得到解决。

我正在监听Firebase FCM的data类型消息,收到FCM消息后,我想从应用程序内显示本地通知。

_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");
}

使用flutter_local_notification插件显示本地通知时出现错误,抛出MissingPluginException异常(在频道dexterous.com/flutter/local_notifications上找不到方法initialize的实现)。我漏了什么?

在这里找到了解决方案。需要在 Android 应用程序类中注册插件。https://dev59.com/VVQJ5IYBdhLWcg3wkGy-#59836752 - krishnakumarcn
这个回答解决了你的问题吗?Flutter:即使应用程序关闭也能推送通知 - Omatt
1个回答

1
在添加代码之前,您应该尝试运行flutter clean
当我添加新包并执行热重载/重新启动而没有进行完全重建时,就会发生这种错误。
如果与此无关,我认为您只是缺少渠道创建部分。对我来说,这段代码运行良好,await部分是您需要的。
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);
    }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接