英文:
App Branching with Google Analytics 4: Got an exception while running a Xamarin Forms Android app
问题
I am a Xamarin developer and I have followed the URL https://help.branch.io/partners-portal/docs/google-analytics-4#properties-sent-to-google-analytics to integrate Google Analytics 4 in Xamarin forms Android to send branch data to Google Analytics dashboard, but I got exception while running the app, saying
> BranchXamarinSDK.BranchException: you must initialize Branch before you can use the Branch object
I have implemented code below in MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set the request metadata before initializing the Branch SDK
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_app_instance_id", (string) FirebaseAnalytics.GetInstance(this).GetAppInstanceId());
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_user_id", "{user_id}");
BranchAndroid.Init(this, Resources.GetString(Resource.String.branch_test_key), this);
LoadApplication(new App());
}
英文:
I am a Xamarin developer and I have followed the URL https://help.branch.io/partners-portal/docs/google-analytics-4#properties-sent-to-google-analytics to integrate Google Analytics 4 in Xamarin forms Android to send branch data to Google Analytics dashboard, but I got exception while running the app, saying
> BranchXamarinSDK.BranchException: you must initialize Branch before you can use the Branch object
I have implemented code below in MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set the request metadata before initializing the Branch SDK
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_app_instance_id", (string) FirebaseAnalytics.GetInstance(this).GetAppInstanceId());
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_user_id", "{user_id}");
BranchAndroid.Init(this, Resources.GetString(Resource.String.branch_test_key), this);
LoadApplication(new App());
}
答案1
得分: 1
不要,如果你想调用 SetRequestMetadata()
方法,BranchAndroid.GetInstance()
必须有值,否则将为 null。
此外,我已经检查了关于 Branch 的官方文档以及关于 BranchAndroid 的资源代码。
官方文档说:
所以,在会话初始化之前和实例初始化之后调用 SetRequestMetadata()
就可以了。你的代码应该是:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
BranchAndroid.Init(this, Resources.GetString(Resource.String.branch_test_key), this);
// 在初始化会话之前设置请求元数据
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_app_instance_id", (string) FirebaseAnalytics.GetInstance(this).GetAppInstanceId());
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_user_id", "{user_id}");
LoadApplication(new App());
}
英文:
No, if you want to call the SetRequestMetadata()
method, the BranchAndroid.GetInstance()
must have value or it will be null.
In addition, I have check the official document about the Branch and the resource code about the BranchAndroid.
The official document said:
So just make the SetRequestMetadata()
called before the session initialization and after the instance initialization is ok. Your code should be:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
BranchAndroid.Init(this, Resources.GetString(Resource.String.branch_test_key), this);
// Set the request metadata before initializing the Session
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_app_instance_id", (string) FirebaseAnalytics.GetInstance(this).GetAppInstanceId());
BranchAndroid.GetInstance().SetRequestMetadata("$firebase_user_id", "{user_id}");
LoadApplication(new App());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论