Messaging - LOMENT-SDK/sdk-cashew-android GitHub Wiki
Messaging
Now that we have our user created and logged in and the user has at least one cashew account associated with it, the next step is to start the message queues. All Cashew messages are delivered via AMQP message queues, messages cannot be sent or received until the message queues are started. To start the message queue, call start() passing in the user and account objects:
new MessagingManager().start(context, cnUser, account, new StartMessageQueuesCallback() {...}
Now that the message queues are started, the next step is to create a conversation. All cashew messages occur in the context of a conversation. The conversation happens with a specific recipient, so we pass in a recipient object to the createConversation() call:
new MessagingManager().createConversation(context, contact, new CreateConversationCallback() {...}
If a conversation has already been started thats ok, the function will just immediately return if a conversation is already created. After creating the conversation the last step is to send the message to the recipient. Before we can send a message we need to setup event handlers so we can tell if the message was sent successfully or not:
@Override
public void onSendMessageSuccess(CNMessage message){
//Handle message sent successfully
}
@Override
public void onSendMessageError(CNMessage message, CNError error){
//Handle message error
}
With the handlers in place, we can now send a message. Here we are passing in the conversation object, the message text, and any options we want to apply (priority, expiry, forward & backup restricted, read acknowledgement, schedule, etc.). This takes place in the context of the event handler for creating a conversation:
@Override
public void onCreateConversationSuccess(CNConversation conversation){
new MessagingManager().sendMessage(context,
conversation,
message,
fileUri,
controlOptions,
expirationOptions,
sendMessageCallback(),
msgType);
}
Note that we do not need to specify the recipient here. That information is already contained in the conversation object we created earlier and passed in as an argument to sendMessage().
Below call will provide the unread count for each conversation
CNConversation conversation = getItem(position);
conversation.getUnreadCount()
Priority Message
Priority level can be set to a message by choosing from the given options(Low, Medium, High, and Highest).
public void setPriority(int priority){
//set a priority level for the message
}
Expiry Message
An expiration time in minutes can be set to a message. The message will be deleted from the receiver's conversation after expiration time ends. For example, if expiry time is set to 5 mins, then the message will be deleted from the receiver's conversation after 5 mins from the time receiver receives the message.
public void setExpiry(int expiry){
//set a time duration for the message to expire
}
Forward & Backup Restriction Message
A message can be restricted without forwarding further by the receiver by setting Forward Restriction to the message.
public void setRestricted(int restricted){
//set forward restriction to a message
}
Read Acknowledgement Message
Receiver can be asked to acknowledge the received message by setting Read Acknowledgement request from sender side.
public void setMessageAck(int messageAck){
//set read acknowledgement to a message
}
Schedule Message
With the new update v2.3.2 we can now schedule a message for later date and time.
A date picker will be displayed when we select schedule message option. After choosing a date and time and tapping on done will add Schedule time to the current message.
If a conversation has already been started that's ok, the function will just immediately return if a conversation is already created. After creating the conversation the last step is to send the message to the recipient. Before we can send a message, we need to setup event handlers so that we can tell if the schedule message was sent successfully or not.
@Override
public void onSchduleMessageSuccess(CNMessage message){
//Handle message sent successfully
}
@Override
public void onScheduleMessageError(CNMessage message, CNError error){
//Handle message error
}
Adding Attachment(s) to a message
Attachment(s) like Pictures, Take Photo, Video, Audio and Documents can be added to a message. Single or multiple attachments can be selected and all the selected attachments will be attached to the current message.