Receiving Unfiltered Messages¶
Backendless Real-Time Messaging delivers messages to listeners added to a channel. A listener is a callback function which is invoked by Backendless to deliver a message to the client application. Depending on the API used to add a listener, it can receive either all messages published into the channel or a filtered subset of messages. The filtering options are explained in greater detail in the Receiving Filtered Messages section of this guide.
Receiving String Messages¶
Adding a listener to receive messages as string objects. Messages should be published as string objects):
Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( new AsyncCallback<String>()
{
@Override
public void handleResponse( String stringMessage )
{
Log.i( "MYAPP", "Received string message " + stringMessage );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "MYAPP", "Error processing a message " + fault );
}
} );
Receiving Dictionary/Map messages¶
Adding a listener to receive messages as dictionary/map objects. Messages can be published as JSON objects, complex types or disctionary/map objects:
Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( new AsyncCallback<HashMap>()
{
@Override
public void handleResponse( HashMap message )
{
Log.i( "received message as a hashmap " + message );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "Error processing a message " + fault );
}
}, HashMap.class );
Receiving custom type/class messages¶
Adding a listener to receive messages as instances of a custom type (the Person
class). Published messages must be instances of the class, map/dictionary objects or JSON objects with the same structure as the class:
Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( new AsyncCallback<Person>()
{
@Override
public void handleResponse( Person personObject )
{
Log.i( "received message as a custom class " + personObject );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "Error processing a message " + fault );
}
}, Person.class );
General signature¶
A general signature for receiving messages as instances of a custom type:
// channel object must be obtained via a subscription call, for example:
// Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( AsyncCallback<T>, Class<T> callback );
Receiving messages in Backendless internal format¶
Adding a listener to receive message in the Backendless internal format:
Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( new MessageInfoCallback()
{
@Override
public void handleResponse( PublishMessageInfo message )
{
Log.i( "MYAPP", "Published message - " + message.getMessage() );
Log.i( "MYAPP", "Publisher ID - " + message.getPublisherId() );
Log.i( "MYAPP", "Message headers - " + message.getHeaders() );
Log.i( "MYAPP", "Message subtopic " + message.getSubtopic() );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "MYAPP", "Error processing a message " + fault );
}
} );