将数据从本机发送到抖动

时间:2019-03-11 17:40:35

标签: flutter

我正在扩展FirebaseMessagingService的本地实现以等待本地android中的推送通知。

当用户单击推送通知时,我需要启动我的flutter应用程序,这样。如何将数据发送到flutter应用程序?

2 个答案:

答案 0 :(得分:0)

您仍然可以基本上查看firebase messaging example(或the same for android

  1. AppDelegate中创建一个Platform Channel(在Android上为MainActivity),然后在飞镖/颤振侧注册同一通道(可能为main.dart,然后注册一个像onPushClicked)这样的方法
  2. 听您的本机代码中的解析消息
  3. 使用平台渠道将它们发送到您的dart代码(如上面的链接示例所示)channel.invokeMethod('onPushClicked', myMessageArguments)

答案 1 :(得分:0)

在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() {
                    }

                });

            }
        });



    }


}