. . .

Messaging Module

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


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


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

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

Objective-C
AOMMessageModel *mm = [[AOMMessageModel alloc] init];
[mm setMessageText:textOfTheMessage];
[mm setReceiverUserName:recipientUserUserName];
[mm saveObjCWithLoadAfterwards:true completion:^(NSError * error) {
}];

Swift

Swift
let mm = AOMMessageModel()
mm.messageText = textOfTheMessage
mm.receiverUserName = recipientUserUserName
mm.save { (error) in
}

JavaScript

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

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

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

Objective-C
//To fetch these messages just to the following:
AOMUser *user = [[AOMUser alloc] init];
[user loadMe:^(NSError * error) {
NSString query = [NSString stringWithFormat:@"receiverUserName == \"%@\"", [user userName] ];
[AOMMessageModel loadListObjCWithQuery:query completion:^(NSArray<AbstractClientDataModel *> * models, NSError * error) {
if(error == FALSE && messages != NULL)
{
for (AOMMessageModel *m in messages)
{
NSLog(@"%@", [m messageText]);
}
}
}];
}];

Swift

Swift
//To fetch these messages just to the following:
let user = AOMUser()
user.loadMe { (error) in
let query = "receiverUserName == \(user.userName)"
AOMMessageModel.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

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

TypeScript
// initialized User
await user.load();
const messages = await MessageModel.getMessageModels('receiverUserName == "' + user.getUserName() + '"');