Firebase Google 具有自定义 FirebaseOptions 初始化方法的分析
Firebase Google Analytics with custom FirebaseOptions initilaize method
我必须为我的 android 应用程序添加简单的 Firebase Google 分析。
我需要在运行时 select 我的应用程序的 Firebase 项目。
"It's very rare, but sometimes an app must be able to select its Firebase project at runtime. This means your app can't use the automatic init provided by the FirebaseInitProvider that's merged into your app. In that case, the solution has two tasks."
Link 供参考:https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
所以,就我而言,我做了如下操作:
创建了一个应用程序class:
public class MyApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseOptions.Builder builder = new FirebaseOptions.Builder().setApplicationId("1:5478125639014:android:054aa87g102b94aa5").setApiKey("ABcdXyzpQrst_8tlItnC5rcbgxkK_PqrstGWsTKo");
FirebaseApp.initializeApp(this, builder.build());
}
添加了所需的清单 Internet 权限。
添加的依赖项:
实施'com.google.firebase:firebase-analytics:17.1.0'
问题是我在那里看不到任何分析数据,应用程序在 Google 分析的最后一步中没有连接。
可能有什么问题或有待解决的问题?
我知道这不是您所期待的答案,但令人遗憾的是,您无法动态初始化 Firebase Analytics。
在官方 Firebase 存储库中,您可以看到问题 https://github.com/firebase/quickstart-android/issues/769,官方答案是 "this is currently working as intended, the Analytics SDK does not allow configuration of the App ID at runtime"。我不相信从那以后它改变了。
我知道这些帖子在那种情况下可能听起来令人困惑:
https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
https://firebase.google.com/docs/projects/multiprojects
但预期用例具有不同的 FirebaseApp
配置(例如,针对不同的数据库)并且 没有 不同的 FirebaseAnalytics
配置。而 Firebase Analytics 只是 Firebase 旗下的众多产品之一。
所以你不需要使用 gradle 插件,但你仍然需要有一个名为 google_app_id
的字符串资源,它包含你的 Firebase 应用程序 ID,只有这样用于配置 FirebaseAnalytics。
这并不意味着 FirebaseAnalytics 的动态初始化根本无法完成,但它需要逆向工程、hacky 解决方案并且它会很脆弱 - 简而言之,可能不是一个好主意。
我不确定你的用例是什么,但如果你只想延迟 Firebase Analytics 初始化,你可以考虑在开始时禁用它:https://firebase.google.com/docs/analytics/configure-data-collection?platform=android
根据此处解释的要点:(不同的问题)
Google Analytics for Firebase (GA4F) doesn't support dynamic
initialization. Our engineers are checking the possible solutions to
support this. It's just that we still haven't found a definite
timeline as to when (or if) this will be available.
GA4F will not work without the google-services.json file (or Gradle on your end). Even though you can initialize the FirebaseApp
dynamically through code, GA4F will not recognize this and will only
result in the error message you are seeing. The scenario you are
getting is only specific to Google Analytics for Firebase. However,
you can still use other products like Firestore, Realtime Database,
Storage even if you are not using Gradle plugin.
最后,按照下面的方式完成:
我需要做什么:
来自一个 Google 帐户,通知应该有效。
必须使用 单独的(另一个) Google 帐户用于 Google 分析。 (阅读问题。你会明白的。)
我做了什么:
- 使用 FirebaseOptions 的集成推送通知任务,将其视为辅助应用程序,如下所示:
FirebaseOptions 选项 = 新 FirebaseOptions.Builder().setApplicationId("1:4578412596352:b47d85se5v2z1a2b").setApiKey("AI4f896IG11gFh5R452FdiYau27sdA7IQ85EdY1").setProjectId("mycuteapp-57490").setGcmSenderId("547841025698 “)。建造();
FirebaseApp.initializeApp(getApplicationContext(), 选项,"pushNotifications");
- 对于 Google 分析,以常用方式完成,即使用 google-services.json 文件。
简而言之,目前官方 link 的指导不适用于 Google Analytics:https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
我必须为我的 android 应用程序添加简单的 Firebase Google 分析。
我需要在运行时 select 我的应用程序的 Firebase 项目。
"It's very rare, but sometimes an app must be able to select its Firebase project at runtime. This means your app can't use the automatic init provided by the FirebaseInitProvider that's merged into your app. In that case, the solution has two tasks."
Link 供参考:https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
所以,就我而言,我做了如下操作:
创建了一个应用程序class:
public class MyApplicationClass extends Application { @Override public void onCreate() { super.onCreate(); FirebaseOptions.Builder builder = new FirebaseOptions.Builder().setApplicationId("1:5478125639014:android:054aa87g102b94aa5").setApiKey("ABcdXyzpQrst_8tlItnC5rcbgxkK_PqrstGWsTKo"); FirebaseApp.initializeApp(this, builder.build()); }
添加了所需的清单 Internet 权限。
添加的依赖项:
实施'com.google.firebase:firebase-analytics:17.1.0'
问题是我在那里看不到任何分析数据,应用程序在 Google 分析的最后一步中没有连接。
可能有什么问题或有待解决的问题?
我知道这不是您所期待的答案,但令人遗憾的是,您无法动态初始化 Firebase Analytics。
在官方 Firebase 存储库中,您可以看到问题 https://github.com/firebase/quickstart-android/issues/769,官方答案是 "this is currently working as intended, the Analytics SDK does not allow configuration of the App ID at runtime"。我不相信从那以后它改变了。
我知道这些帖子在那种情况下可能听起来令人困惑: https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html https://firebase.google.com/docs/projects/multiprojects
但预期用例具有不同的 FirebaseApp
配置(例如,针对不同的数据库)并且 没有 不同的 FirebaseAnalytics
配置。而 Firebase Analytics 只是 Firebase 旗下的众多产品之一。
所以你不需要使用 gradle 插件,但你仍然需要有一个名为 google_app_id
的字符串资源,它包含你的 Firebase 应用程序 ID,只有这样用于配置 FirebaseAnalytics。
这并不意味着 FirebaseAnalytics 的动态初始化根本无法完成,但它需要逆向工程、hacky 解决方案并且它会很脆弱 - 简而言之,可能不是一个好主意。
我不确定你的用例是什么,但如果你只想延迟 Firebase Analytics 初始化,你可以考虑在开始时禁用它:https://firebase.google.com/docs/analytics/configure-data-collection?platform=android
根据此处解释的要点:
Google Analytics for Firebase (GA4F) doesn't support dynamic initialization. Our engineers are checking the possible solutions to support this. It's just that we still haven't found a definite timeline as to when (or if) this will be available.
GA4F will not work without the google-services.json file (or Gradle on your end). Even though you can initialize the FirebaseApp dynamically through code, GA4F will not recognize this and will only result in the error message you are seeing. The scenario you are getting is only specific to Google Analytics for Firebase. However, you can still use other products like Firestore, Realtime Database, Storage even if you are not using Gradle plugin.
最后,按照下面的方式完成:
我需要做什么:
来自一个 Google 帐户,通知应该有效。
必须使用 单独的(另一个) Google 帐户用于 Google 分析。 (阅读问题。你会明白的。)
我做了什么:
- 使用 FirebaseOptions 的集成推送通知任务,将其视为辅助应用程序,如下所示:
FirebaseOptions 选项 = 新 FirebaseOptions.Builder().setApplicationId("1:4578412596352:b47d85se5v2z1a2b").setApiKey("AI4f896IG11gFh5R452FdiYau27sdA7IQ85EdY1").setProjectId("mycuteapp-57490").setGcmSenderId("547841025698 “)。建造(); FirebaseApp.initializeApp(getApplicationContext(), 选项,"pushNotifications");
- 对于 Google 分析,以常用方式完成,即使用 google-services.json 文件。
简而言之,目前官方 link 的指导不适用于 Google Analytics:https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html