PluginRegistry ไม่สามารถแปลงเป็น FlutterEngine


22

ทันทีที่ฉันอัปเดตกระพือเป็นรุ่น 1.12.13 ฉันพบปัญหานี้และไม่สามารถแก้ไขได้ ฉันทำตามที่กวดวิชา firebase_messaging ส่งและได้รับข้อผิดพลาดต่อไปนี้: "ข้อผิดพลาด: ประเภทที่เข้ากันไม่ได้: PluginRegistry ไม่สามารถแปลงเป็น FlutterEngine 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);
  }
}

ฉันได้รับข้อผิดพลาดนี้ด้วย วิธีการแก้ปัญหาใด ๆ หรือยัง
— ajonno

ไม่ฉันลองแล้วทำไม่ได้
— Gabriel G. Pavan

คำตอบ:


21

อัปเดตเมื่อวันที่ 31 ธันวาคม 2019

คุณไม่ควรใช้เครื่องมือการส่งข้อความบนคลาวด์ Firebase เพื่อส่งการแจ้งเตือนเนื่องจากจะบังคับให้คุณใช้ชื่อและเนื้อหา

คุณต้องส่งการแจ้งเตือนโดยไม่มีชื่อและเนื้อหา มีแอพพลิเคชั่นอยู่เบื้องหลัง

ถ้ามันเหมาะกับคุณฉันจะขอบคุณถ้าคุณสามารถให้ฉันลงคะแนนในคำตอบนี้ขอบคุณ


ฉันพบวิธีแก้ปัญหาชั่วคราว ฉันไม่แน่ใจว่านี่คือการแก้ไขที่ดีที่สุด แต่ปลั๊กอินของฉันทำงานได้ตามที่คาดไว้และฉันคิดว่าปัญหาต้องอยู่กับรีจิสตรีที่จัดทำโดย io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService ที่บรรทัดที่ 164

ไฟล์ 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>

My Application.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);
  }
}

My FirebaseCloudMessagingPluginRegistrant.java

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

ส่งการแจ้งเตือนในโผ:

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

ฉันเพิ่มการรอด้วยระยะเวลา 5 วินาทีเพื่อให้คุณสามารถใส่แอปพลิเคชันในพื้นหลังและตรวจสอบว่าข้อความในพื้นหลังกำลังทำงาน


ฉันพยายามเป็นวิธีแก้ปัญหาของคุณ แต่ฉันไม่ประสบความสำเร็จในสถานะ ONLAUNCH ONRESUME และ ONMESSAGE ปรากฏบน ONBACKGROUND เท่านั้น ฉันวางไฟล์ FirebaseCloudMessagingPluginRegistrant.java ไว้ในโฟลเดอร์เดียวกับ Application.java ใช่ไหม ฉันหวังว่าทีม Flutter จะแก้ปัญหานี้ในไม่ช้า ถึงตอนนั้นฉันจะต้องใช้เวอร์ชั่น 1.9.1 ถึงแม้ว่าฉันต้องการใช้ 1.12.13 แย่มาก
— Gabriel G. Pavan

คุณสามารถสร้างโครงการและให้ลิงก์ใน Github ของคุณให้ฉันเพื่อดาวน์โหลดและลองใช้ในโครงการทดสอบ Firebase ของฉันได้หรือไม่
— Gabriel G. Pavan

ฉันได้อัปเดตคำตอบแล้วฉันพลาดข้อเท็จจริงสำคัญที่ต้องเพิ่ม
— DomingoMG

ฉันออกจากโครงสร้างที่ช่วยให้ฉันส่งการแจ้งเตือนแบบพุชด้วย dart
— DomingoMG

สิ่งนี้ใช้ได้ผล ไม่แน่ใจว่าทำไม แต่มันทำ หวังว่าทีมกระพือแก้ไขปัญหานี้ในรุ่นถัดไป
— Avi

10

พอร์ตของรหัส DomingoMG ไปยัง Kotlin สามารถดูได้ที่ด้านล่าง ผ่านการทดสอบและทำงานในเดือนมีนาคม 2563

pubspec.yaml

firebase_messaging: ^6.0.12

Application.kt

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: FlutterApplication(), PluginRegistrantCallback {
  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry) {
    FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
  }
}

FirebaseCloudMessagingPluginRegistrant.kt

package YOUR_PACKAGE_HERE

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

class FirebaseCloudMessagingPluginRegistrant {
  companion object {
    fun registerWith(registry: PluginRegistry) {
      if (alreadyRegisteredWith(registry)) {
        return;
      }
      FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
      val key = FirebaseCloudMessagingPluginRegistrant::class.java.name
      if (registry.hasPlugin(key)) {
        return true
      }
      registry.registrarFor(key)
      return false
    }
  }
}

สวัสดี `` `การดำเนินการล้มเหลวสำหรับงาน ': แอป: mergeDexDebug' > มีความล้มเหลวเกิดขึ้นขณะเรียกใช้งาน com.android.build.gradle.internal.tasks.Workers $ ActionFacade> com.android.builder.dexing.DexArchiveMergerException: ข้อผิดพลาดขณะรวมเอกสารสำคัญ dex: เรียนรู้วิธีแก้ไขปัญหาที่developer.android.com / studio / build / … . มีประเภทโปรแกรมอยู่แล้ว: com.example.gf_demo.FirebaseCloudMessagingPluginRegistrant `` `
— Kamil

7

แทนที่บรรทัดรหัสด้านล่างของคุณ:

GeneratedPluginRegistrant.registerWith(registry);

ด้วยสิ่งนี้:

FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));

1
มันใช้งานได้ ... อย่าลืมนำเข้าคลาสที่กล่าวถึง นำเข้า io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
— Zion

1

นอกจากคำตอบของ DomingoMG อย่าลืมลบ

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

จากไฟล์ mainactivity ภายใต้โฟลเดอร์ android ถ้าไม่คุณจะได้รับข้อผิดพลาด


แต่ที่ฉันสามารถลงทะเบียน MethodChannel ของตัวเองเมื่อฉันจะลบ configureFlutterEngine
— Kamil Svoboda

ตามคำตอบของ DomingoMG, FirebaseCloudMessagingPluginRegistrant.java ได้ทำการลงทะเบียน "registerWith ... " ดังนั้นนั่นคือเหตุผลที่ไม่จำเป็นต้องกำหนดค่า FlutterEngine อีกต่อไป นั่นตอบคำถามของคุณหรือไม่
— Axes Grinds

ฉันเข้าใจว่า FirebaseCloudMessagingPluginRegistrant.java ทำการลงทะเบียนแทนที่จะกำหนดค่า FlutterEngine แต่ configureFlutterEngine เป็นสถานที่ที่ฉันสามารถลงทะเบียน MethodChannel ของฉันเองเพื่อเรียก API ดั้งเดิม (โปรดดูที่ "การเขียนรหัสเฉพาะแพลตฟอร์มที่กำหนดเอง" บน flutter.dev) ฉันจะลงทะเบียน MethodChannel ได้อย่างไรเมื่อวิธีการกำหนดค่า FlutterEngine ถูกลบออก
— Kamil Svoboda

ฉันไม่มีประสบการณ์ในการเขียนโค้ดเฉพาะแพลตฟอร์ม ขออภัยที่ฉันไม่สามารถช่วยด้วยข้อมูลที่ ฉันหวังว่าคุณจะพบคำตอบ
— Axes Grinds

1

ฉันเพิ่มระดับน้ำเท่านั้นเป็นพิเศษจากขั้นตอนในแพ็คเกจ Firebase Messaging และแก้ไข:

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;
}}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.