gpt4 book ai didi

c# - Xamarin.Google.UserMessagingPlatform 指南?

转载 作者:行者123 更新时间:2023-12-03 08:19:31 24 4
gpt4 key购买 nike

我正处于使用 Xamarin/C# 设置 Android 应用程序的最后阶段。我已经实现了 Google Admob,但 GDPR 规则规定我必须有隐私声明才能展示广告。 Google 的文档显示 Consent SDK 已弃用,我应该使用新的用户消息传递平台 https://developers.google.com/admob/ump/android/quick-start

我已经下载了 Nuget 包 Xamarin.Google.UserMessagingPlatform ( https://www.nuget.org/packages/Xamarin.Google.UserMessagingPlatform/1.0.0?_src=template ) 并导入了库,但我正在努力将 Google 的代码翻译到我的项目中,并且在线搜索后似乎没有任何地方有实时文档链接以及 C#/Xamarin 中的实现示例。包 URL 404 上的项目站点和源存储库通向通用 Xamarin 存储库,但我在其中找不到对 UMP 的引用。

具体来说,我不知道如何处理的一个说法是:

new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}

有没有C#实现的例子?

最佳答案

在MainActivity.OnCreate中,调用base.OnCreate(bundle)后一段时间;插入此代码 fragment :

        App.LogMessage("DEBUG: MainActivity.OnCreate: Starting consent management flow, via UserMessagingPlatform.");
try
{
ConsentRequestParameters requestParameters = new ConsentRequestParameters
.Builder()
.SetTagForUnderAgeOfConsent(false)
.Build();

IConsentInformation consentInformation = UserMessagingPlatform.GetConsentInformation(this);

consentInformation.RequestConsentInfoUpdate(
this,
requestParameters,
new GoogleUMPConsentUpdateSuccessListener(
() =>
{
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.IsConsentFormAvailable)
{
UserMessagingPlatform.LoadConsentForm(
this,
new GoogleUMPFormLoadSuccessListener((IConsentForm f)=> {
this.googleUMPConsentForm = f;
this.googleUMPConsentInformation = consentInformation;
App.LogMessage("DEBUG: MainActivity.OnCreate: Consent management flow: LoadConsentForm has loaded a form, which will be shown if necessary, once the ViewModel is ready.");
DisplayAdvertisingConsentFormIfNecessary();
}),
new GoogleUMPFormLoadFailureListener((FormError e)=> {
// Handle the error.
App.LogMessage("ERROR: MainActivity.OnCreate: Consent management flow: failed in LoadConsentForm with error " + e.Message);
}));
}
else
{
App.LogMessage("DEBUG: MainActivity.OnCreate: Consent management flow: RequestConsentInfoUpdate succeeded but no consent form was available.");
}
}),
new GoogleUMPConsentUpdateFailureListener(
(FormError e) =>
{
// Handle the error.
App.LogMessage("ERROR: MainActivity.OnCreate: Consent management flow: failed in RequestConsentInfoUpdate with error " + e.Message);
})
);
}
catch (Exception ex)
{
App.LogMessage("ERROR: MainActivity.OnCreate: Exception thrown during consent management flow: ", ex);
}

然后,在 MainActivity 类的主体中,您还需要添加以下定义:

    private IConsentForm googleUMPConsentForm = null;
private IConsentInformation googleUMPConsentInformation = null;
public void DisplayAdvertisingConsentFormIfNecessary()
{
try
{
if (googleUMPConsentForm != null && googleUMPConsentInformation != null)
{
/* ConsentStatus:
Unknown = 0,
NotRequired = 1,
Required = 2,
Obtained = 3
*/
if (googleUMPConsentInformation.ConsentStatus == 2)
{
App.LogMessage("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is being displayed.");
DisplayAdvertisingConsentForm();
}
else
{
App.LogMessage("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is not being displayed because consent status is "+ googleUMPConsentInformation.ConsentStatus.ToString());
}
}
else
{
App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: consent form or consent information missing.");
}
}
catch(Exception ex)
{
App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Exception thrown: ", ex);
}
}

public void DisplayAdvertisingConsentForm()
{
try
{
if (googleUMPConsentForm != null && googleUMPConsentInformation != null)
{
App.LogMessage("DEBUG: MainActivity.DisplayAdvertisingConsentForm: Consent form is being displayed.");

googleUMPConsentForm.Show(this, new GoogleUMPConsentFormDismissedListener(
(FormError f) =>
{
if (googleUMPConsentInformation.ConsentStatus == 2) // required
{
App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentForm: Consent was dismissed; showing it again because consent is still required.");
DisplayAdvertisingConsentForm();
}
}));
}
else
{
App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentForm: consent form or consent information missing.");
}
}
catch (Exception ex)
{
App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentForm: Exception thrown: ", ex);
}
}

public class GoogleUMPConsentFormDismissedListener : Java.Lang.Object, IConsentFormOnConsentFormDismissedListener
{
public GoogleUMPConsentFormDismissedListener(Action<FormError> failureAction)
{
this.a = failureAction;
}
public void OnConsentFormDismissed(FormError f)
{
a(f);
}

private Action<FormError> a = null;
}


public class GoogleUMPConsentUpdateFailureListener : Java.Lang.Object, IConsentInformationOnConsentInfoUpdateFailureListener
{
public GoogleUMPConsentUpdateFailureListener(Action<FormError> failureAction)
{
this.a = failureAction;
}
public void OnConsentInfoUpdateFailure(FormError f)
{
a(f);
}

private Action<FormError> a = null;
}

public class GoogleUMPConsentUpdateSuccessListener : Java.Lang.Object, IConsentInformationOnConsentInfoUpdateSuccessListener
{
public GoogleUMPConsentUpdateSuccessListener(Action successAction)
{
this.a = successAction;
}

public void OnConsentInfoUpdateSuccess()
{
a();
}

private Action a = null;
}

public class GoogleUMPFormLoadFailureListener : Java.Lang.Object, UserMessagingPlatform.IOnConsentFormLoadFailureListener
{
public GoogleUMPFormLoadFailureListener(Action<FormError> failureAction)
{
this.a = failureAction;
}
public void OnConsentFormLoadFailure(FormError e)
{
a(e);
}

private Action<FormError> a = null;
}

public class GoogleUMPFormLoadSuccessListener : Java.Lang.Object, UserMessagingPlatform.IOnConsentFormLoadSuccessListener
{
public GoogleUMPFormLoadSuccessListener(Action<IConsentForm> successAction)
{
this.a = successAction;
}
public void OnConsentFormLoadSuccess(IConsentForm f)
{
a(f);
}

private Action<IConsentForm> a = null;
}

关于c# - Xamarin.Google.UserMessagingPlatform 指南?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68302342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com