Search code examples
titaniummessageappceleratortitanium-mobile

Display the last message in Message Threads : Titanium.Cloud.Messages


I'm creating an application that sends message within the app users using Titanium Framework. In the app, I've to display the conversation between the users(message threads). I'm taking the threads as given in the following code

var tableView = Ti.UI.createTableView({
   top          : '10%',
   scrollable  : true,
   width        : '100%',
   minRowHeight: '50',
   bottom       : '10%'
});

/*getting the message threads and adding them to the tableview
  and displaying it while opening the window*/

win1.addEventListener('open', function(){
   tableData = [];
   Cloud.Messages.showThreads(function (e) {
       if (e.success) {
           Ti.API.info('Success: ' +'Count: ' + e.messages.length);
           for (var i = 0; i < e.messages.length; i++) {
               var message = e.messages[i];
               //alert(JSON.stringify(message));
               var row = Ti.UI.createTableViewRow({
                   title            : message.body,
                   backgroundColor : '#FF9900',
                   threadID     : message.thread_id ,
                   color            : 'blue'
               });

               tableData.push(row);
           }
           tableView.data = tableData;
       } else {
           alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
       }
   });
});

I want to display the last message in each thread in the tableViewRow(marked as message body). But the message body of the each threads does not displays the last message in the conversation and it displays the first message instead.

image of message.

Can anyone help me please? Thanks in advance!!


Solution

  • Finally I got it. I've made some changes in the code.

    win1.addEventListener('open', function() {
    tableData = [];
    // show all threads
    Cloud.Messages.showThreads(function(eThread) {
        if (eThread.success) {
            Ti.API.info('Total Threads: ' + eThread.messages.length);
            for (var i = 0; i < eThread.messages.length; i++) {
                var thread = eThread.messages[i];
                var message = "";
            // get the newest message from a thread
                Cloud.Messages.showThread({
                    thread_id : thread.thread_id
                }, function(eMessage) {
                        if (eMessage.success) {
                            var totalMessages = eMessage.messages.length;
                            Ti.API.info('Total messages in the thread ' + totalMessages);
                             //Getting the last message in the thread. 
                            message = eMessage.messages[0];
                             var row = Ti.UI.createTableViewRow({
                                    title : message.body,
                                backgroundColor : '#FF9900',
                                    threadID : thread.thread_id,
                                    color : 'blue'
                            });
    
                            tableData.push(row);
                tableView.data = tableData;
                    } else {
                        alert('Error showThread: ' + ((eMessage.error && eMessage.message) || JSON.stringify(eMessage)));
                    }
                 }); 
            }
        } else {
            alert('Error showThreads: ' + ((eThread.error && eThread.message) || JSON.stringify(eThread)));
        }
    });
    });
    

    Reference : Display the last message in Message Threads. I got hint from Mr.Matthias's answer in the link. The description is also given there. May be it will be helpful for someone else.