Flutter:Firebase带有格式通道的消息传递-重复的FlutterActivity

时间:2020-01-24 12:10:14

标签: android firebase flutter firebase-cloud-messaging

我正在尝试使用Flutter实现一个应用程序。该应用程序应该能够使用FCM将消息从智能手机发送到服务器,或通过FCM接收来自服务器的消息。 我使用firebase_messaging插件(https://pub.dev/packages/firebase_messaging)实现了FCM功能。下游消息(服务器->设备)一切正常。现在,我尝试添加上游消息(设备->服务器)。据我从文档了解,该插件尚不支持上游消息。因此,我开始编写本机代码以将消息发送到服务器,并通过平台通道功能(https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-kotlin-tab)调用此代码。

初始MainActivity.kt:

import android.os.Bundle

import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
  }
}

添加了Flutter文档中的方法通道示例。使用Flutter文档中指定的导入:

    private val CHANNEL = "samples.flutter.dev/battery"
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            // Note: this method is invoked on the main thread.
            call, result ->
            if (call.method == "getBatteryLevel") {
                val batteryLevel = getBatteryLevel()

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else {
                result.notImplemented()
            }
        }
    }

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }

但是不幸的是,出现了问题。 firebase_messaging插件需要mainactivity.kt来扩展io.flutter.app.FlutterActivity。平台通道需要mainactivity.kt来扩展io.flutter.embedding.android.FlutterActivity

有什么办法可以同时使用-firebase_messaging插件和方法通道吗?

1 个答案:

答案 0 :(得分:0)

我将代码与firebase_messaging插件中的示例项目进行了比较:https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/example/android/app/src/main/java/io/flutter/plugins/firebasemessagingexample/MainActivity.java

然后我意识到该示例也使用了io.flutter.embedding.android.FlutterActivity。将import语句更改为io.flutter.embedding...包后,我删除了GeneratedPluginRegistrant.registerWith(this),因为GeneratedPluginRegistrant是从io.flutter.plugins.GeneratedPluginRegistrant导入的,似乎与嵌入的包不匹配。然后,我再次检查了io.flutter.embedding.engine.FlutterEngine中的字段和函数,并添加了以下行:flutterEngine?.plugins?.add(FirebaseMessagingPlugin())

此后,我重新编译了该应用程序,并在启动时成功打印出了FCM设备ID和电池状态。

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

class MainActivity : io.flutter.embedding.android.FlutterActivity() {

    private val CHANNEL = "samples.flutter.dev/battery"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        flutterEngine?.plugins?.add(FirebaseMessagingPlugin())
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            // Note: this method is invoked on the main thread.
            call, result ->
            if (call.method == "getBatteryLevel") {
                val batteryLevel = getBatteryLevel()

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else {
                result.notImplemented()
            }
        }
    }

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }
}