Skip to main content

Implementing advert in Windows Phone app using Nokia NAX with CSharp Background Thread

== Introduction ==
One of the new ways of advertising in Windows Phone application is to use Nokia Ad Exchange (NAX), powered by [http://www.inner-active.com/ Inneractive]. If you live in country where you can’t use Microsoft Ad Exchange, you can easily use NAX to earn some money from your Windows Phone app.

NAX is a mobile in-app advertising exchange offering access to the top ad networks in the world. With one API and one partner, you'll get access to over 120 ad agencies and networks, and you only need PayPal account to get your money.

The key NAX features are:
* Optimization across 120+ ad networks
* Payment enabled in over 200 countries
* Manage your own ad campaigns to promote your app
* Powerful ad performance dashboard
* NAX is free for developers


This article shows how to implement location based NAX ad (Banner) in your Windows Phone 8 application. The instructions complement the [https://inneractive.jira.com/wiki/display/DevWiki/Windows+Phone+SDK+guidelines Windows Phone SDK guidelines] on Inneractive wiki.



== Step 1 – Register and download SDK ==
First what you need to do is to register on https://nax.nokia.com.
After that you need to download SDK from SDKs page for Windows Phone 8. Current version is 1.1.3 (14 February 2013).




I propose you to look at '''Ad Placement Strategy.html''' document in folder Documentation with suggestions where to put your advert.
The selected location influences how much will you earn from each ad.

== Step 2 – Put ad files into Windows Phone project ==
From the '''InneractiveAdSDK''' folder of the extracted SDK, copy files to the root of your Visual Studio project (root for simplicity only, you can put these files in a separate folder):
* Inneractive.Ad.dll
* InneractiveAdLocation.cs (use this file only if you want to use location based ad in your WP app)



and then in Visual Studio for your Project do '''Add/Existing Item''' and choose both of the files.

== Step 3 – Register NAX add dll file==
You need to register '''Inneractive.Ad.dll''' file. Use '''References/Add Reference''', click '''Browse''' button and find dll file from your Windows Phone solution.

After successful adding of dll file you need to get this reference:


Note: If you can't add dll file as reference with error "A reference to a higher version or incompatible assembly cannot be added to the project" you need to Unblock dll file.

You can Unblock file with '''Right-click''' the '''Inneractive.Ad.dll''' file and choose '''Properties'''. At the bottom of the window, under Security, click the '''Unblock''' button and click '''Apply'''

== Step 4 – Include capabilities ==
In order to NAX work in your app you need to activate some of the capabilities. From '''Properties/WMAppManifest.xaml''' file activate next check boxes in Capabilities section:

  1. ID_CAP_LOCATION
  2. ID_CAP_NETWORKING
  3. ID_CAP_WEBBROWSERCOMPONENT
  4. ID_CAP_PHONEDIALER
  5. ID_CAP_IDENTITY_DEVICE


== Step 5 – Display NAX ad in XAML ==
First, you need to add control in XAML where your ad will be positioned. We will use NAX control
<code xml>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="53"/>
    </Grid.RowDefinitions>

    <ListBox Grid.Row="0">
    </ListBox>
         
    <StackPanel Height="53" Name="nax_control" Grid.Row="1">
    </StackPanel>
</Grid>
</code>

List of supported ad sizes:
* 300 x 50
* 320 x 53
* 300 x 250 (Rectangle)
* 320 x 480 (Full Screen)

In this example we will use '''''320x53''''' pixel size on the bottom of the page using {{Icode|Grid}}.

The final result is:



== Step 6 – C# code for NAX with modification background thread ==
After adding control in XAML code we need to write some code using C#. First we need to add two namespace in page where we want to put our ad control, in this case '''MainPage.xaml.cs''' with:

Here is the C# code for the task

using Inneractive.Nokia.Ad;
using InneractiveAdLocation;
using Microsoft.Phone.Net.NetworkInformation; // is to check if Internet connection is available

public partial class MainPage : PhoneApplicationPage
{
    Dictionary<InneractiveAd.IaOptionalParams, string> optionalParams;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
//this makes the code run in the background and prevent the application from freezing
 Task.Run(() =>
                    {

                     
        if (DeviceNetworkInformation.IsNetworkAvailable)
        {
            // Watch location
            IaLocationClass iaLocation = new IaLocationClass();
            iaLocation.Done += new System.EventHandler<IaLocationEventArgs>(iaLocation_Done);
            iaLocation.StartWatchLocation();

            optionalParams = new Dictionary<InneractiveAd.IaOptionalParams, string>();
            optionalParams.Add(InneractiveAd.IaOptionalParams.Key_OptionalAdWidth, "320"); //ad width
            optionalParams.Add(InneractiveAd.IaOptionalParams.Key_OptionalAdHeight, "53"); //add height
        }

 Dispatcher.BeginInvoke(() =>
                        {
        //Show Add Banner. Remarks: pay attention to use Application Id from NAX
        //nax_control.Childred.Count()==0 => just to add one banner control on a page. Without this, code //would add as many banners as you navigate to page where banner is placed
        if (optionalParams != null && nax_control.Children.Count() == 0)
        {
            InneractiveAd iaBanner = new InneractiveAd("ApplicationId", InneractiveAd.IaAdType.IaAdType_Banner, 30, optionalParams);
            nax_control.Children.Add(iaBanner);
        }
  });
 });

    }

    void iaLocation_Done(object sender, IaLocationEventArgs e)
    {
        try
        {
            // Add location, if received
            if (e != null && e.location != null)
                optionalParams.Add(InneractiveAd.IaOptionalParams.Key_Gps_Coordinates, e.location);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Error: " + ex.ToString());
        }
    }
}


=== Step 7 – AppId generation ===

ApplicationId_NAX is generated from ''nax.nokia.com'' from Add App section.



You need to enter information including:
* Mobile platform
* Application name
* Category
* Does your app use location

After this you get generated Application Id for you application. This is crucial info to track your ad banner in your Windows Phone app in Nokia NAX dashboard.

Generated Application Id (AppID) that you need to use in your app (we call it ApplicationId_NAX in our app):



That is it!



I hope that you can earn some money using Nokia NAX ad.

== References ==

* [http://spasol.wordpress.com/2013/04/28/implementing-advert-in-windows-phone-app-using-nokia-nax-with-c/ Implementing advert in Windows Phone app using Nokia NAX with C#] (Spaso Lazarevic Blog - original source)
* [https://inneractive.jira.com/wiki/display/DevWiki/Windows+Phone+SDK+guidelines Windows Phone SDK guidelines] (Inneractive wiki)
*[https://www.developer.nokia.com/Distribute/NAX/ Nokia Ad Exchange]


Comments

Popular posts from this blog

Installing libssl1.0 on Ubuntu 22.04

Following the upgrade of distro from 20.04 to 22.04 the libssl packages got affected, this ended up causing my vpn client to fail. The client kept opening and closing immediately.  on futher investigation I found the issue as the libssl client. Attached is a screenshot of the message I was getting when I tries starting it from the terminal. Tring to install libsll1.0-dev from apt was generating the following error: Due to this installing the package was not viable via the regular way, I really didn't want to downgrade back to 20.04 as this could have meant a clean installation, and I had just come from doing that, as the upgrade wasn't successful, but I still needed to try out the new 22.04 features. Google been my best friend finally come through after a long search on how to install libssl1.0-dev. I ended up landing on this link  , here they gave some instruction on how to install libss1.0-dev. Below are the steps of installing libssl1.0-dev on ubuntu 22.04 for backward compa

Creating PDF in MVC 5 using ITextSharp

Creating PDF in MVC 5 using ITextSharp For a long time I have been looking for free and cheaper ways of creating pdf documents within MVC. I have tried all the other options and none was able to give me the standard of document I wanted. After long and tiresome nights of trying to figure out the solution I stumbled upon ItextSharp... This was my life changer. Maybe it might be for you too..  Process Download ItextSharp using the Nuget Package manager and if you prefer using command line arguments you an use: Install-Package iTextSharp full documentation to this can be retrieved here Create a class that will be used to write the data you created as a byte array. How this works can be found here  or here   public class BinaryResult : ActionResult { private byte[] _fileBinary; private string _contentType; private string _fileName; public BinaryResult(byte[] fileBinary, string contentType, string fileName) { _fileBinary = f

KREDOH | BUY AIRTIME FAST & EASY AT A DISCOUNT

What is kredoh : This is a mobile applications where one can easily and conveniently buy airtime.  kredoh can be accessed via the web  kredoh.com  or  kredoh.co.ke   or via google play store  kredoh android app Kredoh is a product of   Bigmachini Enterprises What are the advantages of using kredoh : No transaction charge. Free Free Free Save money when you purchase airtime for 100 and above. Discount Discount Discount Fast & Easy.  It takes one step to buy airtime as compared to more than 5 steps when other methods are used Automatic reversal on failure Buy for other networks from mpesa and still enjoy the discount Allows buying airtime using FULIZA . What discounts does kredoh offer:  100 - 999 1% discount  eg. you will be charged KES 99  but receive 100 worth of airtime 1000 - 4999 2% discount eg. you will be charged KES 980  but received 1000 worth of airtime 5000 and above 3%   discount  eg. you will be charged KES 4850 but receive 5000 worth  airtim