-
Notifications
You must be signed in to change notification settings - Fork 7
ネイティブ広告実装手順
まだ広告枠作成、SDKダウンロードを行っていない場合、下記リンク先を参照してください。
プロジェクトにSDKを追加していない場合、以下の方法でSDKを追加してください。
ネイティブ広告の実装は以下の3ステップで完了します。
また、下記二つの機能をオプションで設定できます。
アプリ側で広告を表示するためのViewをレイアウトXMLで用意します。
広告を構成する各Viewは、ImageView
、TextView
を使用してください。
要素 | 使用するView |
---|---|
広告画像 | ImageView |
ロゴ画像 | ImageView |
広告明示 | TextView |
広告見出し | TextView |
広告文 | TextView |
プロモーション名 | TextView |
表示URL | TextView |
アクションボタン | TextView |
以下にレイアウトの例を示します。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/ad_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"/>
<ImageView
android:id="@+id/ad_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/ad_title"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/ad_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ad_title"
android:layout_toRightOf="@+id/ad_image"/>
<TextView
android:id="@+id/ad_pr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FFC107"
android:layout_below="@+id/ad_image"/>
<TextView
android:id="@+id/ad_promotion_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ad_image"
android:layout_toRightOf="@+id/ad_pr"
android:layout_toLeftOf="@+id/ad_action"/>
<TextView
android:id="@+id/ad_promotion_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/ad_promotion_name"
android:layout_toRightOf="@+id/ad_pr"
android:layout_toLeftOf="@+id/ad_action"/>
<TextView
android:id="@+id/ad_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@android:color/white"
android:layout_below="@+id/ad_image"
android:background="#3b5998"/>
</RelativeLayout>
NendAdNativeViewBinder
を使用し、上記で作成したレイアウトの各要素に定義した android:id を指定することで関連付けを行います。
広告明示には、第2引数に表示する文言を指定します。
Kotlin
val binder = NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image) // 広告画像
.logoImageId(R.id.logo_image) // ロゴ画像
.titleId(R.id.ad_title) // 広告見出し
.contentId(R.id.ad_content) // 広告文
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.PR) // 広告明示
.promotionUrlId(R.id.ad_promotion_url) // 表示URL
.promotionNameId(R.id.ad_promotion_name) // プロモーション名
.actionId(R.id.ad_action) // アクションボタン
.build()
Java
NendAdNativeViewBinder binder = new NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image) // 広告画像
.logoImageId(R.id.logo_image) // ロゴ画像
.titleId(R.id.ad_title) // 広告見出し
.contentId(R.id.ad_content) // 広告文
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.PR) // 広告明示
.promotionUrlId(R.id.ad_promotion_url) // 表示URL
.promotionNameId(R.id.ad_promotion_name) // プロモーション名
.actionId(R.id.ad_action) // アクションボタン
.build();
定数 | 文言 |
---|---|
NendAdNative.AdvertisingExplicitly.PR | PR |
NendAdNative.AdvertisingExplicitly.SPONSORED | Sponsored |
NendAdNative.AdvertisingExplicitly.AD | 広告 |
NendAdNative.AdvertisingExplicitly.PROMOTION | プロモーション |
- 「広告明示」は必ず表示させる必要があります
- 広告見出しと広告文、プロモーション名のうちいずれか一つを表示させる必要があります(※1)
要素 | メソッド | 備考 |
---|---|---|
広告明示 | .prId | 必須 |
広告見出し | .titleId | ※1 |
広告文 | .contentId | ※1 |
プロモーション名 | .promotionNameId | ※1 |
表示URL | .promotionUrlId | - |
アクションボタン | .actionId | - |
- 広告画像を含む広告枠を選択された場合は、広告画像を必ず表示させる必要があります(※2)
- 画像は下記の制限内で表示させる必要があります
要素 | 拡縮 | 切り抜き | メソッド | 備考 |
---|---|---|---|---|
広告画像 | 30%~150% | 上下左右6%ずつまで | .adImageId | ※2 |
ロゴ画像 | 制限なし | 禁止 | .logoImageId | - |
拡縮を行う場合は縦横の比率が同じである必要があります。
まず、NendAdNativeClient
クラスのインスタンスを生成します。
Kotlin
import net.nend.android.NendAdNativeClient
...
val client = NendAdNativeClient(context, spotId, "apiKey")
Java
import net.nend.android.NendAdNativeClient;
...
NendAdNativeClient client = new NendAdNativeClient(context, spotId, "apiKey");
インスタンス生成時に必要な情報は以下の通りです。
引数名 | 型 | 説明 |
---|---|---|
context | Context | |
spotId | int | 管理画面より発行されたSpotId |
apiKey | String | 管理画面より発行されたApiKey |
続いて、NendAdNativeClient
クラスの loadAd
メソッドで広告のロードを行います。
広告のロードに成功すると引数のCallback関数の onSuccess
で広告オブジェクト NendAdNative
が取得できます。
広告のロードに失敗すると引数のCallback関数の onFailure
でエラー内容が取得できます。
Kotlin
val client = NendAdNativeClient(context, spotId, "apiKey")
client.loadAd(object : NendAdNativeClient.Callback {
override fun onSuccess(nendAdNative: NendAdNative) {
}
override fun onFailure(nendError: NendAdNativeClient.NendError) {
Log.i(TAG, "広告取得失敗: ${nendError.message}")
}
})
Java
NendAdNativeClient client = new NendAdNativeClient(context, spotId, "apiKey");
client.loadAd(new NendAdNativeClient.Callback() {
@Override
public void onSuccess(NendAdNative nendAdNative) {
}
@Override
public void onFailure(NendAdNativeClient.NendError nendError) {
Log.i(TAG, "広告取得失敗 " + nendError.getMessage());
}
});
ロード失敗時に NendAdNativeClient.NendError
に設定されるエラーコードは以下の通りです。
コード | 内容 | 備考 |
---|---|---|
FAILED_AD_REQUEST | リクエスト失敗 | インターネット未接続等 |
INVALID_RESPONSE_TYPE | レスポンスタイプ不正 | SDK内部のエラー (発生は稀です) |
onSuccess
で取得した NendAdNative
オブジェクトの intoView
メソッドを使用して、広告を描画することができます。
引数は表示するViewによって異なります。
nendAdNative.intoView(View view, NendAdNativeViewBinder binder)
nendAdNative.intoView(NendAdNativeViewHolder viewHolder)
ViewGroup
の場合
広告情報をレイアウトしたViewGroup
と NendAdNativeViewBinder
を使用します。
Kotlin
val binder = NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image)
.logoImageId(R.id.logo_image)
.titleId(R.id.ad_title)
.contentId(R.id.ad_content)
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.PR)
.promotionUrlId(R.id.ad_promotion_url)
.promotionNameId(R.id.ad_promotion_name)
.actionId(R.id.ad_action)
.build()
val adLayout = findViewById(R.id.ad)
val client = NendAdNativeClient(context, spotId, "apiKey")
client.loadAd(object : NendAdNativeClient.Callback {
override fun onSuccess(nendAdNative: NendAdNative) {
nendAdNative.intoView(adLayout, binder)
}
override fun onFailure(nendError: NendAdNativeClient.NendError) {
Log.i(TAG, "広告取得失敗: ${nendError.message}")
}
})
Java
NendAdNativeViewBinder binder = new NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image)
.logoImageId(R.id.logo_image)
.titleId(R.id.ad_title)
.contentId(R.id.ad_content)
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.PR)
.promotionUrlId(R.id.ad_promotion_url)
.promotionNameId(R.id.ad_promotion_name)
.actionId(R.id.ad_action)
.build();
RelativeLayout adLayout = findViewById(R.id.ad);
NendAdNativeClient client = new NendAdNativeClient(context, spotId, "apiKey");
client.loadAd(new NendAdNativeClient.Callback() {
@Override
public void onSuccess(NendAdNative nendAdNative) {
nendAdNative.intoView(adLayout, binder);
}
@Override
public void onFailure(NendAdNativeClient.NendError nendError) {
Log.i(TAG, "広告取得失敗 " + nendError.getMessage());
}
});
NendAdNativeViewHolder
を使用します。
広告情報をレイアウトしたViewから、NendAdNativeViewBinder
の createViewHolder
メソッドで NendAdNativeViewHolder
を生成します。
Kotlin
class NativeListAdapter(context: Context, resource: Int, list: List<String>) : ArrayAdapter<String>(context, resource, list) {
private val client = NendAdNativeClient(context, spotId, "apiKey")
private val binder = NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image)
.titleId(R.id.ad_title)
.promotionNameId(R.id.ad_promotion_name)
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.SPONSORED)
.build()
// 表示したポジションと広告を紐付けて保持
private val loadedAd = mutableMapOf<Int, NendAdNative>()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View
val holder: ViewHolder
val adHolder: NendAdNativeViewHolder
when (getItemViewType(position)) {
AD -> {
if (loadedAd.containsKey(position)) {
// ポジションに紐づくロード済み広告がある場合
adHolder = convertView?.tag as NendAdNativeViewHolder
loadedAd[position]?.intoView(adHolder)
view = convertView
} else {
// ポジションに紐づくロード済み広告がない場合は新しくロードする
view = LayoutInflater.from(context).inflate(R.layout.native_ad_left_row, parent, false)
adHolder = binder.createViewHolder(view)
view.tag = adHolder
client.loadAd(object : NendAdNativeClient.Callback {
override fun onSuccess(nendAdNative: NendAdNative) {
Log.i(TAG, "広告取得成功")
loadedAd[position] = nendAdNative
loadedAd[position]?.intoView(adHolder)
}
override fun onFailure(nendError: NendAdNativeClient.NendError) {
Log.i(TAG, "広告取得失敗: ${nendError.message}")
}
})
}
}
else -> {
...
}
}
return view
}
...
}
Java
class NativeListAdapter extends ArrayAdapter<String> {
private NendAdNativeClient mClient;
private NendAdNativeViewBinder mBinder;
// ポジションとロード済み広告を紐付けて保持する
private HashMap<Integer, NendAdNative> mLoadedAd = new HashMap<>();
private Handler mHandler = new Handler();
public NativeListAdapter(Context context, int resource, List<String> list) {
super(context, resource, list);
mBinder = new NendAdNativeViewBinder.Builder()
.adImageId(R.id.ad_image)
.titleId(R.id.ad_title)
.promotionNameId(R.id.ad_promotion_name)
.prId(R.id.ad_pr, NendAdNative.AdvertisingExplicitly.SPONSORED)
.build();
mClient = new NendAdNativeClient(context, spotId, "apiKey");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final NendAdNativeViewHolder adHolder;
switch (getItemViewType(position)){
case NORMAL:
...
break;
case AD:
if (mLoadedAd.containsKey(position)) {
// ポジションに紐づくロード済み広告がある場合
adHolder = (NendAdNativeViewHolder) convertView.getTag();
mLoadedAd.get(position).intoView(adHolder);
} else {
// ポジションに紐づくロード済み広告がない場合は新しくロードする
convertView = LayoutInflater.from(getContext()).inflate(R.layout.native_ad_left_row, parent, false);
adHolder = mBinder.createViewHolder(convertView);
convertView.setTag(adHolder);
mClient.loadAd(new NendAdNativeClient.Callback() {
@Override
public void onSuccess(NendAdNative nendAdNative) {
mLoadedAd.put(position, nendAdNative);
mLoadedAd.get(position).intoView(adHolder);
}
@Override
public void onFailure(NendAdNativeClient.NendError nendError) {
Log.i(TAG, "広告取得失敗 " + nendError.getMessage());
}
});
}
break;
}
return convertView;
}
...
}
広告イベントをアプリ側で取得するには、広告オブジェクトの NendAdNative
に対し、setNendAdNativeListener
を使用してリスナを登録します。
Kotlin
nendAdNative.setNendAdNativeListener(object : NendAdNativeListener {
override fun onImpression(nendAdNative: NendAdNative) {
Log.i(TAG, "onImpression")
}
override fun onClickAd(nendAdNative: NendAdNative) {
Log.i(TAG, "onClickAd")
}
override fun onClickInformation(nendAdNative: NendAdNative) {
Log.i(TAG, "onClickInformation")
}
})
Java
mNendAdNative.setNendAdNativeListener(new NendAdNativeListener() {
@Override
public void onImpression(@NonNull NendAdNative nendAdNative) {
Log.i(TAG, "onImpression");
}
@Override
public void onClickAd(@NonNull NendAdNative nendAdNative) {
Log.i(TAG, "onClickAd");
}
@Override
public void onClickInformation(@NonNull NendAdNative nendAdNative) {
Log.i(TAG, "onClickInformation");
}
});
ver.3.2.0 より、ネイティブ広告の自動リロード機能が追加されました。
NendAdNativeClient
の enableAutoReload
メソッドを使用する事で、自動で新しい広告オブジェクトを取得する事ができます。
nendAdNativeClient.enableAutoReload(long intervalMillis, NendAdNativeClient.Callback Callback);
第一引数の long intervalMillis には自動リロードを行うインターバルをミリ秒で指定します。
(30000(30秒)未満の数値を指定する事はできません)
自動リロードに成功した場合、第二引数のCallback関数の onSuccess
で 広告オブジェクトの NendAdNative
が取得できます。
自動リロードに失敗した場合、Callback関数の onFailure
に NendError
が返されます。
※ 自動リロードは一度有効にすると繰り返し行われますので、onFailure
にエラー通知が来た場合や、アクティビティがバックグラウンドに回った際などの適切なタイミングで 無効 にする必要があります。
自動リロードを無効にするには NendAdNativeClient
の disableAutoReload
メソッドを使用します。
(実装例)
Kotlin
client.enableAutoReload(30000, object : NendAdNativeClient.Callback {
override fun onSuccess(nendAdNative: NendAdNative) {
// 新たに取得した nendAdNative をレイアウトにセットする処理などを記述します
}
override fun onFailure(nendError: NendAdNativeClient.NendError) {
Log.i(TAG, "enableAutoReload: ${nendError.message}")
// エラー発生時自動リロードを無効化
client.disableAutoReload()
}
})
...
// アクティビティがバックグラウンドに回った為自動リロードを無効化
override fun onPause() {
super.onPause()
client.disableAutoReload()
}
Java
mClient.enableAutoReload(30000, new NendAdNativeClient.Callback() {
@Override
public void onSuccess(NendAdNative nendAdNative) {
// 新たに取得した nendAdNative をレイアウトにセットする処理などを記述します
}
@Override
public void onFailure(NendAdNativeClient.NendError nendError) {
Log.i(TAG, "enableAutoReload:" + nendError);
// エラー発生時自動リロードを無効化
mClient.disableAutoReload();
}
});
...
// アクティビティがバックグラウンドに回った為自動リロードを無効化
@Override
protected void onPause() {
super.onPause();
mClient.disableAutoReload();
}
-
2023年9月1日より、SDK v6.0.0未満では動画広告の配信を停止いたします。新しいバージョンのご利用をご検討ください。
-
Google Play Consoleのデータセーフティに関して、nendSDKのデータ収集項目のページを追加しました。詳しくはこちらをご覧ください。
-
Android 13より、Google Advertising IDのアクセスのために権限の追加を強く推奨しています。詳しくはSDKの組み込み方法をご覧ください。
-
v9.0.0よりSDK内部の依存性及び位置情報機能の完全な削除を行いました。詳しくは更新履歴を御覧ください。
-
らくらくスマートフォンの
F-01L
にて、特定のOSバージョンでバナー広告利用時にアプリがクラッシュするという報告を一部のメディア様から頂いております。詳細はこちらをご確認ください。 -
Google Play Services r30 よりライブラリプロジェクトの提供がなくなり、今後はEclipseプロジェクトへのGoogle Play Servicesの導入が不可能となったため、nendSDKにおいてもEclispeでの導入・開発のサポートを終了させていただきました。
EclipseからAndroidStudioへの移行はこちらをご参照ください。 -
Googleのデベロッパーポリシーの更新に伴い、v3.1.0のSDKよりインタースティシャル広告のアプリ終了時表示機能を廃止させていただきました。
-
Effective September 1, 2023, we will stop serving video ads with SDK under v6.0.0. Please consider using a newer version.
-
For data safety section in Google Play Console, We added a page about data collection used on nendSDK. For details, please see here.
-
Since Android 13, we highly recommend add the permision to access Google Advertising ID. For details, please see About integrate the SDK.
-
In v9.0.0, a new dependency had been added to the sdk and all functions of collecting location data are completely deleted.
-
We have received reports from some media that a model of smartphone,
F-01L
crashes when using banner ads on a specific OS version. For details, please check here. -
nendSDK terminated the introduction and development support of Eclispe as Google Play Services (r30) no longer provides library project and impossible to install Google Play Services to the Eclispe project.
Please check the link below for how to transfer from Eclipse to AndroidStudio. -
With the update of Google Developer Policy, we terminated the function to show interstitial ads before the user closes the app from v3.1.0 SDK.
- バナー型広告
- アイコン型広告
- インタースティシャル広告
- ネイティブ広告
- フルボード広告
- 動画広告
- 動画ネイティブ広告
- Release Notes
- Preparation
- Supported Devices and IDE
- nendSDK Data Collection Items
- nendSDK Privacy Policy
- About Information Button
- Banner Ad
- Icon Ads
- Interstitial Ad
- Native Ad
- Fullscreen Ad
- Video Ad
- Native Video Ad