First steps. Hello World

Create a new Cross-Platform project, the following example was created as a Native Portable project.

Add XAbetooLibrary dll and dependencies automatically using nuget package or manually, as described in the Introduction article.

Once the package is installed let’s go with an example, It was created with a very basic UI just to provide information about the last message received:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using xabetooLibrary;

namespace abetooApp.Droid
{
 [Activity (Label = "abetooApp.Droid", MainLauncher = true, 
 Icon = "@drawable/icon")]
 public class MainActivity : Activity
 {
 protected override void OnCreate (Bundle bundle)
 {
 base.OnCreate (bundle);

 SetContentView(Resource.Layout.Main);

 // UI
 Button button = FindViewById<Button>(Resource.Id.sendMessagebutton);
 EditText fromText = FindViewById<EditText>(Resource.Id.frombox);
 EditText corridText = FindViewById<EditText>(Resource.Id.corridbox);
 EditText messageText = FindViewById<EditText>(Resource.Id.messagebox);

 // Abetoo connection
 String myChannel = "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#"+
 "b90a9186b9b61ead17d51251f689aea1779d5203";
 String title = "Abetoo Test";
 String description = "Abetoo test running";
 int heartBeat = 1;

 AbtooConnection abcn = AbtooDroidConnection.getService(this, myChannel, 
 heartBeat, Resource.Drawable.logo_black, title, description);

 // Abetoo Listener
 abcn.messageReceived = ((sender, e) =>
 {
 AbtooMessage message = e;
 RunOnUiThread(() =>
 {
 fromText.Text = message.from;
 corridText.Text = message.correlationid;
 messageText.Text = message.body;
 });
 });

 const String remoteChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 button.Click += ((sender, e) =>
 {
 abcn.sendMessage(remoteChannel, "Message sent from Android!");
 });

 // Launch connection
 abcn.connect();
 
 }
 }
}

(You can download the associated axml here)

Let’s check it out the code a little bit deeper.

First at all we have to define the connection parameters, there are 6 parameters. We have to introduce a valid channel id string, a heartbeat timeout in minutes, an int logo from resources, a title text and a description text.

// Abetoo connection
 String myChannel = "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#"+
 "b90a9186b9b61ead17d51251f689aea1779d5203";
 String title = "Abetoo Test";
 String description = "Abetoo test running";
 int heartBeat = 1;

 AbtooConnection abcn = AbtooDroidConnection.getService(this, myChannel, 
 heartBeat, Resource.Drawable.logo_black, title, description);
  • channel id define our local channel wich we are listening to.
  • heartbeat is used to monitor the channel health and to reestablish the connection if it was lost.
  • logo is used to present an icon within the notification bar.
  • title, to put a title near logo.
  • description, to introduce a brief description of the service.

We must take into account that the service is going to work as an Android Foreground Service, it means that we have to put some information about our service within the notification bar.

If we launch the app we get the following notification bar’s appearance.

Using a Foreground Service we get some advantages over a regular android service, the main advantage is related with the fact that we won’t need a google account to keep a service running and responsive to an incoming messages. By using regular service approach we have to rely on the Google Cloud Messaging Service  or the newer FireBase Cloud Messaging Service. Using a foreground service with sockets, you can establish your own heartbeat timeout, using lower heartbeat if you want, keeping your app responsive under low signal situation.


If you want to know more about this question, let’s check it out this forum.

GCM Heartbeat is sent with the following intervals:

  • Every 28 minutes on mobile connection, 2g/3g/4g;
  • Every 15 minutes on wifi;

You can see by your self introducing the following code *#*#8255#*#* 


Then we can establish a Listener to respond to an incoming messages.

 // Abetoo Listener
abcn.messageReceived = ((sender, e) =>
{
AbtooMessage message = e;
RunOnUiThread(() =>
{
fromText.Text = message.from;
corridText.Text = message.correlationid;
messageText.Text = message.body;
});
}); 

There is a button that allows you to send a message to a remote device.

const String remoteChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 button.Click += ((sender, e) =>
 {
 abcn.sendMessage(remoteChannel, "Message sent from Android!");
 });

… And lastly the launch connection line.

// Launch connection
 abcn.connect();

Whenever we receive a new message the listener is triggered and we see the channel id source, the correlation id and the body message in the screen.

Was this article helpful?

Related Articles

Leave A Comment?