Android Webview Implementation

Android Webview Implementation

Prerequisites

Step Summary

  1. Add The initialization code.

  2. Create the Channel between web and native with configureWebView() method

  3. Use loadUrlWithConsentDataOrWait to load your urls into the webview

  4. Test everything is working

  5. Change everything to production cod

  6. Your app is now prepared for monetization with us.

 

Remember that you should be using our CMP also in web so consent data can be understood.

If you are placing all or some ads natively, meaning that the actions on the web view trigger native code through some kind of javascript interface, then we recommend using Manual Implementation or if you are displaying some ads inside the webview and others in the native app you can use this implementation and the manual together to make this happen.

Initialize the SDK

We require the SDK to be initialized only once as early as possible so for this purpose using Android’s application class is recommended but if your app only has one activity, if using the activity make sure it is never destroyed unless killing the app or placing it in the background, matching then the lifecycle of Application, you can initialize it in onCreate method of that Activity.

Let’s see the recommended method.

Create an Application class and add it to the manifest code to start the app with the Application class.

class Application: Application() { override fun onCreate() { super.onCreate() R89SDK.setDebug() //This is for testing purposes, remove it on prod R89SDK.setLogLevel(LogLevels.DEBUG) //This is for testing purposes, remove it on prod R89SDK.initialize( appContext = this, publisherId = "TestRefinery89ID", /* This is for testing purposes, change it on prod */ appId = "TestDemoApp", /* This is for testing purposes, change it on prod */ singleLine = false, initializationEvents = null ) } }

Your publisherId and appId will be provided to you during onboarding process by email, you can also retrieve them from the web interface after login.

This appID is not the same as the Manifest App Id you used in previous steps

Then add this application class to the manifest you already had from the Android Get Started.

The code for adding the application is the following

Remember that the ".Application" depends on the name of the class. If we named the class MyApplication we would need to put “.MyApplication"

<manifest> <application android:name=".Application"> </application> </manifest>

With this addition your full manifest should look like so:

This is a simplified example, you should have many more lines in the manifest, copy the important bits only.

<manifest> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.gms.permission.AD_ID"/> <application android:name=".Application"> <!-- This is the Sample App ID--> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> </application> </manifest>

 

Until here is exactly the same as the Manual implementation, this is why this implementation is compatible with manual, but now instead of focusing in wrappers, events and formats we will see how to create the channel and how to load urls to have consent in them.

If you want to have native ads to be triggered from the webview we recommend using the manual configuration alongside some kind of javascript interface implemented in the web side to trigger native code, although first finish this whole guide and then go to the manual

Create the Channel

Right after Initialization you should use the following methods

override fun onCreate(savedInstanceState: Bundle?) { webview = findViewById(R.id.webview) val url = YOUR URL //This helps the channel to identify this as an app val userAgent = R89SDK.getUserAgent(webview!!, <YOUR-SITE-NAME>) /* The R89WebViewClient behaves as a normal WebViewClient so you can override the same methods but some of them are called the same with an [r89] prefix so keep in mind in case you can't override a method you would normally could [pageStarted] would be [r89pageStarted] they will behave exactly the same. */ R89SDK.configureWebView(webview!!, userAgent, object : R89WebViewClient() { }) }

Load Urls with consent

Use the following method replacing any android webview.loadUrl() methods you might have

R89SDK.loadUrlWithConsentDataOrWait(webview!!, url)

This url must only be the urls related to you content, or own. Other urls can be loaded normally. with webview.loadUrl()

Go into production

  • Remove the methods R89SDK.setDebug()

  • Change the Google app id in the manifest for the production one

  • Change the appId and publisherId in the initialization method for the production ones

  • Change the r89ConfigId in the formats for the production ones

  • it’s also recommended to remove R89SDK.setLogLevel(LogLevels.DEBUG) but not needed

How does it work?

Works by creating a channel linking our SDK to your current in-web monetization solutions. This channel just makes sure that the ad units in web know that current environment is app and also sends the consent data so you get the same consent in both sides complying with privacy policies.