【问题标题】:Flutter: Not able to show local notification when the app is in backgroundFlutter:当应用程序处于后台时无法显示本地通知
【发布时间】:2020-05-08 15:36:23
【问题描述】:

编辑:此问题已在最新的颤振和 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 插件显示本地通知会引发错误

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

我在这里错过了什么?

【问题讨论】:

标签: flutter firebase-cloud-messaging


【解决方案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);
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2021-07-02
    • 2017-06-25
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多