gpt4 book ai didi

android - 我已经在 Flutter 中使用了 Google Analytics,如何从原生 iOS/Android 代码向它发送事件?

转载 作者:行者123 更新时间:2023-12-05 00:23:29 28 4
gpt4 key购买 nike

我使用 firebase 进行 flutter ,其中所有依赖项都在 pubspec.yaml
我想针对 native 端的某些 Activity 向 Firebase Analytics 发送事件 - 主屏幕小部件、收到的通知等。
可能吗?
FirebaseAnalytics 类在 Android 上无法识别,如果我将它添加到 gradle,它可能会与 flutter 发生冲突。

最佳答案

使用 PlatformChannel是这种情况的解决方案,请参阅 platform-channels .
示例代码(来自 here 的原始代码:

  • 安卓端:

  • package com.example.shared;

    import android.content.Intent;
    import android.os.Bundle;

    import androidx.annotation.NonNull;

    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.embedding.android.FlutterActivity;
    import io.flutter.embedding.engine.FlutterEngine;
    import io.flutter.plugins.GeneratedPluginRegistrant;

    public class MainActivity extends FlutterActivity {

    private static final String CHANNEL = "app.channel.shared.data";
    private String sharedText;

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

    sharedText = "Home screen displayed";
    }

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

    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
    .setMethodCallHandler(
    (call, result) -> {
    if (call.method.contentEquals("onScreenView")) {
    result.success(sharedText);
    sharedText = null;
    }
    }
    );
    }

    }

  • flutter 面:

  • import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';

    void main() {
    runApp(const SampleApp());
    }

    class SampleApp extends StatelessWidget {
    const SampleApp({super.key});

    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Sample Shared App Handler',
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: const SampleAppPage(),
    );
    }
    }

    class SampleAppPage extends StatefulWidget {
    const SampleAppPage({super.key});

    @override
    State<SampleAppPage> createState() => _SampleAppPageState();
    }

    class _SampleAppPageState extends State<SampleAppPage> {
    static const platform = MethodChannel('app.channel.shared.data');
    String dataShared = 'No data';

    @override
    void initState() {
    super.initState();
    getSharedText();
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text(dataShared)));
    }

    Future<void> getSharedText() async {
    var sharedData = await platform.invokeMethod('onNotificationClicked');
    if (sharedData != null) {
    setState(() {
    dataShared = sharedData;

    // Send data to Firebase Analytics here
    });
    }
    }
    }

    关于android - 我已经在 Flutter 中使用了 Google Analytics,如何从原生 iOS/Android 代码向它发送事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72979907/

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