Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振:带patform通道的Firebase消息传递-重复颤振活动_Android_Firebase_Flutter_Firebase Cloud Messaging - Fatal编程技术网

Android 颤振:带patform通道的Firebase消息传递-重复颤振活动

Android 颤振:带patform通道的Firebase消息传递-重复颤振活动,android,firebase,flutter,firebase-cloud-messaging,Android,Firebase,Flutter,Firebase Cloud Messaging,我正在尝试使用flatter实现一个应用程序。应用程序应该能够使用FCM从智能手机向服务器发送消息,或者通过FCM从服务器接收消息。 我使用firebase_消息传递插件()实现了FCM功能。下游消息(服务器->设备)一切正常。现在我尝试添加上游消息(设备->服务器)。据我从文档中了解,这个插件还不支持上游消息。因此,我开始编写本机代码,将消息发送到服务器,并通过平台通道功能()调用此代码 初始MainActivity.kt: import android.os.Bundle import i

我正在尝试使用flatter实现一个应用程序。应用程序应该能够使用FCM从智能手机向服务器发送消息,或者通过FCM从服务器接收消息。 我使用firebase_消息传递插件()实现了FCM功能。下游消息(服务器->设备)一切正常。现在我尝试添加上游消息(设备->服务器)。据我从文档中了解,这个插件还不支持上游消息。因此,我开始编写本机代码,将消息发送到服务器,并通过平台通道功能()调用此代码

初始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)
  }
}
添加了颤振文档中的方法通道示例。使用颤振文档中指定的导入:

    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_消息传递插件需要mainactivity.kt来扩展io.flatter.app.flatterActivity。平台频道要求mainactivity.kt扩展
io.flatter.embedded.android.flatterActivity


有什么方法可以同时使用-firebase\u消息传递插件和方法通道吗?

我将我的代码与firebase\u消息传递插件中的示例项目进行了比较:

然后我意识到示例也使用了
io.flatter.embedded.android.flatterActivity
。将导入语句更改为
io.flatter.embedding…
package后,我删除了
GeneratedPluginRegistrant.registerWith(this)
,因为
GeneratedPluginRegistrant
是从
io.flatter.plugins.GeneratedPluginRegistrant
导入的,似乎与嵌入包不匹配。然后我再次检查了
io.flatter.embedding.engine.flatterengine
中的字段和函数,并添加了以下行:
flatterengine?.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
    }
}