[go: up one dir, main page]

Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

ネイティブ広告実装手順V2

fan-w-suzuki edited this page May 15, 2019 · 11 revisions

事前準備

まだ広告枠作成、SDKダウンロードを行っていない場合、下記リンク先を参照してください。

SDKの組み込み

プロジェクトにSDKを追加していない場合、以下の方法でSDKを追加してください。

ネイティブ広告実装手順

nendSDK ver3.2.0 からネイティブ広告のテキストや画像などの広告情報をアプリ側から個別で取得できるようになりました。
新しい機能を使った実装方法は以下の通りです。

  1. ネイティブ広告をロードする
  2. 広告オブジェクトから広告の各データを取得する
  3. 広告をアクティブにする

ネイティブ広告をロードする

まず、NendAdNativeClient クラスのインスタンスを生成します。
続いて NendAdNativeClient に用意された loadAd(NendAdNativeClient.Callback) メソッドを使用し広告オブジェクトを取得します。

広告オブジェクトの取得に成功した場合はCallback関数の onSuccess で広告オブジェクトの NendAdNative が取得できます。
広告オブジェクトの取得に失敗した場合はCallback関数の onFailureNendError が返されます。

Kotlin
// 広告クライアントのインスタンス生成
val client = NendAdNativeClient(this, spotId, "apiKey")
var nativeAd: NendAdNative? = null

...

// 広告オブジェクトのロード
client.loadAd(object : NendAdNativeClient.Callback {
    override fun onSuccess(nendAdNative: NendAdNative) {
        // この広告オブジェクトから各広告データが取得できます
        nativeAd = nendAdNative
    }

    override fun onFailure(nendError: NendAdNativeClient.NendError) {
        Log.d(TAG, "広告取得失敗: ${nendError.message}")
    }
})
Java
// 広告クライアントのインスタンス生成
NendAdNativeClient mClient = new NendAdNativeClient(context, spotId, "apiKey");
NendAdNative mNendAdNative;

...

// 広告オブジェクトのロード
mClient.loadAd(new NendAdNativeClient.Callback() {
    @Override
    public void onSuccess(NendAdNative nendAdNative) {
        // この広告オブジェクトから各広告データが取得できます
        mNendAdNative = nendAdNative;
    }

    @Override
    public void onFailure(NendAdNativeClient.NendError nendError) {
        Log.d(TAG, "広告取得失敗:" + nendError);
    }
});

インスタンス生成時に必要な情報は以下の通りです。

引数名 説明
context Context
spotId int 管理画面より発行されたSpotId
apiKey String 管理画面より発行されたApiKey

広告オブジェクトから広告の各データを取得する

NendAdNative オブジェクトから広告のテキストやBitmap画像などの各データを取得し表示させます。 なお、ネイティブ広告はこちらのガイドラインに沿って表示させてください。

  • 広告画像とロゴ画像
    downloadAdImage で広告画像、downloadLogoImage でロゴ画像のBitmapが取得可能です。
    メソッドが成功すると、Callback関数の onSuccessBitmap画像が取得できます。
    失敗した場合、Callback関数の onFailure にエラーメッセージが返されます。
    また、各画像が含まれていない広告タイプでこれらのメソッドを使用すると、Callback関数の onFailure にエラーメッセージが返されます。

  • 広告画像URLとロゴ画像URLの取得について
    getAdImageUrl , getLogoImageUrl を使用して画像のURLを取得できます。
    取得したURLを使用してアプリ側で画像をダウンロードする事が可能です。
    各画像が含まれていない広告タイプでこれらのメソッドを使用した場合は null が返されます。

取得項目 取得メソッド名
広告画像 downloadAdImage(NendAdNative.Callback()) Bitmap
広告画像URL getAdImageUrl() String
ロゴ画像 downloadLogoImage(NendAdNative.Callback()) Bitmap
ロゴ画像URL getLogoImageUrl() String
広告見出し getTitleText() String
広告文 getContentText() String
プロモーション名 getPromotionName() String
表示URL getPromotionUrl() String
アクションボタン getActionText() String
広告明示 NendAdNative.AdvertisingExplicitly.xxx.getText() String

広告明示 に関しては、NendAdNative.AdvertisingExplicitly.xxx.getText() で取得できます。
「xxx」部分は以下から選択できます。

定数 取得できるテキスト
NendAdNative.AdvertisingExplicitly.PR PR
NendAdNative.AdvertisingExplicitly.SPONSORED Sponsored
NendAdNative.AdvertisingExplicitly.AD 広告
NendAdNative.AdvertisingExplicitly.PROMOTION プロモーション

実装例

Kotlin
// レイアウトの取得
val prText = findViewById(R.id.pr_text)
val titleText = findViewById(R.id.title_text)
val contentText = findViewById(R.id.content_text)
val promotionText = findViewById(R.id.promotion_name)
val promoUrlText = findViewById(R.id.promotion_url)
val actionText = findViewById(R.id.action_text)
val adImage = findViewById(R.id.ad_image)

// 広告明示の取得
prText.text = NendAdNative.AdvertisingExplicitly.SPONSORED.text
// 各広告文の取得
titleText.text = nendAdNative.titleText
contentText.text = nendAdNative.contentText
promotionText.text = nendAdNative.promotionName
promoUrlText.text = nendAdNative.promotionUrl
actionText.text = nendAdNative.actionText

// 広告画像の取得
nendAdNative.downloadAdImage(object : NendAdNative.Callback {
    override fun onSuccess(bitmap: Bitmap) {
        adImage.setImageBitmap(bitmap)
    }

    override fun onFailure(error: Exception) {
        Log.d(TAG, "広告画像取得失敗: ${error.message}")
    }
})
Java
// レイアウトの取得
TextView prText = findViewById(R.id.pr_text);
TextView titleText = findViewById(R.id.title_text);
TextView contentText = findViewById(R.id.content_text);
TextView promotionText = findViewById(R.id.promo_name_text);
TextView promoUrlText = findViewById(R.id.promo_url_text);
TextView actionText = findViewById(R.id.action_text);
ImageView adImage = findViewById(R.id.ad_image);

// 広告明示の取得
prText.setText(NendAdNative.AdvertisingExplicitly.SPONSORED.getText());

// 各広告文の取得
titleText.setText(mNendAdNative.getTitleText());
contentText.setText(mNendAdNative.getContentText());
promotionText.setText(mNendAdNative.getPromotionName());
promoUrlText.setText(mNendAdNative.getPromotionUrl());
actionText.setText(mNendAdNative.getActionText());
// 広告画像の取得
mNendAdNative.downloadAdImage(new NendAdNative.Callback() {
    @Override
    public void onSuccess(final Bitmap bitmap) {
        adImage.setImageBitmap(bitmap);
    }
    @Override
    public void onFailure(Exception error) {
        Log.d(TAG, "広告画像取得失敗: " + error);
    }
});

広告をアクティブにする

表示した広告をクリック可能にし、Impressionを送信するには、NendAdNative オブジェクトに用意された activate メソッドを使用します。

第一引数の adContainer には広告の各パーツを配置した親の View を渡します。
第二引数の prTextView には「広告明示」をセットした TextView を渡します。
これら二つの引数は 必須 となっています。

Kotlin
var nendAdNative: NendAdNative? = null
...

nendAdNative?.activate(adContainer, prTextView)
Java
NendAdNative mNendAdNative;
...

mNendAdNative.activate(adContainer, prTextView);

クリックイベントの通知

広告の自動リロード


検証

日本語

nendSDK Android について

SDKの組み込み

広告の表示

ログ出力

導入サポート


English

About nendSDK Android

SDK Implementation

Display Ads

Logs Output

Supports

Clone this wiki locally