{"_id":"58c6ba11c7f1550f005939a5","project":"574ff4bd3fa1870e00889ba6","user":"5770dd042659e20e00c90a98","version":{"_id":"574ff4bd3fa1870e00889ba9","project":"574ff4bd3fa1870e00889ba6","__v":21,"createdAt":"2016-06-02T08:56:29.492Z","releaseDate":"2016-06-02T08:56:29.492Z","categories":["574ff4bd3fa1870e00889baa","577278ecdceb570e003a9001","577279865617b117009e643f","577279985617b117009e6440","577279bc8c48e00e00503504","5774fe18605b770e0037be8a","577ce3ad87acf617003c4187","577f8476a77c700e006a6c52","57b486ae0d2b640e00e9d3f5","57b722133d3e620e004ec74b","57bd95f00fe3a00e003e2cc5","57c8349359cd4b0e00b888ef","57c8349b5754fa1700b12242","57cedb0bad483e0e00890239","57cedbe807d7ea0e00e438cc","57d82967156ef72b007ffcd8","58ee353ad1ee2f0f0034a13d","596623221738df00298622a5","59a51730192dba000fc9ca38","59a8129f1e7b26000fa0fb1a","5a0322bf044b6f001c236e36"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.0.0","version":"3.0"},"parentDoc":null,"category":{"_id":"57b486ae0d2b640e00e9d3f5","__v":0,"project":"574ff4bd3fa1870e00889ba6","version":"574ff4bd3fa1870e00889ba9","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-08-17T15:45:50.062Z","from_sync":false,"order":7,"slug":"native-ads-mediation-layer","title":"Android SDK"},"__v":1,"updates":["59edb41d5c4027001098a3f7"],"next":{"pages":[],"description":""},"createdAt":"2017-03-13T15:26:09.183Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":7,"body":"[block:api-header]\n{\n  \"title\": \"Getting Started\"\n}\n[/block]\n* Before integrating native ads in your app, you’ll need to create an account on the PubNative Dashboard, and integrate the SDK into your project.\n* Add a native ad unit to your app in the PubNative Dashboard\n* Make sure you have added the ad network SDKs you wish to use to your app\n\nFor native ads, first you need to create a request, then fill it with your data, and, after sending, provide your ad with a callback for the ad response.\n[block:api-header]\n{\n  \"title\": \"Requesting Native Ads\"\n}\n[/block]\n##Initializing Request\nYou can request a native ad by using PNRequest\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"PNRequest request = new PNRequest();\\nrequest.start(<CONTEXT>,  \\\"<YOUR_APP_TOKEN_HERE>\\\", \\\"<YOUR_PLACEMENT_NAME_HERE>\\\", new PNRequest.Listener() {\\n\\n            :::at:::Override\\n            public void onPNRequestLoadFinish(PNRequest request, PNAdModel ad) {\\n                // Requested ad returned\\n            }\\n\\n            @Override\\n            public void onPNRequestLoadFail(PNRequest request, Exception exception) {\\n                // Request failed\\n            }\\n        });\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nYou can set up several data before starting the request by using the helper PNRequest methods. This is optional, but in the long term it will seriously improve your ad placement behavior.\n\n##Cache Resource\nBy default, resources are cached before serving ads.  However, you can set it to false as follows in order to use urls to download resources such as icons, banners, etc.  \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"request.setCacheResources(false);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Fill View\"\n}\n[/block]\nIn order to display a native ad on screen, you need to get ad content by calling public methods of PNAdModel as follows:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.getTitle();           // Ad's title\\n\\npubnativeAdModel.getDescription();     // Ad's description\\n\\npubnativeAdModel.getIcon();            // Ad's icon image view\\n\\npubnativeAdModel.getBanner();          // Ad's banner view\\n\\npubnativeAdModel.getStarRating();      // Rating of the ad. The rating range is 1-5.\\n\\npubnativeAdModel.getCallToAction();    // Ad's CTA text\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n##Get Advertising Disclosure View\nYou can get the advertising disclosure view (ad choices, sponsor label, etc.) by using getAdvertisingDisclosureView().\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.getContentInfoView(); \",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nThis will return a View that can be added into ad container.\n\n*Because advertising disclosure view depends on the network and there may be some networks that don’t have it, it is necessary to check for null before placement in any ViewGroup.*\n[block:html]\n{\n  \"html\": \"<div class=\\\"isa_info\\\">\\n  <strong><font color=\\\"7d0a80\\\">Note:</font></strong>  It's always necessary to show advertising disclosure view on your ads.\\n</div> \"\n}\n[/block]\n##Get the Ad's Image Bitmap\nBy default, caching is enabled for resources in PubNative Mediation-SDK, so you can get the images of the ad as a pre-cached bitmap, and, if requested, using getBanner and getIcon.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.getIcon()   // this will return bitmap image that can be set to <ICON_VIEW>.\\n\\npubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nIf you are disabling resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resource images of the ad as bitmap. You can use FetchListener of PNAdModel as follows:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.fetch(new PNAdModel.FetchListener() {\\n\\n    @Override\\n    public void onFetchFinished() {\\n       // here you can get bitmap for resources\\n       pubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.\\n       pubnativeAdModel.getIcon()   // this will return bitmap image that can be set to <ICON_VIEW>.\\n    }\\n});\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Track\"\n}\n[/block]\nOnce you have your ad rendered, you should make the model start tracking your view so we can confirm the impression and handle the clicks, this can be done with the following method from the PNAdModel\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nYou can also start tracking and filling content together by using:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.withTitle(<TITlE_VIEW>) \\n  .withDescription(<DESCRIPTION_VIEW>)\\n  .withIcon(<ICON_VIEW>)\\n  .withBanner(<BANNER_VIEW>)\\n  .withRating(<RATING_VIEW>)\\n  .withCallToAction(<CALL_TO_ACTION_VIEW>) //CALL_TO_ACTION_VIEW view can be TextView or Button view\\n  .withContentInfoContainer(<ADVERTISING_DISCLOSURE_VIEW>);\\n  .startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nIf your ad is removed from the screen or you're exiting the activity, you'll need to stop the tracking process with the following method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.stopTracking()\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nYou can also set up a listener on the model to listen for callbacks on the tracking process.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.setListener(new PNAdModel.Listener() {\\n\\n    @Override\\n    public void onPNAdImpression(PNAdModel model) {\\n        // Called when the ad impression was confirmed\\n    }\\n\\n    @Override\\n    public void onPNAdClick(PNAdModel model) {\\n        // Called when the ad was clicked\\n    }\\n});\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n##Other\nIf you disable resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resources as follows:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pubnativeAdModel.fetch(new PubnativeAdModel.FetchListener() {\\n\\n    @Override\\n    public void onFetchFinished() {\\n       // here you can get bitmap for resources\\n       pubnativeAdModel.getBanner() //this will return bitmap image that can be set to <BANNER_VIEW>.\\n       pubnativeAdModel.getIcon()   //this will return bitmap image that can be set to <ICON_VIEW>.\\n    }\\n});\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"ad-layouts","type":"basic","title":"Native Ads"}
[block:api-header] { "title": "Getting Started" } [/block] * Before integrating native ads in your app, you’ll need to create an account on the PubNative Dashboard, and integrate the SDK into your project. * Add a native ad unit to your app in the PubNative Dashboard * Make sure you have added the ad network SDKs you wish to use to your app For native ads, first you need to create a request, then fill it with your data, and, after sending, provide your ad with a callback for the ad response. [block:api-header] { "title": "Requesting Native Ads" } [/block] ##Initializing Request You can request a native ad by using PNRequest [block:code] { "codes": [ { "code": "PNRequest request = new PNRequest();\nrequest.start(<CONTEXT>, \"<YOUR_APP_TOKEN_HERE>\", \"<YOUR_PLACEMENT_NAME_HERE>\", new PNRequest.Listener() {\n\n @Override\n public void onPNRequestLoadFinish(PNRequest request, PNAdModel ad) {\n // Requested ad returned\n }\n\n @Override\n public void onPNRequestLoadFail(PNRequest request, Exception exception) {\n // Request failed\n }\n });", "language": "java" } ] } [/block] You can set up several data before starting the request by using the helper PNRequest methods. This is optional, but in the long term it will seriously improve your ad placement behavior. ##Cache Resource By default, resources are cached before serving ads. However, you can set it to false as follows in order to use urls to download resources such as icons, banners, etc. [block:code] { "codes": [ { "code": "request.setCacheResources(false);", "language": "java" } ] } [/block] [block:api-header] { "title": "Fill View" } [/block] In order to display a native ad on screen, you need to get ad content by calling public methods of PNAdModel as follows: [block:code] { "codes": [ { "code": "pubnativeAdModel.getTitle(); // Ad's title\n\npubnativeAdModel.getDescription(); // Ad's description\n\npubnativeAdModel.getIcon(); // Ad's icon image view\n\npubnativeAdModel.getBanner(); // Ad's banner view\n\npubnativeAdModel.getStarRating(); // Rating of the ad. The rating range is 1-5.\n\npubnativeAdModel.getCallToAction(); // Ad's CTA text", "language": "java" } ] } [/block] ##Get Advertising Disclosure View You can get the advertising disclosure view (ad choices, sponsor label, etc.) by using getAdvertisingDisclosureView(). [block:code] { "codes": [ { "code": "pubnativeAdModel.getContentInfoView(); ", "language": "text" } ] } [/block] This will return a View that can be added into ad container. *Because advertising disclosure view depends on the network and there may be some networks that don’t have it, it is necessary to check for null before placement in any ViewGroup.* [block:html] { "html": "<div class=\"isa_info\">\n <strong><font color=\"7d0a80\">Note:</font></strong> It's always necessary to show advertising disclosure view on your ads.\n</div> " } [/block] ##Get the Ad's Image Bitmap By default, caching is enabled for resources in PubNative Mediation-SDK, so you can get the images of the ad as a pre-cached bitmap, and, if requested, using getBanner and getIcon. [block:code] { "codes": [ { "code": "pubnativeAdModel.getIcon() // this will return bitmap image that can be set to <ICON_VIEW>.\n\npubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.", "language": "java" } ] } [/block] If you are disabling resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resource images of the ad as bitmap. You can use FetchListener of PNAdModel as follows: [block:code] { "codes": [ { "code": "pubnativeAdModel.fetch(new PNAdModel.FetchListener() {\n\n @Override\n public void onFetchFinished() {\n // here you can get bitmap for resources\n pubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.\n pubnativeAdModel.getIcon() // this will return bitmap image that can be set to <ICON_VIEW>.\n }\n});", "language": "java" } ] } [/block] [block:api-header] { "title": "Track" } [/block] Once you have your ad rendered, you should make the model start tracking your view so we can confirm the impression and handle the clicks, this can be done with the following method from the PNAdModel [block:code] { "codes": [ { "code": "pubnativeAdModel.startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);", "language": "java" } ] } [/block] You can also start tracking and filling content together by using: [block:code] { "codes": [ { "code": "pubnativeAdModel.withTitle(<TITlE_VIEW>) \n .withDescription(<DESCRIPTION_VIEW>)\n .withIcon(<ICON_VIEW>)\n .withBanner(<BANNER_VIEW>)\n .withRating(<RATING_VIEW>)\n .withCallToAction(<CALL_TO_ACTION_VIEW>) //CALL_TO_ACTION_VIEW view can be TextView or Button view\n .withContentInfoContainer(<ADVERTISING_DISCLOSURE_VIEW>);\n .startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);", "language": "java" } ] } [/block] If your ad is removed from the screen or you're exiting the activity, you'll need to stop the tracking process with the following method: [block:code] { "codes": [ { "code": "pubnativeAdModel.stopTracking()", "language": "java" } ] } [/block] You can also set up a listener on the model to listen for callbacks on the tracking process. [block:code] { "codes": [ { "code": "pubnativeAdModel.setListener(new PNAdModel.Listener() {\n\n @Override\n public void onPNAdImpression(PNAdModel model) {\n // Called when the ad impression was confirmed\n }\n\n @Override\n public void onPNAdClick(PNAdModel model) {\n // Called when the ad was clicked\n }\n});", "language": "java" } ] } [/block] ##Other If you disable resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resources as follows: [block:code] { "codes": [ { "code": "pubnativeAdModel.fetch(new PubnativeAdModel.FetchListener() {\n\n @Override\n public void onFetchFinished() {\n // here you can get bitmap for resources\n pubnativeAdModel.getBanner() //this will return bitmap image that can be set to <BANNER_VIEW>.\n pubnativeAdModel.getIcon() //this will return bitmap image that can be set to <ICON_VIEW>.\n }\n});", "language": "java" } ] } [/block]