Xamarin end-to-end demonstration

Overview

The purpose of this demonstration is to experience an end-to-end flow:

  1. A sample application that is bundled with the PMF Xamarin client SDK is registered with the PMF Operations Console.
  2. A new or provided adapter is deployed to the PMF Operations Console.
  3. The application logic is changed to make a resource request.

End result:

  • Successfully pinging the PMF.

Prerequisites:

  • Xamarin Studio
  • Optional. Stand-alone PMF (download)

1. Starting the PMF

Make sure you have PMF Developer Kit, navigate to the server’s folder and run the command: ./run.sh in Mac and Linux or run.cmd in Windows.

2. Creating an application

In a browser window, open the PMF Operations Console by loading the URL: http://your-server-host:server-port/mfpconsole. If running locally, use: http://localhost:9080/mfpconsole. The username/password are admin/admin.

  1. Click the New button next to Applications
    • Select the Android platform
    • Enter worklightsample.android as the application identifier (depending on the application scaffold you will download in the next step)
    • Enter 1.0 as the version value
    • Click on Register application

    Register an application

3. Editing application logic

  • Create a Xamarin project.
  • Add the Xamarin SDK as mentioned in the Adding the SDK tutorial.
  • Add a property of type IWorklightClient in any class file as below.

     /// <summary>
     /// Gets or sets the worklight sample client.
     /// </summary>
     /// <value>The worklight client.</value>
     public static IWorklightClient WorklightClient {get; set;}
    
  • If you’re developing for iOS, paste the following code inside FinishedLaunching method of the AppDelegate.cs file:

     <ClassName>.WorklightClient = WorklightClient.CreateInstance();
    

    Replace <ClassName> with the name of your Class.

  • If you’re developing for Android, include the following line of code inside OnCreate method of the MainActivity.cs file:

     <ClassName>.WorklightClient = WorklightClient.CreateInstance(this);
    

    Replace <ClassName> with the name of your Class.

  • Define a method to obtain the access token and perform a resource request to the PMF Server as below.

      public async void ObtainToken()
             {
              try
                     {
    
                         IWorklightClient _newClient = App.WorklightClient;
                         WorklightAccessToken accessToken = await _newClient.AuthorizationManager.ObtainAccessToken("");
    
                         if (accessToken.Value != null && accessToken.Value != "")
                         {
                             System.Diagnostics.Debug.WriteLine("Received the following access token value: " + accessToken.Value);
                             StringBuilder uriBuilder = new StringBuilder().Append("/adapters/javaAdapter/resource/greet");
    
                             WorklightResourceRequest request = _newClient.ResourceRequest(new Uri(uriBuilder.ToString(), UriKind.Relative), "GET");
                             request.SetQueryParameter("name", "world");
                             WorklightResponse response = await request.Send();
    
                             System.Diagnostics.Debug.WriteLine("Success: " + response.ResponseText);
                         }
                     }
                     catch (Exception e)
                     {
                         System.Diagnostics.Debug.WriteLine("An error occurred: '{0}'", e);
                     }
                 }
             }
      }
    
  • Invoke ObtainToken method within a class constructor or on click of a button.

4. Deploy an adapter

Download this prepared .adapter artifact and deploy it from the PMF Operations Console using the Actions → Deploy adapter action.

Alternatively, click the New button next to Adapters.

  1. Select the Actions → Download sample option. Download the “Hello World” Java adapter sample.

    If Maven and PMF CLI are not installed, follow the on-screen Set up your development environment instructions.

  2. From a Command-line window, navigate to the adapter’s Maven project root folder and run the command:

    pmfdev adapter build
    
  3. When the build finishes, deploy it from the PMF Operations Console using the Actions → Deploy adapter action. The adapter can be found in the [adapter]/target folder.

    Deploy an adapter

5. Testing the application

  1. In Xamarin Studio, select the mfpclient.properties file and edit the protocol, host and port properties with the correct values for your PMF.
    • If using a local PMF, the values are typically http, localhost and 9080.
    • If using a remote PMF (on Cloud), the values are typically https, your-server-address and 443.
    • If using a Kubernetes cluster on Cloud Private and if the deployment is of type NodePort, the value of the port would typically be the NodePort exposed by the service in Kubernetes cluster.
  2. Press the Play button.


Results

  • Clicking the Ping Persistent Mobile Foundation Server button will display Connected to Persistent Mobile Foundation Server.
  • If the application was able to connect to the PMF, a resource request call using the deployed Java adapter will take place.

The adapter response is then printed in the Xamarin Studio Console.

Image of application that successfully called a resource from the PMF

Next steps

Learn more on using adapters in applications, and how to integrate additional services such as Push Notifications, using the PMF security framework and more:

Last modified on