. . .

Messaging Module


Using the messaging module you can send messages to other users. Each user has then an inbox where his income messages are stored.

images/download/attachments/61479574/db3-module-messaging.PNG

Configuration

Nothing to configure here.

Usage

The messaging module provides you the class message model, which covers a message. To send a message, simply set the message text and the receivers user name (an other users user name) and post the message. The code:

Android
String textOfTheMessage = "Hello world!";
String recipientUserName = "exampleUser";
MessageModel mm = new MessageModel( );
mm.setMessageText( textOfTheMessage );
mm.setReceiverUserName( recipientUserName );
mm.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
Objective-C
AOMMessageModel *mm = [[AOMMessageModel alloc] init];
[mm setMessageText:textOfTheMessage];
[mm setReceiverUserName:recipientUserUserName];
[mm saveAsyncWithBlock:^(NSError *error) {
}];
Swift
let mm = MessageModel()
mm.messageText = textOfTheMessage
mm.receiverUserName = recipientUserUserName
mm.save { (error) in
}
JavaScript
var textOfTheMessage = "Hello world!";
var recipientUserName = "exampleUser";
 
var mm= new Apiomat.MessageModel();
mm.setMessageText(textOfTheMessage);
mm.setReceiverUserName(recipientUserName);
 
var saveCB = {
onOk : function() {
console.log("saved");
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
mm.save(saveCB);
TypeScript
const textOfTheMessage = "Hello world!";
const recipientUserName = "exampleUser";
 
const mm = new MessageModel();
mm.messageText = textOfTheMessage;
mm.receiverUserName = recipientUserName;
await mm.save();

The user model for all your users is enriched with an inbox field, which is a list of message models containing all messages sent to this user. To fetch these messages just to the following:

Android
private void test() {
final User user = new User();
user.loadAsync(new AOMEmptyCallback() {
@Override
public void isDone(boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if (exception != null) {
// ...
processMessages(user);
}
}
});
}
 
private void processMessages(final User user) {
String query = "receiverUserName == \"" + user.getUserName() + "\"";
MessageModel.getMessageModelsAsync(query, new AOMCallback<List<MessageModel>>() {
@Override
public void isDone(List<MessageModel> resultObject, boolean wasLoadedFromStorage, ApiomatRequestException exception) {
if ( resultObject != null && exception != null ) {
for ( MessageModel m : resultObject ) {
System.out.println(m.getMessageText());
}
}
}
});
}
Objective-C
//To fetch these messages just to the following:
AOMUser *user = [[AOMUser alloc] init];
[user loadMeAsyncWithFinishingBlock:^(NSError *error) {
NSString query = [NSString stringWithFormat:@"receiverUserName == \"%@\"", [user userName] ];
[AOMMessageModel getAsyncWithQuery:query withBlock:^(NSMutableArray *messages, NSError *error) {
if(error == FALSE && messages != NULL)
{
for (AOMMessageModel *m in messages)
{
NSLog(@"%@", [m messageText]);
}
}
}];
}];
Swift
//To fetch these messages just to the following:
let user = User()
user.loadMe { (error) in
let query = "receiverUserName == \(user.userName)"
MessageModel.loadList(query: query, usePersistentStorage: false) { (models, error) in
ifs error == nil && messages != nil {
 
if let messages = models as? [MessageModel] {
for let m in messages {
 
 
print(m.messageText)
}
}
}
}
}
JavaScript
function test() {
var user= new Apiomat.User();
 
var loadCB = {
onOk : function() {
processMessages(user);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
user.load(loadCB);
}
 
function processMessages(user) {
var query="receiverUserName == \"" + user.getUserName() + "\"";
Apiomat.MessageModel.getMessageModels(query, {
onOk : function(messages) {
console.log(messages);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
});
}
TypeScript
// initialized User
await user.load();
const messages = await MessageModel.getMessageModels('receiverUserName == "' + user.getUserName() + '"');