Firebase analytics event logging in multi-process app

怎甘沉沦 提交于 2019-12-18 09:05:25

问题


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.


回答1:


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.




回答2:


Firebase Analytics supports multi-process apps. Did you test your app on device with Google Play services or on an emulator w/o Google Play Services. A capture of the logcat output with enabled debug logging would answer most of this questions.

In order to preserve battery drain events are uploaded not more often then once an hour. If you have logged events from the main process waited to see upload (which generally happens 15 seconds after first event is recorded on devices w/o Google Play services) then logged more events from the secondary process and waited another 15 seconds you will not see the second batch of events being uploaded. There will be approximately one hour before the second batch of events is uploaded.

If you have left the emulator running the background events should appear on your report within couple of hours.



来源:https://stackoverflow.com/questions/38370881/firebase-analytics-event-logging-in-multi-process-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!