App Branching with Google Analytics 4: 在运行 Xamarin Forms Android 应用程序时遇到异常

huangapple go评论47阅读模式
英文:

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()); 

} 

App Branching with Google Analytics 4: 在运行 Xamarin Forms Android 应用程序时遇到异常

App Branching with Google Analytics 4: 在运行 Xamarin Forms Android 应用程序时遇到异常

答案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:

App Branching with Google Analytics 4: 在运行 Xamarin Forms Android 应用程序时遇到异常

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()); 

} 

huangapple
  • 本文由 发表于 2023年5月25日 09:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定