. . .

Writing a simple Messenger App with ApiOmat

As you might have heard, ApiOmat provides a bunch of useful modules - packaged functionality which is ready to use. Today, I want to show how simple it is to use such a module in your app. In this example, we choose Andoid as language.

Creating the app in ApiOmat

First you have to login and wait until the dashboard has loaded. Create a new app and add the "Messaging" module from the Module Market.

images/download/attachments/12649153/messaging_moduleAdd.png

After the module is added to your app, you have to deploy it.

Now your app is up and running. Download the SDK for Android in the SDK screen.

Creating an Android App

At first, unzip the downloaded SDK to your src folder. com.apiomat.frontend.basics.MemberModel should be now accessible as Java class.

The messaging module enables users (members) of your app to send messages to each other, each message is stored in the recipients inbox. The app consists of four screens (or "activities" in the android world):

  • Account screen - where members can log in to the app

  • Inbox screen - the inbox / received messages of the member

  • Send screen - where the member can send a new message

  • Main screen - starting point of the app

The connection from the SDK to ApiOmat has to be initialized once to set up the credentials. You can do this in the Main screen during the onCreate method:

public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
Intent me = getIntent( );
// Try to get user credentials from saved bundle
Bundle b = me.getExtras( ) != null ? me.getExtras( ) : savedInstanceState;
//If no credentials were found, open account screen
if ( b == null || !b.containsKey( USERNAME ) ) {
Intent intent = new Intent( this, AccountActivity.class );
startActivity( intent );
}
else {
//Initialize SDK
Datastore.configure( MemberModel.baseURL, MemberModel.apiKey, b.getString( USERNAME ),b.getString( PASSWORD ) );
//Init your member
MemberModel m = new MemberModel( );
m.setUserName( b.getString( USERNAME ) );
m.setPassword( b.getString( PASSWORD ) );
//Load or create our member on server via a async task
CreateOrLoadMemberTask task = new CreateOrLoadMemberTask( );
task.execute( m );
...
setContentView( R.layout.main );
}

Now that the SDK knows which URL and credentials to use, we can try to load our member from server (if it already exists) or create a new one. Since requesting tasks are done asynchronously in Android, we have the CreateOrLoadMemberTask.doInBackground method for that:

protected MemberModel doInBackground( MemberModel... m ) {
MemberModel member = m[ 0 ];
//try to load member
member.loadMe( );
if ( member.getHref( ) == null ){
//OK, does not exist, create a new one
member.save( );
}
return member;
}

Sending or receiving messages itself is very easy, just create a new thread for the request (requests in main thread raise an exception, as you might know) and call the models methods, like for sending:

MessageModel mm = new MessageModel( );
mm.setMessageText( messageText.toString( ) );
mm.setReceiverUserName( recipient.toString( ) );
mm.save();

The app is done.

Further documentation on the Messaging module can be found in our wiki.

Attention: If you use the code without unzipping your own SDK into it, you have to change the baseURL and apiKey in the MemberModel class to your own values. These can be found in the SDK or in the deploy screen.

* link only available in Enterprise Documentation