Xamarin.Google.UserMessagingPlatform 指南?
Guide for Xamarin.Google.UserMessagingPlatform?
我正处于使用 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 的代码翻译到我的项目并在那里进行了在线搜索在任何地方似乎都不是实时文档 link,其中包含 C#/Xamarin 中的实施示例。包 URL 404s 上的项目站点和源存储库通向通用 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)之后的某个时间;插入此代码段:
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 的主体中 class,您还需要添加这些定义:
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;
}
我在我的博客中写了完整的 tutorial,但这里有:
AdMob 部分
转到您的AdMob account。
转到隐私和消息并选择phone图标。
- 创建新消息并填写所需信息。
- 配置您的消息:
- 为您的应用添加隐私政策:
- 保存并发布。
C# 部分
从 NuGet 下载 Xamarin.Google.UserMessagingPlatform。
Double-check 你的 AndroidManifest.xml 有这两行:
<activity android:name="com.google.android.gms.ads.AdActivity" android:value="AD_UNIT_ID" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="APP_ID" />
您可以从您的 AdMob 帐户获得 AD_UNIT_ID 和 APP_ID:
- Load/copy这一行之后的GDPR代码(之前不要这样做!):
MobileAds.Initialize(this);
- 复制并粘贴此代码:
private void SetGDPR()
{
Console.WriteLine("DEBUG: MainActivity.OnCreate: Starting consent management flow, via UserMessagingPlatform.");
try
{
#if DEBUG
var debugSettings = new Xamarin.Google.UserMesssagingPlatform.ConsentDebugSettings.Builder(this)
.SetDebugGeography(Xamarin.Google.UserMesssagingPlatform.ConsentDebugSettings
.DebugGeography
.DebugGeographyEea)
.AddTestDeviceHashedId(Android.Provider.Settings.Secure.GetString(this.ContentResolver,
Android.Provider.Settings.Secure.AndroidId))
.Build();
#endif
var requestParameters = new Xamarin.Google.UserMesssagingPlatform.ConsentRequestParameters
.Builder()
.SetTagForUnderAgeOfConsent(false)
#if DEBUG
.SetConsentDebugSettings(debugSettings)
#endif
.Build();
var consentInformation = Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.GetConsentInformation(Context);
consentInformation.RequestConsentInfoUpdate(
Activity,
requestParameters,
new GoogleUMPConsentUpdateSuccessListener(
() =>
{
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.IsConsentFormAvailable)
{
Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.LoadConsentForm(
Context,
new GoogleUMPFormLoadSuccessListener((Xamarin.Google.UserMesssagingPlatform.IConsentForm f) => {
googleUMPConsentForm = f;
googleUMPConsentInformation = consentInformation;
Console.WriteLine("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((Xamarin.Google.UserMesssagingPlatform.FormError e) => {
// Handle the error.
Console.WriteLine("ERROR: MainActivity.OnCreate: Consent management flow: failed in LoadConsentForm with error " + e.Message);
}));
}
else
{
Console.WriteLine("DEBUG: MainActivity.OnCreate: Consent management flow: RequestConsentInfoUpdate succeeded but no consent form was available.");
}
}),
new GoogleUMPConsentUpdateFailureListener(
(Xamarin.Google.UserMesssagingPlatform.FormError e) =>
{
// Handle the error.
Console.WriteLine("ERROR: MainActivity.OnCreate: Consent management flow: failed in RequestConsentInfoUpdate with error " + e.Message);
})
);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.OnCreate: Exception thrown during consent management flow: ", ex);
}
}
private Xamarin.Google.UserMesssagingPlatform.IConsentForm googleUMPConsentForm = null;
private Xamarin.Google.UserMesssagingPlatform.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)
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is being displayed.");
DisplayAdvertisingConsentForm();
}
else
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is not being displayed because consent status is " + googleUMPConsentInformation.ConsentStatus.ToString());
}
}
else
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: consent form or consent information missing.");
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Exception thrown: ", ex);
}
}
public void DisplayAdvertisingConsentForm()
{
try
{
if (googleUMPConsentForm != null && googleUMPConsentInformation != null)
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentForm: Consent form is being displayed.");
googleUMPConsentForm.Show(Activity, new GoogleUMPConsentFormDismissedListener(
(Xamarin.Google.UserMesssagingPlatform.FormError f) =>
{
if (googleUMPConsentInformation.ConsentStatus == 2) // required
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: Consent was dismissed; showing it again because consent is still required.");
DisplayAdvertisingConsentForm();
}
}));
}
else
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: consent form or consent information missing.");
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: Exception thrown: ", ex);
}
}
public class GoogleUMPConsentFormDismissedListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentFormOnConsentFormDismissedListener
{
public GoogleUMPConsentFormDismissedListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentFormDismissed(Xamarin.Google.UserMesssagingPlatform.FormError f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPConsentUpdateFailureListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentInformationOnConsentInfoUpdateFailureListener
{
public GoogleUMPConsentUpdateFailureListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentInfoUpdateFailure(Xamarin.Google.UserMesssagingPlatform.FormError f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPConsentUpdateSuccessListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentInformationOnConsentInfoUpdateSuccessListener
{
public GoogleUMPConsentUpdateSuccessListener(Action successAction)
{
a = successAction;
}
public void OnConsentInfoUpdateSuccess()
{
a();
}
private Action a = null;
}
public class GoogleUMPFormLoadFailureListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.IOnConsentFormLoadFailureListener
{
public GoogleUMPFormLoadFailureListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentFormLoadFailure(Xamarin.Google.UserMesssagingPlatform.FormError e)
{
a(e);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPFormLoadSuccessListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.IOnConsentFormLoadSuccessListener
{
public GoogleUMPFormLoadSuccessListener(Action<Xamarin.Google.UserMesssagingPlatform.IConsentForm> successAction)
{
a = successAction;
}
public void OnConsentFormLoadSuccess(Xamarin.Google.UserMesssagingPlatform.IConsentForm f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.IConsentForm> a = null;
}
- 加载 GDPR:
MobileAds.Initialize(this);
SetGDPR();
仅此而已!
我正处于使用 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 的代码翻译到我的项目并在那里进行了在线搜索在任何地方似乎都不是实时文档 link,其中包含 C#/Xamarin 中的实施示例。包 URL 404s 上的项目站点和源存储库通向通用 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)之后的某个时间;插入此代码段:
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 的主体中 class,您还需要添加这些定义:
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;
}
我在我的博客中写了完整的 tutorial,但这里有:
AdMob 部分
转到您的AdMob account。
转到隐私和消息并选择phone图标。
- 创建新消息并填写所需信息。
- 配置您的消息:
- 为您的应用添加隐私政策:
- 保存并发布。
C# 部分
从 NuGet 下载 Xamarin.Google.UserMessagingPlatform。
Double-check 你的 AndroidManifest.xml 有这两行:
<activity android:name="com.google.android.gms.ads.AdActivity" android:value="AD_UNIT_ID" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="APP_ID" />
您可以从您的 AdMob 帐户获得 AD_UNIT_ID 和 APP_ID:
- Load/copy这一行之后的GDPR代码(之前不要这样做!):
MobileAds.Initialize(this);
- 复制并粘贴此代码:
private void SetGDPR()
{
Console.WriteLine("DEBUG: MainActivity.OnCreate: Starting consent management flow, via UserMessagingPlatform.");
try
{
#if DEBUG
var debugSettings = new Xamarin.Google.UserMesssagingPlatform.ConsentDebugSettings.Builder(this)
.SetDebugGeography(Xamarin.Google.UserMesssagingPlatform.ConsentDebugSettings
.DebugGeography
.DebugGeographyEea)
.AddTestDeviceHashedId(Android.Provider.Settings.Secure.GetString(this.ContentResolver,
Android.Provider.Settings.Secure.AndroidId))
.Build();
#endif
var requestParameters = new Xamarin.Google.UserMesssagingPlatform.ConsentRequestParameters
.Builder()
.SetTagForUnderAgeOfConsent(false)
#if DEBUG
.SetConsentDebugSettings(debugSettings)
#endif
.Build();
var consentInformation = Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.GetConsentInformation(Context);
consentInformation.RequestConsentInfoUpdate(
Activity,
requestParameters,
new GoogleUMPConsentUpdateSuccessListener(
() =>
{
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.IsConsentFormAvailable)
{
Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.LoadConsentForm(
Context,
new GoogleUMPFormLoadSuccessListener((Xamarin.Google.UserMesssagingPlatform.IConsentForm f) => {
googleUMPConsentForm = f;
googleUMPConsentInformation = consentInformation;
Console.WriteLine("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((Xamarin.Google.UserMesssagingPlatform.FormError e) => {
// Handle the error.
Console.WriteLine("ERROR: MainActivity.OnCreate: Consent management flow: failed in LoadConsentForm with error " + e.Message);
}));
}
else
{
Console.WriteLine("DEBUG: MainActivity.OnCreate: Consent management flow: RequestConsentInfoUpdate succeeded but no consent form was available.");
}
}),
new GoogleUMPConsentUpdateFailureListener(
(Xamarin.Google.UserMesssagingPlatform.FormError e) =>
{
// Handle the error.
Console.WriteLine("ERROR: MainActivity.OnCreate: Consent management flow: failed in RequestConsentInfoUpdate with error " + e.Message);
})
);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.OnCreate: Exception thrown during consent management flow: ", ex);
}
}
private Xamarin.Google.UserMesssagingPlatform.IConsentForm googleUMPConsentForm = null;
private Xamarin.Google.UserMesssagingPlatform.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)
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is being displayed.");
DisplayAdvertisingConsentForm();
}
else
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Consent form is not being displayed because consent status is " + googleUMPConsentInformation.ConsentStatus.ToString());
}
}
else
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: consent form or consent information missing.");
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentFormIfNecessary: Exception thrown: ", ex);
}
}
public void DisplayAdvertisingConsentForm()
{
try
{
if (googleUMPConsentForm != null && googleUMPConsentInformation != null)
{
Console.WriteLine("DEBUG: MainActivity.DisplayAdvertisingConsentForm: Consent form is being displayed.");
googleUMPConsentForm.Show(Activity, new GoogleUMPConsentFormDismissedListener(
(Xamarin.Google.UserMesssagingPlatform.FormError f) =>
{
if (googleUMPConsentInformation.ConsentStatus == 2) // required
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: Consent was dismissed; showing it again because consent is still required.");
DisplayAdvertisingConsentForm();
}
}));
}
else
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: consent form or consent information missing.");
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: MainActivity.DisplayAdvertisingConsentForm: Exception thrown: ", ex);
}
}
public class GoogleUMPConsentFormDismissedListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentFormOnConsentFormDismissedListener
{
public GoogleUMPConsentFormDismissedListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentFormDismissed(Xamarin.Google.UserMesssagingPlatform.FormError f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPConsentUpdateFailureListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentInformationOnConsentInfoUpdateFailureListener
{
public GoogleUMPConsentUpdateFailureListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentInfoUpdateFailure(Xamarin.Google.UserMesssagingPlatform.FormError f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPConsentUpdateSuccessListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.IConsentInformationOnConsentInfoUpdateSuccessListener
{
public GoogleUMPConsentUpdateSuccessListener(Action successAction)
{
a = successAction;
}
public void OnConsentInfoUpdateSuccess()
{
a();
}
private Action a = null;
}
public class GoogleUMPFormLoadFailureListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.IOnConsentFormLoadFailureListener
{
public GoogleUMPFormLoadFailureListener(Action<Xamarin.Google.UserMesssagingPlatform.FormError> failureAction)
{
a = failureAction;
}
public void OnConsentFormLoadFailure(Xamarin.Google.UserMesssagingPlatform.FormError e)
{
a(e);
}
private Action<Xamarin.Google.UserMesssagingPlatform.FormError> a = null;
}
public class GoogleUMPFormLoadSuccessListener : Java.Lang.Object, Xamarin.Google.UserMesssagingPlatform.UserMessagingPlatform.IOnConsentFormLoadSuccessListener
{
public GoogleUMPFormLoadSuccessListener(Action<Xamarin.Google.UserMesssagingPlatform.IConsentForm> successAction)
{
a = successAction;
}
public void OnConsentFormLoadSuccess(Xamarin.Google.UserMesssagingPlatform.IConsentForm f)
{
a(f);
}
private Action<Xamarin.Google.UserMesssagingPlatform.IConsentForm> a = null;
}
- 加载 GDPR:
MobileAds.Initialize(this);
SetGDPR();
仅此而已!