Xamarin end-to-end demonstration
Overview
Release 9.0.3
The purpose of this demonstration is to experience an end-to-end flow:
- A sample application that is bundled with the PMF Xamarin client SDK is registered with the PMF Operations Console.
- A new or provided adapter is deployed to the PMF Operations Console.
- The application logic is changed to make a resource request.
- End result - Successfully pinging the PMF.
Prerequisites
- Xamarin Studio
- Optional. Stand-alone PMF (download)
Procedure
-
Start the PMF.
-
Make sure you have PMF Developer Kit, go to the server’s folder and run the following command:
Mac and Linux
./run.sh
Windows
run.cmd
-
-
Create an application.
In a browser window, open the PMF Operations Console by loading the following URL:
http://your-server-host:server-port/mfpconsole
.If running locally, use the following URL:
http://localhost:9080/mfpconsole.
The username/password are admin/admin.
- Click the New 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.
-
Edit 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:
csharp <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.
-
Deploy an adapter.
Click javaAdapter to download an sample adapter. Deploy it from the PMF Operations Console using the Actions → Deploy adapter action.
Alternatively, click the New button next to Adapters.
-
Select the Actions → Download sample option. Download the “Hello World” Java adapter sample.
Note: If Maven and PMF CLI are not installed, follow the on-screen Set up your development environment instructions.
-
From a Command-line window, navigate to the adapter’s Maven project root folder and run the command:
pmfdev adapter build
-
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.
-
-
Test the application.
-
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.
-
Press the Play button.
-
Results
Clicking the Ping Persistent Mobile Foundation Server button will display Connected to the 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.
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:
- Review the Adapters development tutorials
- Review the Authentication and security tutorials
- Review All Tutorials