gpt4 book ai didi

flutter - 将数据从原生发送到 flutter

转载 作者:行者123 更新时间:2023-12-03 19:30:43 25 4
gpt4 key购买 nike

我扩展了 FirebaseMessagingService 的 native 实现等待本地推送通知 android .

当用户单击推送通知时,我需要启动我的 Flutter 应用程序,所以..如何将数据发送到我的 Flutter 应用程序?

最佳答案

在 flutter 中


void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ScreenPage(),
);
}
}
class ScreenPage extends StatefulWidget {
@override
_ScreenPageState createState() => _ScreenPageState();
}

class _ScreenPageState extends State<ScreenPage> {

static const platform = const MethodChannel("myChannel");

@override
void initState() {
platform.setMethodCallHandler(nativeMethodCallHandler);
super.initState();
}

Future<dynamic> nativeMethodCallHandler(MethodCall methodCall) async {
print('Native call!');
switch (methodCall.method) {
case "methodNameItz" :
return "This data from flutter.....";
break;
default:
return "Nothing";
break;
}
}






@override
Widget build(BuildContext context) {

//return ();
}

}

在 Java 中

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
//import io.flutter.view.FlutterNativeView;


public class MyJavaFile extends FlutterActivity {

Button clickMeButton;
MethodChannel channel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

channel = new MethodChannel(getFlutterView(), "myChannel");

setContentView(R.layout.home_activity);
clickMeButton = findViewById(R.id.clickMeButton);

clickMeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


channel.invokeMethod("methodNameItz", null, new MethodChannel.Result() {
@Override
public void success(Object o) {
Log.d("Results", o.toString());
}
@Override
public void error(String s, String s1, Object o) {
}
@Override
public void notImplemented() {
}

});

}
});



}


}

关于flutter - 将数据从原生发送到 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55107498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com