MissingPluginException (реализация не найдена для показа метода на канале dexterous.com/flutter/local_notifications) - PullRequest
0 голосов
/ 01 марта 2020

Я пытался реализовать Firebase Cloud Messaging с Flutter, и у меня все получалось, пока я не использовал плагин 'Local Notification' для показа уведомлений

Мое уведомление работает нормально на переднем плане, но на фоне это показывает эту ошибку:

[ОШИБКА: flutter / lib / ui / ui_dart_state. cc (157)] Необработанное исключение: MissingPluginException (Не найдена реализация для метода, показанного на канале dexterous.com/flutter/local_notifications)

Я использую Firebase Cloud Messaging 6.0.9, Local Notification 1.2.0 + 4 и последний Flutter

Вот мой код: NotificationHandler

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationHandler{
  static final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // make it a static field of the class

  static void initNotification()
  {

// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }
  static Future onSelectNotification(String payload) async {
    if (payload != null) {
      print('notification payload: ' + payload);
    }

  }
  static Future onDidReceiveLocalNotification(int id, String title, String body, String payload) {
    print(title+" "+body);
  }

}

ShowNotification метод

static void showNotification(data, data2) async {
      var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
          'dexterous.com.flutter.local_notifications', 'your channel name', 'your channel description',
          importance: Importance.Max,
          priority: Priority.High);
      var iOSPlatformChannelSpecifics =
      new IOSNotificationDetails();
      var platformChannelSpecifics = new NotificationDetails(
          androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);

      await NotificationHandler.flutterLocalNotificationsPlugin
      .show(
        0,
        data,
        data2,
        platformChannelSpecifics,
        payload: 'Custom_Sound',
      );

  }

1 Ответ

4 голосов
/ 07 марта 2020

Я нашел решение, поэтому на стороне платформы мы не регистрируем внешний плагин

Добавляем в свой FCMPluginRegistrant. java вроде

public final  class FirebaseCloudMessagingPluginRegistrant {

    public static void registerWith(PluginRegistry registry) {

        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));


    }




    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

И вызываем его в приложении . java

  @Override
    public void registerWith(PluginRegistry registry) {



        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...