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

时间:2020-01-22 12:59:34

标签: flutter firebase-cloud-messaging

我正在收听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)

我在这里想念什么?

1 个答案:

答案 0 :(得分: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);
    }
  }