MissingPluginException(在通道 dexterous.com/flutter/local_notifications 上找不到方法显示的实现)

Posted

技术标签:

【中文标题】MissingPluginException(在通道 dexterous.com/flutter/local_notifications 上找不到方法显示的实现)【英文标题】:MissingPluginException(No implementation found for method show on channel dexterous.com/flutter/local_notifications) 【发布时间】:2020-06-13 17:41:03 【问题描述】:

我已经尝试使用 Flutter 实现 Firebase 云消息传递,直到我使用“本地通知”插件来显示通知时我才成功

我的通知在前台工作正常,但在后台显示此错误:

[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常: MissingPluginException(找不到方法显示的实现 频道 dexterous.com/flutter/local_notifications)

我使用 Firebase Cloud Messaging 6.0.9、本地通知 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) 使用 flutter clean 命令 2) 卸载以前的应用程序 3) 重新安装应用程序 谢谢,问题已解决。这一步没用:( 【参考方案1】:

我找到了解决方案,为什么在平台方面我们不注册外部插件

像这样添加你的 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;
    

然后在 Application.java 上调用它

  @Override
    public void registerWith(PluginRegistry registry) 



        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);

    

【讨论】:

这对我有用。尚未检查其他平台是否需要类似的解决方法。 import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin 同时导入 io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;在上面的第一段代码中。它对我有用。【参考方案2】:

对于 Kotlin: 应用程序.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"));
        
    

【讨论】:

这似乎对我不起作用***.com/questions/62820872/…【参考方案3】:

当我在我的混合应用程序的本机部分中使用result.notImplemented 时,我遇到了同样的问题。当我将此方法替换为result.success(0)时,一切都解决了@

【讨论】:

【参考方案4】:

我只是先卸载应用程序,然后重新安装它,它解决了我的问题。

【讨论】:

以上是关于MissingPluginException(在通道 dexterous.com/flutter/local_notifications 上找不到方法显示的实现)的主要内容,如果未能解决你的问题,请参考以下文章

MissingPluginException(未找到通道上的方法的实现)

GoogleMaps Flutter 插件 MissingPluginException

发布模式android中的MissingPluginException

尝试直接从 FLUTTER 发出呼叫:MissingPluginException(未找到方法 callNumber 的实现

android Unhandled Exception 上的 Flutter 运行错误:MissingPluginException(未找到方法的实现)

MissingPluginException(在通道 com.amazonaws.amplify/amplify 上找不到方法配置的实现)