Skip to content

Message Store

Olga Koroleva edited this page Mar 11, 2021 · 11 revisions

It is possible to supply an implementation of Message Store to the Mobile Messaging library during initialization. In this case the library will save all received Push messages to the supplied Message Store. Library can also be initialized without message store. In this case developer has to deal with messages himself.

Message Store Example Implementation

Implement MessageStore interface to have your messages stored there by Mobile Messaging library (there is also an example implementation of Message Store based on SQLite database, see SQLiteMessageStore).

class MyMessageStore : MessageStore {
    /**
     * Finds all stored messages
     *
     * @param context current context
     * @return all stored messages
     */
    override fun findAll(context: Context): List<Message> {
        return ArrayList(messages)
    }

    /**
     * Counts all stored messages
     *
     * @param context current context
     * @return all stored messages count
     */
    override fun countAll(context: Context): Long {
        return messages.size.toLong()
    }

    /**
     * Saves the messages in the store
     *
     * @param context current context
     */
    @Synchronized
    override fun save(context: Context, vararg message: Message) {
        for (m in findAll(context)) {
            if (m.messageId == message[0].messageId) {
                messages.remove(m)
            }
        }
        messages.add(message[0])
    }

    /**
     * Deletes all stored messages
     *
     * @param context current context
     */
    @Synchronized
    override fun deleteAll(context: Context) {
        messages.clear()
    }

    companion object {
        private val messages: MutableList<Message> = ArrayList()
    }
}
expand to see Java code

public class MyMessageStore implements MessageStore {

    private static List<Message> messages = new ArrayList<>();

    /**
     * Finds all stored messages
     *
     * @param context current context
     * @return all stored messages
     */
    @Override
    public List<Message> findAll(Context context) {
        return new ArrayList<>(messages);
    }

    /**
     * Counts all stored messages
     *
     * @param context current context
     * @return all stored messages count
     */
    @Override
    public long countAll(Context context) {
        return messages.size();
    }

    /**
     * Saves the messages in the store
     *
     * @param context current context
     */
    @Override
    public synchronized void save(Context context, Message message) {
        for (Message m : findAll(context)) {
            if (m.getMessageId().equals(message.getMessageId())) {
                messages.remove(m);
            }
        }
        messages.add(message);
    }

    /**
     * Deletes all stored messages
     * <br>
     * This is convenience method.
     * Mobile Messaging SDK will never delete messages from the store by itself.
     * @param context current context
     */
    @Override
    public synchronized void deleteAll(Context context) {
        messages.clear();
    }
}

Initializing Library with Message Store

Supply your Message Store implementation to the library during initialization.

val mobileMessaging = MobileMessaging.Builder(application)
                .withMessageStore(MyMessageStore::class.java)
                .build()
expand to see Java code

MobileMessaging mobileMessaging = new MobileMessaging.Builder(application)
                .withMessageStore(MyMessageStore.class)
                .build();

Sorting messages

Sorting is not done by default in SQLiteMessageStore and also not implied to be done in any custom message store.

At the same time Message object implements Comparable interface and messages are compared by the time they were sent. It means that sorting of messages by sent time can be done using Collections.sort() as easy as:

val messages: List<Message> = messageStore.findAll(context)
Collections.sort(messages)
expand to see Java code

List<Message> messages = messageStore.findAll(context);
Collections.sort(messages);
Clone this wiki locally