Guide for Xamarin.Google.UserMessagingPlatform?

adamhalperin 26 Reputation points
2021-07-19T11:21:20.01+00:00

I am in the final stages of setting up my Android App using Xamarin / C#. I have implemented Google Admob but GDPR rules say that I must have a privacy notice to display ads. Google's documentation says that the Consent SDK is deprecated and that I should use the new User Messaging Platform https://developers.google.com/admob/ump/android/quick-start

I have downloaded the Nuget package Xamarin.Google.UserMessagingPlatform (https://www.nuget.org/packages/Xamarin.Google.UserMessagingPlatform/1.0.0?_src=template) and have imported the libraries but I am struggling to translate Google's code to my project and having searched online there does not appear to be a live documentation link anywhere with examples of implementation in C# / Xamarin. The project site on the package URL 404s and the source repository leads to the general Xamarin repository but I couldn't find a reference to UMP in there.

Is there any documentation available or any examples of the UMP implementation in C#?

Specifically, an example statement I do not know how to handle is this:

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.
            }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Jim Chapman 11 Reputation points
    2021-09-30T17:55:39.033+00:00

    In case it's useful, @adamhalperin : In MainActivity.OnCreate, some time after the call to base.OnCreate(bundle); insert this code snippet:

    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.");  
                                            ViewModel.MainViewModel.OnceViewModelInitialised(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);  
                }  
    

    Then, in the body of the MainActivity class, you will also need to add these definitions:

        private IConsentForm googleUMPConsentForm = null;  
        private IConsentInformation googleUMPConsentInformation = null;  
        public void DisplayAdvertisingConsentFormIfNecessary()  
        {  
            try  
            {  
                if (ViewModel.MainViewModel.Instance.AdvertisingEnabled)  
                {  
                    if (googleUMPConsentForm != null && googleUMPConsentInformation != null)  
                    {  
                        /* ConsentStatus:  
                            Unknown = 0,  
                            Required = 1,  
                            NotRequired = 2,  
                            Obtained = 3  
                        */  
                        if (googleUMPConsentInformation.ConsentStatus == 1)  
                        {  
                            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 (ViewModel.MainViewModel.Instance.AdvertisingEnabled)  
                {  
                    if (googleUMPConsentForm != null && googleUMPConsentInformation != null)  
                    {  
                        App.LogMessage("DEBUG: MainActivity.DisplayAdvertisingConsentForm: Consent form is being displayed.");  
    
                        Model.DispatcherHelper.CheckBeginInvokeOnUI(() =>  
                        {  
                              
                            googleUMPConsentForm.Show(this, new GoogleUMPConsentFormDismissedListener(  
                                (FormError f) =>  
                                {  
                                    if (googleUMPConsentInformation.ConsentStatus == 1) // required  
                                    {  
                                        App.LogMessage("ERROR: MainActivity.DisplayAdvertisingConsentForm: Consent was dismissed; showing it again because consent is still required.");  
    
                                        Model.DispatcherHelper.RunAsyncOnUI(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;  
        }  
    

    This is just a cut-and-paste of my code - in your case you may need to do something different to replace the call to Model.DispatcherHelper.RunAsyncOnUI/Model.DispatcherHelper.CheckBeginInvokeOnUI (which I use to run something on the UI thread), App.LogMessage which I use to write to the app log, and ViewModel.MainViewModel.Instance.AdvertisingEnabled, which is used to check whether the app is meant to display advertising (it will be false if the user paid to remove advertising)

    1 person found this answer helpful.

  2. Federico Navarrete 616 Reputation points
    2022-03-01T17:36:42.393+00:00

    I wrote a full tutorial in my blog if it is useful for you or anyone:

    https://supernovaic.blogspot.com/2022/02/guide-for-using-xamaringoogleusermessag.html

    1 person found this answer helpful.