MissingPluginException(No implementation found for method show on channel dexterous.com/flutter/local_notifications)

时光毁灭记忆、已成空白 提交于 2020-08-09 09:03:45

问题


I've try implement Firebase Cloud Messaging with Flutter, and i was success until i use 'Local Notification' plugin for show notification

My notification work fine on foreground, but on background this show this error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method show on channel dexterous.com/flutter/local_notifications)

I use Firebase Cloud Messaging 6.0.9, Local Notification 1.2.0+4 and latest Flutter

Here is my code: 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 method

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:


I have found solution, that why in platform side we don't register for outside plugin

Add in your FCMPluginRegistrant.java like

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;
    }
}

And call it on Application.java

  @Override
    public void registerWith(PluginRegistry registry) {



        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);

    }



回答2:


For Kotlin : Application.kt

package your.package.name

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
        if (!registry!!.hasPlugin("com.dexterous.flutterlocalnotifications")) {
                FlutterLocalNotificationsPlugin.registerWith(registry!!.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
        }
    }
}


来源:https://stackoverflow.com/questions/60471697/missingpluginexceptionno-implementation-found-for-method-show-on-channel-dexter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!