Messages

Bit6 messaging capabilities are accessed via MessageClient.

MessageClient messageClient = bit6.getMessageClient();

Send Message

Address to = Address.parse("usr:john");
messageClient.compose(to).text("Hello!").send(statusListener);

To follow the message progress as it is being sent implement MessageStatusListener.

public void onMessageStatusChanged(Message m, int state) {
    switch(state) {
        case Message.STATUS_SENDING:
            break;
        case Message.STATUS_FAILED:
            break;
    }
}

Delete Message

To delete a message you need its _id value, usually retrived from a cursor.

long msgId = cursor.getLong(cursor.getColumnIndex(Contract.Messages._ID));
messageClient.deleteMessage(msgId, new ResultHandler() {
    public void onResult(boolean success, String msg) {}
});

Message Details

Message flags contain information about the message type and status:

int flags = cursor.getInt(cursor.getColumnIndex(Messages.FLAGS));
// Is this an incoming message?
boolean isIncoming = Message.isIncoming(flags)
// Get message type: Message.TYPE_TEXT, Message.TYPE_ATTACH etc
int type = Message.getType(flags);
// Get message status: Message.STATUS_SENT, Message.STATUS_DELIVERED
int status = Message.getStatus(flags);

Incoming Messages

To get notified about new incoming messages even when your app is not in the foreground:

Step 1. Register Broadcast Receiver for your.package.name.BIT6_INCOMING_MESSAGE intent in your Manifest.xml

<receiver android:name=".IncomingMessageReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="your.package.name.BIT6_INCOMING_MESSAGE"></action>
    </intent-filter>
</receiver>

Step 2. Create a receiver class which extends BroadcastReceiver.

public class IncomingMessageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras() != null){
            String content = intent.getStringExtra(Bit6.INTENT_EXTRA_CONTENT);
            String senderName = intent.getStringExtra(Bit6.INTENT_EXTRA_NAME);
        }
    }
}

Conversations

Get all conversations:

Cursor cursor = getContentResolver().query(
        Contract.Conversations.CONTENT_URI,
        null, null, null, null);

Get all conversations with last message content:

Cursor cursor = getContentResolver().query(
        Contract.Conversations.CONTENT_URI_WITH_LAST_MESSAGE,
        null, null, null, null);

Get all messages for a conversation - query by conversation _id:

Cursor cursor = getContentResolver().query(
        Contract.Messages.CONTENT_URI, null,
        Contract.Messages.CONVERSATION_ID + "=?", new String[] {
            c_id
        },
        Contract.Messages.CREATED + " ASC");

Attachments

Send Photo or Video

When creating an outgoing message, specify the content type and the path to the file to send.

Address to = Address.parse("usr:john");
messageClient.compose(to).text("Yo").attach("/path/to/file.mp4", "video/mp4").send(statusListener);

For these messages you get additional status updates via MessageStatusListener:

public void onMessageStatusChanged(Message m, int state) {
    switch(state) {
        case Message.STATUS_PREPARING:
            break;
        case Message.STATUS_UPLOADED:
            break;
        case Message.STATUS_SENDING:
            break;
        case Message.STATUS_FAILED:
            break;
    }
}

Attachment URI

If message type is Contract.Messages.TYPE_ATTACH you can retrieve the attachment information from message's data.

String dataStr = cursor.getString(cursor.getColumnIndex(Contract.Messages.DATA));
Message.Data data = Message.getData(dataStr, flags);
if (data.isAttachment()) {
    Message.Attachment attach = (Message.Attachment)data;
    // Attachment content type
    String contentType = attach.getContentType();
    // URI to access the attachment data
    String uri = attach.getUri();
}

Thumbnail Image

Some messages (photo, video, location) may have a thumbnail image. Note that you need to handle the image download and caching in your app.

String uri = cursor.getString(cursor.getColumnIndex(Contract.Messages.THUMB_URI));

Location

To send a location message with text, latitude and longitude:

Address to = Address.parse("usr:john");
messageClient.compose(to).geo(40.192324, 44.504161).send(statusListener);

If message type is Contract.Messages.TYPE_GEOLOC you can retrieve the geo location information from the message data.

String dataStr = cursor.getString(cursor.getColumnIndex(Contract.Messages.DATA));
Message.Data data = Message.getData(dataStr, flags);
if (data.isGeoLocation()) {
    Message.GeoLocation geo = (Message.GeoLocation)data;
    // URI to view location in a map application
    String uri = geo.getMapUri();
    // Get the location
    double lat = geo.getLatitude();
    double lng = geo.getLongitude();
}

Notifications

Bit6 notification capabilities are accessed via NotificationClient

NotificationClient notificationClient = bit6.getNotificationClient();

Get notified when connection is established or closed, another user is typing a message, or on any other real-time notification:

notificationClient.addListener(new NotificationClient.Listener() {
    public void onTypingReceived(String from) {
        Log.v(TAG, "Typing from: " + from);
    }
    public void onNotificationReceived(String from, String type, JSONObject data) {
        Log.e(TAG, "Notification: " + data.toString());
    }
    public void onConnectedChanged(boolean isConnected) {
        Log.e(TAG, "RT is connected " + isConnected);
    }
});

Typing

Send 'typing' notification:

Address to = Address.parse("usr:john");
notificationClient.sendTypingNotification(to);

Custom

Send custom notification:

Address to = Address.parse("usr:john");
notificationClient.sendNotification(to, "mytype");

Groups

A Group contains a set of properties (group id, meta information, permissions) and has members. A user can be a member of multiple groups.

Management

Get all groups that this user is a member of:

Cursor cursor = getContentResolver().query(Contract.Groups.CONTENT_URI,null, null, null, null);

Create a new group. The current user will become the group administrator.

// groupId, permissions and meta are optional and can be null
bit6.createGroup(groupId, permissions, meta, handler);

Update a group:

bit6.updateGroup(groupId, permissions, meta, handler);

Delete a group:

bit6.deleteGroup(groupId, handler);

Members

Join a group:

// role is one of constants from Group.Permissions class, e.g Group.Permissions.ROLE_USER
bit6.joinGroup(groupId, role, handler);

Leave a group:

bit6.leaveGroup(groupId, handler);

Invite a member to a group:

bit6.inviteGroupMember(groupId, identity, role, handler);

Kick a member from a group:

bit6.kickGroupMember(groupId, identity, handler);