简体   繁体   中英

Android - open Chrome custom tab instead of launch web browser PublisherAdView

I have been searching the web for finding a way of disabling the on-click for a PublisherAdView but to no avail. We are using DFP for ads https://developers.google.com/mobile-ads-sdk/docs/dfp/android/quick-start . When a user clicks the ad in the app, the DFP SDK opens a web browser. However, I would like to open a Chrome custom tab instead of launching a web browser. Is this possible?

I know its a little late, but here is a way I was able to solve this.

PublisherAdView does not expose any methods to accomplish this. One way to accomplish this us to use custom events.

The idea is to fire a custom event on the DFP side when users click on your ad and capture it using AppEventListener on your android app.

on DFP you would set up your template or style like this:

<script src="//media.admob.com/api/v1/google_mobile_app_ads.js"></script>
<script>
    handleClick = function() {
      admob.events.dispatchAppEvent("destinationUrl", "%%DEST_URL%%");
    };
  </script>
<div onClick="handleClick()"> ...ad template here... </div>

And then on your Android app, add an AppEventListener to capture the event:

private static final String DFP_DESTINATION_URL = "destinationUrl";

...

PublisherAdView publisherAdView = new PublisherAdView(getActivity());
publisherAdView.setAppEventListener((eventName, eventValue) -> { // or use publisherAdView.setAppEventListener(new AppEventListener() {...});  if you don't use lambdas
    if (DFP_DESTINATION_URL.equals(eventName)) {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        /// setup your intent as needed
        CustomTabsIntent customTabsIntent = builder.build();
        /// And finally open the custom chrome tab
        customTabsIntent.launchUrl(this, Uri.parse(eventValue));
    }
});
publisherAdView.loadAd(mPublisherAdRequest);

Hope this helps or points you on the right direction to solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM