Firebase PluginRegistry无法转换为引擎

Firebase PluginRegistry无法转换为引擎,firebase,flutter,firebase-cloud-messaging,flutter-dependencies,Firebase,Flutter,Firebase Cloud Messaging,Flutter Dependencies,当我将颤振更新到版本1.12.13时,我发现了这个问题,无法修复它。我按照firebase_消息传递教程发送的方式进行了操作,并收到以下错误: 错误:不兼容的类型:PluginRegistry无法转换为FlatterEngine GeneratedPluginRegistrant.registerWith(注册表);” 我的代码如下: package io.flutter.plugins; import io.flutter.app.FlutterApplication; import io.

当我将颤振更新到版本1.12.13时,我发现了这个问题,无法修复它。我按照firebase_消息传递教程发送的方式进行了操作,并收到以下错误: 错误:不兼容的类型:PluginRegistry无法转换为FlatterEngine GeneratedPluginRegistrant.registerWith(注册表);” 我的代码如下:

package io.flutter.plugins;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  @Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingService.setPluginRegistrant(this);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
      NotificationChannel channel = new NotificationChannel("messages","Messages", NotificationManager.IMPORTANCE_LOW);
  NotificationManager manager = getSystemService(NotificationManager.class);
  manager.createNotificationChannel(channel);
    }
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
  }
}

于2019年12月31日更新。

您不应该使用Firebase云消息工具发送通知,因为它会强制您使用标题和正文

您必须发送不带标题和正文的通知。将应用程序放在后台,这应该适合您

如果它对你有效,如果你能就这个答案给我投票,我将不胜感激,谢谢


我找到了一个暂时的解决办法。我不确定这是否是最好的修复方案,但我的插件工作正常,我假设问题出在第164行的io.flatter.plugins.firebasemessaging.flatterfirebaseMessagingService提供的注册表上

我的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="Your Package"> // CHANGE THIS

    <application
        android:name=".Application"
        android:label="" // YOUR NAME APP
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        <!-- BEGIN: Firebase Cloud Messaging -->    
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        <!-- END: Firebase Cloud Messaging -->    
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
Future<void> sendNotificationOnBackground({
  @required String token,
}) async {
  await firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false),
  );
  await Future.delayed(Duration(seconds: 5), () async {
    await http.post(
    'https://fcm.googleapis.com/fcm/send',
     headers: <String, String>{
       'Content-Type': 'application/json',
       'Authorization': 'key=$SERVERTOKEN', // Constant string
     },
     body: jsonEncode(
     <String, dynamic>{
       'notification': <String, dynamic>{

       },
       'priority': 'high',
       'data': <String, dynamic>{
         'click_action': 'FLUTTER_NOTIFICATION_CLICK',
         'id': '1',
         'status': 'done',
         'title': 'title from data',
         'message': 'message from data'
       },
       'to': token
     },
    ),
  );
  });  
}
我的FirebaseCloudMessagingPluginRegistrant.java

package YOUR PACKAGE HERE;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {

  @Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
  }
}
package YOUR PACKAGE HERE;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public final class FirebaseCloudMessagingPluginRegistrant{
  public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
      return;
    }
    FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
  }

  private static boolean alreadyRegisteredWith(PluginRegistry registry) {
    final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
    if (registry.hasPlugin(key)) {
      return true;
    }
    registry.registrarFor(key);
    return false;
  }
}
用dart发送通知:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="Your Package"> // CHANGE THIS

    <application
        android:name=".Application"
        android:label="" // YOUR NAME APP
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        <!-- BEGIN: Firebase Cloud Messaging -->    
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        <!-- END: Firebase Cloud Messaging -->    
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
Future<void> sendNotificationOnBackground({
  @required String token,
}) async {
  await firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false),
  );
  await Future.delayed(Duration(seconds: 5), () async {
    await http.post(
    'https://fcm.googleapis.com/fcm/send',
     headers: <String, String>{
       'Content-Type': 'application/json',
       'Authorization': 'key=$SERVERTOKEN', // Constant string
     },
     body: jsonEncode(
     <String, dynamic>{
       'notification': <String, dynamic>{

       },
       'priority': 'high',
       'data': <String, dynamic>{
         'click_action': 'FLUTTER_NOTIFICATION_CLICK',
         'id': '1',
         'status': 'done',
         'title': 'title from data',
         'message': 'message from data'
       },
       'to': token
     },
    ),
  );
  });  
}
未来发送通知背景({
@必需的字符串标记,
})异步的{
等待firebaseMessaging.requestNotificationPermissions(
const IONotificationSettings(声音:真、徽章:真、警报:真、临时:假),
);
等待未来。延迟(持续时间(秒:5),()异步{
等待http.post(
'https://fcm.googleapis.com/fcm/send',
标题:{
“内容类型”:“应用程序/json”,
'Authorization':'key=$SERVERTOKEN',//常量字符串
},
正文:JSONECODE(
{
“通知”:{
},
“优先级”:“高”,
“数据”:{
“点击动作”:“颤振通知点击”,
“id”:“1”,
“状态”:“完成”,
“标题”:“来自数据的标题”,
“消息”:“来自数据的消息”
},
“to”:令牌
},
),
);
});  
}

我添加了一个持续时间为5秒的等待,这样您就可以将应用程序放在后台,并验证后台的消息是否正在运行

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);

从android文件夹下的mainactivity文件。如果没有,您将得到一个错误。

下面可以找到DomingoMG到Kotlin的代码端口(包括文件路径)。 在2020年10月10日进行测试和工作

/公共规范yaml

firebase\u消息:^7.0.0
/android/app/src/main/kotlin/Application.kt

在这里打包您的包
导入io.flatter.app.flatterApplication
导入io.flatter.plugin.common.PluginRegistry
导入io.flatter.plugin.common.PluginRegistry.PluginRegistrantCallback
导入io.flatter.plugins.firebasemessaging.FlatterFireBaseMessagingService
公共类应用程序:flatterApplication(),PluginRegistrantCallback{
重写fun onCreate(){
super.onCreate()
FlatterFireBaseMessAgingService.setPluginRegistrNet(此)
}
用(注册表:PluginRegistry)覆盖乐趣注册表{
FirebaseCloudMessagingPluginRegistrant.registerWith(注册表)
}
}
/android/app/src/main/kotlin/FirebaseCloudMessagingPluginRegistrant.kt

在这里打包您的包
导入io.flatter.plugin.common.PluginRegistry
导入io.flatter.plugins.firebasemessaging.FirebaseMessagingPlugin
类FirebaseCloudMessagingPluginRegistrant{
伴星{
乐趣注册表(注册表:PluginRegistry){
if(alreadyRegisteredWith(注册表)){
返回;
}
FirebaseMessagingPlugin.registerWith(registry.registerFor(“io.flatter.plugins.FirebaseMessagingPlugin.FirebaseMessagingPlugin”))
}
fun-alreadyRegisteredWith(注册表:PluginRegistry):布尔值{
val key=FirebaseCloudMessagingPluginRegistrant::class.java.name
if(registry.hasPlugin(key)){
返回真值
}
注册表。注册表项(注册表项)
返回错误
}
}
}

我在Firebase消息包中的步骤中仅添加了水类作为额外项,解决了以下问题:

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class FirebaseCloudMessagingPluginRegistrant{
public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
        return;
    }
    FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}

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

替换此代码行:

GeneratedPluginRegistrant.registerWith(registry);
为此:

FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
确保导入:

import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
我发现这是可行的:

@覆盖
public void配置flatterengine(@NonNull flatterengine flatterengine){
超级配置颤振发动机(颤振发动机);
//继续自定义方法通道注册。
...    
}
不确定是否还需要使用清理引擎来清理方法通道注册。

只需执行即可

GeneratedPluginRegistrant.registerWith((FlutterEngine) registry);
代替

GeneratedPluginRegistrant.registerWith(registry);

经过一些研究,我发现这是可行的。
搜索官方的FlatterFire存储库,并查看其中的最新插件。
GeneratedPluginRegistrant.registerWith(注册表)不起作用,这在官方文件中也有提及

使用我从

目前在firabase_消息:9.0.0

//
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;
// Be sure to import the exact Plugin
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
//
@Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this);
    }
 @Override
  public void registerWith(PluginRegistry registry) {
     FlutterFirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
  }
}

我也得到这个错误。到目前为止有什么解决方案吗?没有。我试过也没试过你的解决方案,但我没有成功,在ONLAUNCH、ONRESUME和ONMESSAGE状态下出现了,只有ONBACKGROUND没有出现。我将文件FirebaseCloudMessagingPluginRegistrant.java与Application.java放在同一个文件夹中,对吗?我希望颤振团队能尽快解决这个问题。到那时,我将不得不使用1.9.1版,尽管我想使用1.12.13版,那么你能不能创建一个项目,并在你的github上给我一个链接,让我下载并尝试在我的Firebase测试项目上运行它?我留下了一个帮助我发送推送通知的结构。不知道为什么,但确实如此。希望Flatter团队在下一个版本中解决这个问题,我将FirebaseCloudMessagingPluginRegistrant.JAVA放在与应用程序相同的文件夹或firebasemessaging文件夹中