Android Native Ads Integration

Native Ads are a component-based ad format that allow publisher to display the customized ads on the application, how layouts headlines and call to action button. By choosing colors, image size and fonts etc for your application, you are able to display natural, well matched advertisement that can give better user experience. Ad impressions, ad clicks tracking, processing Advertising SDK.

Preparation

Preparation Integration for Native Ads, please look at [Install Android SDK] below.
You must install Geniee SDK to project.
Install Android SDK

Class and Interface

Android native ad delivery, use the following class.

  • GNNativeAdRequest : Class to Request Native Ad with asynchronous
  • GNNativeAd : Class to provide information of Native Ad
  • GNNativeAdRequestListener Interface to receive loading event of Native Ad

Set Up Native Ads

  1. Add SDK to your project. Install Android SDK

  2. Import GNNativeAd

    import jp.co.geniee.sdk.ads.nativead.GNNativeAd;
    import jp.co.geniee.sdk.ads.nativead.GNNativeAdRequest;
    import jp.co.geniee.sdk.ads.nativead.GNNativeAdRequestListener;
    import jp.co.geniee.gnadsdk.common.GNAdLogger;
    
    1
    2
    3
    4
  3. Declare GNNativeAdRequest variable

    GNNativeAdRequest nativeAdRequest;
    
    1
  4. Initialize GNNativeAdRequest instance

  • Initialize API

    public GNNativeAdRequest(Context context, String zoneids)
    
    1
  • Example for using Initialize API

    nativeAdRequest = new GNNativeAdRequest(this, "YOUR_ZONE_ID");
    
    1
  • multiAd Example for using Initialize API

    nativeAdRequest = new GNNativeAdRequest(this, "ZONE_ID_1,ZONE_ID_2,ZONE_ID_3,...,ZONE_ID_10");
    
    1

thisParameter sets Context of App.
YOUR_ZONE_ID sets managementID of AdZones in Geniee.

  1. Implement the GNNativeAdRequestListener callback function, Receive the results of native ad load event.

    GNNativeAdRequestListener nativeListener = new GNNativeAdRequestListener () {
        @Override
        public void onNativeAdsLoaded (GNNativeAd [] nativeAds) {
        }
        @Override
        public void onNativeAdsFailedToLoad (GNSException e) {
        }
        @Override
        public boolean onShouldStartInternalBrowserWithClick (String landingURL) {
            return false;
        }
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

The received native ad GNNativeAd is passed as an arraynativeAds argument. For multi-native advertisement distribution processing, use zoneID information ofGNNativeAd. GNNativeAdRequest When one ZONE_ID is specified at initialization, the number of elements of the arraynativeAds is one.

  1. Set Implementation class of GNNativeAdRequestListener interface

Native ad load event, you will be notified by the implementation class via the interface.
Set the implementation variables of GNNativeAdRequestListener interface.
java nativeAdRequest.setAdListener(this);

  1. Load NativeAds
  • single Ad.
    nativeAdRequest.loadAds(this);
    
    1
  • multi Ad.
    nativeAdRequest.multiLoadAds(this)
    
    1

this parameter sets Context of App.

  • Example for implement ListActivity:
    import jp.co.geniee.sdk.ads.nativead.GNNativeAd;
    import jp.co.geniee.sdk.ads.nativead.GNNativeAdRequest;
    import jp.co.geniee.sdk.ads.nativead.GNNativeAdRequestListener;
    import jp.co.geniee.gnadsdk.common.GNAdLogger;
    
    public class NativeAdSampleActivity extends ListActivity {
        private GNNativeAdRequest nativeAdRequest;
        private GNNativeAdRequestListener nativeListener = new GNNativeAdRequestListener() {
            @Override
            public void onNativeAdsLoaded(GNNativeAd[] nativeAds) {
                for(int i=0; i<nativeAds.length; i++) {
                    queueAds.enqueue(nativeAds[i]);
                }
            }
    
            @Override
            public void onNativeAdsFailedToLoad(GNSException e) {
                Log.w(TAG, "onNativeAdsFailedToLoad Err " + e.getCode() + ":" + e.getMessage());
            }
    
            @Override
            public boolean onShouldStartInternalBrowserWithClick(String landingURL) {
                Log.i("NativeAdSampleActivity","onShouldStartInternalBrowserWithClick : " + landingURL);
                return false;
            }
        };
        @Override
        public void onCreate(Bundle savedInstanceState) {
            nativeAdRequest = new GNNativeAdRequest(this, "YOUR_ZONE_ID");
            nativeAdRequest.setAdListener(this);
            //nativeAdRequest.setGeoLocationEnable(true);
            if (zoneId.split(",").length > 1) {
                nativeAdRequest.multiLoadAds(this);
            } else {
                nativeAdRequest.loadAds(this);
            }
        }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

Rendering Native Ad

You need to render Native Ad based on information of received data.

  • Type of information data, refer definition of GNNativeAd class

  • If the information data (String type) is not set, it will be ""

  • If the information data (int, double type) is not set, it will be 0

    Information Name Type Content of Information
    zoneID String ID for AdZone in Geniee
    advertiser String Name of client
    title String Title of feed advertisement
    description String description of advertisment
    cta String text for "call to action"
    icon_aspectRatio double Aspect ratio of icon image
    icon_url String URL of icon image
    icon_height int heigh of icon image
    icon_width int width of icon image
    screenshots_aspectRatio double aspect ratio of screenshot
    screenshots_url String URL of screenshot image
    screenshots_height int heigh of screenshot image
    screenshots_width int width of screenshot image
    app_appName String Name of App
    app_appid String App ID(iOS:number、Android:package)
    app_rating double Evaluation of App
    app_storeURL String URL of App store
    app_targetAge String Target age of App
  • Example for Implementation of ListActivity:
    When Setting is information of native ad in dashboard below :

    • title
    • description
    • icon image URL
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
    
        Object cell = getItem(position);
        //SDK: Rendering NativeAd content
        if (cell instanceof GNNativeAd) {
            holder.textView.setText(((GNNativeAd)cell).title);
            holder.imageView.setTag(((GNNativeAd)cell).icon_url);
            new ImageGetTask(holder.imageView, ((GNNativeAd)cell).icon_url).execute();
            //SDK: Report impression
            ((GNNativeAd)cell).onTrackingImpression();
        } else {
            // Cell display processing other than GNNativeAd
        }
        return convertView;
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  • You can refer to GNNativeAd class to know opt-out information. Get opt-out information from SDK You can use instance of GNNativeAd nativeAd to access opt-out information

Name Type Data
optout_text String Text for displaying opt-out information
optout_image_url String URL of Opt-out image
optout_url String URL of Opt-out page
String optout_text = nativeAd.optout_text;
String optout_image_url = nativeAd.optout_image_url;
String optout_url = nativeAd.optout_url;
1
2
3
  • Fire click for Opt-out When User clicks to open Opt-out page for setting information, you will open optout_url from your Application

Note: Sometime optout_url can be empty (It means this Ad not support to setup optout information)

  • Example for Implementation of ListActivity Image

    image

Native ad impressions report

  • When the Native Ad is rendered, you can extract the ad impression report.

  • For impressions native ads that has been previously reported, can not be reported again.

  • You need to request Native Ad again to display new advertisement.

    nativeAd.onTrackingImpression();
    
    1

Native ad click tracking

  • When Native ad is clicked, it will lead to ad landing page in external browser.

    nativeAd.onTrackingClick();
    
    1
  • Example for Implementation of ListActivity:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ListView listView = (ListView)parent;
        Object cell = listView.getItemAtPosition(position);
        if (cell instanceof GNNativeAd) {
            //SDK: Report click
            ((GNNativeAd)cell).onTrackingClick();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

Landing page screen transition control when it is clicked

Landing page of Ads starts on an external browser by default,
Implement CallBack function of GNNativeAdRequestListener,
With URL of landing page it is possible to start in-app browser.
In addition, by the return value of function, to control the start-up of an external browser.
If it returns true, you will need to request AppSide landingURL.

  • false : Start the false external browser.

  • true : Do not start the true external browser.

    public boolean onShouldStartInternalBrowserWithClick(String landingURL)
    
    1

Re-acquisition of native advertising

  • To view the new ads, you will need to re-acquire Native advertising.

    • single Ad.
    nativeAdRequest.loadAds(this);
    
    1
  • multi Ad.

    nativeAdRequest.multiLoadAds(this)
    
    1

Video Native Ad

By adding option to allow movies with Native Ad setting information from the management screen, you can acquire video native advertisement.

Classes and Event Listeners

For Android video native ads, use the following video player classes in addition to classic classes.

  • GNSNativeVideoPlayerListener Video listener for receiving native player's load results
  • GNSNativeVideoPlayerView Class for providing video native player view

Video native ad determination

When viewing movie native ads, please use hasVideoContent() from GNNativeAd instance to make motion picture judgment.

Method Return type Contents
hasVideoContent() boolean Video ads holding decision
public void onNativeAdsLoaded(GNNativeAd[] nativeAds) {
    for (final GNNativeAd ad : nativeAds) {
        Log.i(TAG, ad.hasVideoContent());
    }
}
1
2
3
4
5

Native video implementation method

  1. Import GNSNativeVideoPlayerView andGNSNativeVideoPlayerListener.

    import jp.co.geniee.sdk.ads.nativead.GNSNativeVideoPlayerListener;
    import jp.co.geniee.sdk.ads.nativead.GNSNativeVideoPlayerView;
    
    1
    2
  2. Generate instances of GNSNativeVideoPlayerView and add views

  • Example of generation from NativeAd

    GNSNativeVideoPlayerView videoView = ((GNNativeAd) ad).getVideoView();
    containerView.AddView (videoView);
    
    1
    2
  • New generation example

    GNSNativeVideoPlayerView videoView = new GNSNativeVideoPlayerView(this);
    containerView.AddView (videoView);
    
    1
    2
  • Calling from layout

    GNSNativeVideoPlayerView videoPlayerView = findViewById(R.id.videoPlayerView);
    
    1
    <jp.co.geniee.sdk.ads.nativead.GNSNativeVideoPlayerView
      android:id="@+id/videoPlayerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
    
    1
    2
    3
    4
  1. Activity cooperation is done in order to shift screen by clicking.
    videoView.setActivity(this);
    
    1

This parameter sets the Context of the application.

  1. Lifecycle cooperation is carried out.

    @Override
    protected void onResume() {
        super.onResume();
        if (videoView != null) {
            videoView.resume();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (videoView != null) {
            videoView.pause();
        }
    }
    
    @Override
    protected void onDestroy() {
        // Please describe below if you do not use videoView on other screen
        if (videoView != null) {
            videoView.remove();
        }
        super.onDestroy();
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
  2. Implement the GNSNativeVideoPlayerListener callback function, Receive video native player event results.

    GNSNativeVideoPlayerListener nativeVideoPlayerListener = new GNSNativeVideoPlayerListener() {
        // Sent when an video ad request succeeded.
        @Override
        public void onVideoReceiveSetting(GNSNativeVideoPlayerView videoView) {
            Log.i(TAG, "onVideoReceiveSetting videoView.isReady()=" + videoView.isReady());
            if (videoView.isReady()) {
                videoView.show();
                Log.i(TAG, "onVideoReceiveSetting videoView.show");
            }
        }
        // Sent when an video ad request failed.
        // (Network Connection Unavailable, Frequency capping, Out of ad stock)
        @Override
        public void onVideoFailWithError(GNSNativeVideoPlayerView videoView, GNSException e) {
            Log.w(TAG, "Err:" + e.getCode() + " " + e.getMessage()));
        }
        // When playback of video ad is started
        @Override
        public void onVideoStartPlaying(GNSNativeVideoPlayerView videoView) {
            Log.i(TAG, "Ad started playing.");
        }
        // When playback of video ad is completed
        @Override
        public void onVideoPlayComplete(GNSNativeVideoPlayerView videoView) {
            Log.i(TAG, "Ad finished playing.");
        }
    
        @Override
        public void onVideoClose(GNSNativeVideoPlayerView videoView) {
            // Not used in Native ads, but requires method implementation.
            Log.i(TAG,"Ad closed");
        }
     };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

To distribute multiple native ads, use the GNSNativeVideoPlayerView information of the first argument of load completion.

  1. Set the GNSNativeVideoPlayerListener event listener to VideoView.

    videoView.setListener(nativeVideoPlayerListener);
    
    1
  2. Set up GNNativeAd and make a video request.

  • When you generate GNSNativeVideoPlayerView fromGNNativeAd (generation example from 2 NativeAd), you can request it below.
    videoView.load();
    
    1
  • When creating a new GNSNativeVideoPlayerView (2 new generation example), setGNNativeAd as an argument and make a request for animation.
    videoView.load((GNNativeAd) ad);
    
    1
  1. If you can prepare the movie, it will judge the movie preparation completion and start the movie.

    @Override
    public void onVideoReceiveSetting(GNSNativeVideoPlayerView videoView) {
        // motion preparation ready judgment
        if (videoView.isReady()) {
            // Movie playback start
            videoView.show();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
  2. The following values can be acquired as options.

    // Release videoView
    videoView.remove();
    // Replay (Cue playback. If used, available after show).
    videoView.replay();
    // mute settings
    setMuted (true);
    // mute setting acquisition
    getMuted(); // true or false, defaul true
    // mute button display state set
    setVisibilityMuteButton (true);
    // mute button display state acquisition
    getVisibilityMuteButton(); // true or false, defaul true
    // progress bar display state set
    setVisibilityProgressbar (true);
    // progress bar display state acquisition
    getVisibilityProgressbar(); // true or false, default true
    // replay button display state setting
    setVisibilityReplayButton(true);
    // replay button display state acquisition
    getVisibilityReplayButton(); // true or false, default true
    // width acquisition of video
    int width = getMediaFileWidth(); // 1600
    // vertical width acquisition of video
    int height = getMediaFileHeight(); // 900
    // the aspect ratio of the video acquisition
    float aspect = getMediaFileAspect(); // 1.777 ..
    // total number of seconds of video acquisition
    float duration = getDuration(); // 30.000
    // of the current playback number of seconds acquisition
    float currentPosition = getCurrentPosition(); // 15.000
    // playing judgment
    isPlaying(); // true or false
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32

How to continue to play videos even when the terminal rotates

  • Add the android:configChanges="orientation|screenSize" attribute to the Activity of AndroidManifest as below.

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize">
    
    1
    2
    3
  • Add the following method to Activity.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (videoView != null) {
                    videoView.getLayoutParams().height = (int) (videoView.getWidth() / videoView.getMediaFileAspect());
                }
            }
        });
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

About stopping automatic playback of movies

As for movie native, the movie area stops playing according to the ratio of the terminal content display area as follows. Playback is stopped at the timing when the ratio changes due to scrolling or when switching to another application.

  • Movie playing condition ... When the moving picture area is 50% or more of the terminal contents display area
  • Movie stop condition ... When the moving image area is less than 50% of the terminal contents display area

About multiple videos

  • For movie display, we recommend one memory per screen due to memory load.
  • It is recommended to release unnecessary video instances.

Regulation of video advertisement

About video advertisement optout-url display Please be sure to go. If there are reasons such as not wanting to display due to the characteristics of the application Please consult Geniee representative.

Last Updated: 7/6/2020, 10:51:22 AM