Firebase分析事件记录在多进程应用程序中 [英] Firebase analytics event logging in multi-process app

查看:512
本文介绍了Firebase分析事件记录在多进程应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Firebase分析整合到了我的应用程序中,该程序有两个过程:背景过程和过程。我使用Firebase的经历是,我可以从 UI 过程而不是从背景过程记录事件。



通过Android Studio控制台上的Firebase日志,我们可以看到,尽管事件已记录,但从背景过程。这是Firebase分析所遵循的行为 - 仅从单个进程记录事件?如果是这样,那么它是如何决定从哪个进程记录事件?

我需要从两个进程中记录事件,以便理解完整的用户体验,他的应用程序健康和一些其他重要参数。



所有帮助都是值得赞赏的。

解决方案

记录Firebase Analytics事件从多个进程中,您需要在第二个进程中手动初始化Firebase。半自动Firebase设置很大程度上期望单进程应用程序,并且要求额外的设置工作,否则对于其大多数API(崩溃报告除外)。

基本上,您可以调用以下假设您通过 google-services.json 文件和Google Services Gradle插件配置了Firebase:

  FirebaseApp.initializeApp(context,FirebaseOptions.fromResource(context))

稍微棘手的部分可以是如何确保这只被调用一次,并且只在第二个进程中。一种方法是模仿Firebase本身为第一个流程所做的事情(通过清单合并) - 定义一个ContentProvider。所以在你的Manifest中加入如下内容:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ MyFBProvider
android:authorities =org.mydomain.mytestapp.MyFBProvider
android:process =:myexternalprocess
android:exported =false
android:syncable =false />

您的ContentProvider看起来基本上是这样的,加上所有抽象方法的空覆盖:

  public class MyFBProvider extends ContentProvider {

private static boolean created = false;
$ b $ @覆盖
public boolean onCreate(){
if(created){
// https://issuetracker.google.com/issues/37045392的解决方法
返回false;
}

上下文context = getContext();
FirebaseApp.initializeApp(context,FirebaseOptions.fromResource(context));
created = true;

//返回false以模拟FirebaseInitProvider的行为。
//它应该保持伪造的ContentProvider不是一个真正的。
返回false;






使用ContentProvider可以确保代码在进程初始化之前运行,并且只在您指定的进程中运行。


I have integrated Firebase analytics into my app which has two process : a Background process and a UI process. What I experienced using Firebase was that I'm able to log events from the UI process but not from the Background process.

I can see through Firebase logs on the Android Studio console that though the events are logged but are never scheduled to be uploaded on the Firebase console when being logged from the Background process. Is this a behaviour that Firebase analytics follows - logging events only from a single process? If so, then how does it decide from which process to log events?

I need to log events from both the processes in order to understand the complete user experience, his app health and some other important parameters.

All help is appreciated.

解决方案

To log Firebase Analytics events from more than one process you need to initialize Firebase manually in the second process. The semi-automated Firebase setup largely expects single-process apps, and requires additional setup work otherwise for most of its APIs (except Crash Reporting).

Essentially you call the following upon initialization of the second process, assuming that you configured Firebase via the google-services.json file and Google Services Gradle plugin:

FirebaseApp.initializeApp(context, FirebaseOptions.fromResource(context))

The slightly trickier part can be how to ensure that this is only called once, and only in the second process. One approach is to mimic what Firebase itself does for the first process (via Manifest merging) - define a ContentProvider. So in your Manifest add something like the following:

<provider
    android:name=".MyFBProvider"
    android:authorities="org.mydomain.mytestapp.MyFBProvider"
    android:process=":myexternalprocess"
    android:exported="false"
    android:syncable="false" />

Your ContentProvider looks essentially like this, plus empty overrides of all abstract methods:

public class MyFBProvider extends ContentProvider {

    private static boolean created = false;

    @Override
    public boolean onCreate() {
        if (created) {
            // Workaround for https://issuetracker.google.com/issues/37045392
            return false;
        }

        Context context = getContext();
        FirebaseApp.initializeApp(context, FirebaseOptions.fromResource(context));
        created = true;

        // Return false to mimic behavior of FirebaseInitProvider.
        // It should keep the pseudo ContentProvider from being a real one.
        return false;
    }

    ...
}

Using a ContentProvider ensures that the code is run before everything else during process initialization, and only in the process that you specify.

这篇关于Firebase分析事件记录在多进程应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆