Search code examples
ionic2chationic3applozic

Applozic Ionic get last message for user


In Applozic/Ionic integrated app, I need to get last message and time of chat for a user or group

I have read documentation for Applozic - Ionic integration but have not found a solution for above. It only mentions below

//Get Total Unread Count
applozic.getUnreadCount(function(response){
     var count = response;
    },
    function(error){alert(error)
   });
   
//Get Unread count per user
   var userId = 'USER_ID'; //pass UserId with which unread count 
   applozic.getUnreadCountForUser(userId,function(response){
     var count = response;
    },
    function(error){alert(error)
   });
   
 //Get unread count per group
   var groupId = 'GROUP_ID'; // pass groupId in which unreadcount required

  applozic.getUnreadCountForGroup(groupId,function(response){
       var count = response;
    },
     function(error){
    });


Solution

  • Currently there is no method that provides the latest message for a particular user or group. But you can get the latest messages for all the contacts and group that the user has inititated a chat with OR get all the messages for a particular contact or group. For this there is a function in the plugin - getConversationList().

    >>Getting Conversation List for particular contact/group:

    Follow the below steps to get the messages for a particualar contact/group:

    1)Create a messageListModel object:

    var messageListModel = {
      'startTime' : null, //start time
     'endTime' : null, //end time
     'contactId' : <your-contact-id>, //(this is string) pass contact id to get the message list for that contact
    'searchString' : null, // pass the search string to get the latest messages that match the search string
    'channelKey' : <your-group-id>, //(this is number) pass the channel key to get the message list for that channel
    'conversationId' : null,
    'isScroll' : false, //is scroll will be false if you want to get all list of chats  
    'isSkipRead' : false,
      };
    

    2) Pass this object to the getConversationList() function:

    applozic.getConversationList(messageListModel, (conversationList)=> {
    console.log(JSON.stringify(conversationList))
    }, ()=>{});
    

    You would receive a conversationList in the onSuccess callback function.

    3) The conversation object has three objects:

    a) Message - Message for a particular contact/group

    b) Contact - would be null if the message is from group

    c) Channel - would be null if the message is for a contact

    So in your case you would receive a list of Conversations with the same Contact/Channel that you have passed in the messageListModel object. The last Conversation in the list is what you are looking for.

    >>>Getting Latest Messages for all contacts/group:

    You could also get the latest messages for all contacts/group that the user has inititated the chat with. Just like whatsapp homescreen.

    1)Create a messageListModel object:

    var messageListModel = {
      'startTime' : null, //start time
     'endTime' : null, //end time
     'contactId' : null, //pass contact id to get the message list for that contact
    'searchString' : null, // pass the search string to get the latest messages that match the search string
    'channelKey' : null, // pass the channel key to get the message list for that channel
    'conversationId' : null,
    'isScroll' : false, //is scroll will be false if you want to get all list of chats  
    'isSkipRead' : false,
      };
    

    2) Pass this object to the getConversationList() function:

    applozic.getConversationList(messageListModel, (conversationList)=> {
    console.log(JSON.stringify(conversationList))
    }, ()=>{});
    

    You would receive a conversationList in the onSuccess callback function.

    3) The conversation object has three objects:

    a) Message - latest message for contact/group

    b) Contact - would be null if the message is from group

    c) Channel - would be null if the message is for a contact

    You could find for the contact/channel in this list and get the message for that. The Conversation list is sorted in descending order of the time a message was created. Just like What you see in whatsapp home screen. Latest messages are on top. So if your contact falls out of the top 60 conversations you need to make the call again but this time passing the latest message's createdAtTime in the message list model object like below, this would give you the batch of next 60 conversations:

    var messageListModel = {
          'startTime' : null, //start time
         'endTime' : null, //end time
         'contactId' : null, //pass contact id to get the message list for that contact
        'searchString' : null, // pass the search string to get the latest messages that match the search string
        'channelKey' : null, // pass the channel key to get the message list for that channel
        'conversationId' : null,
        'isScroll' : false, //is scroll will be false if you want to get all list of chats  
        'isSkipRead' : false,
        'createdAtTime' : conversationList[conversationList.length - 1].message.createdAtTime;
          };
    

    How to get the Time of the message:

    To get the time of the message you could do:

    conversationList[index].message.createdAtTime;
    

    Below are all the properties of the above used objects for your convenience.

    Properties of Conversation object:

    interface Conversation{
      message : Message;
      contact : Contact;
      channel : Channel;
    }
    

    Properties of Message object:

        interface Message{
          createdAtTime : number;
          to : string;
          message : string;
          key : string;
          deviceKey : string;
          userKey : string;
          emailIds : string;
          shared : boolean;
          sent : boolean;
          delivered : boolean;
          type : number;
          storeOnDevice : boolean;
          contactIds : string;
          groupId : number;
          scheduledAt : number;
          source : number;
          timeToLive : number;
          sentToServer : boolean;
          fileMetaKey : string;
          filePaths : string[];
          pairedMessageKey : string;
          sentMessageTimeAtServer : number;
          canceled : boolean;
          clientGroupId : string;
          messageId : number;
          read : boolean;
          attDownloadInProgress : boolean;
          applicationId : string;
          conversationId : number;
          topicId : string;
          connected : boolean;
          contentType : number;
          status : number;
          hidden : boolean;
          replyMessage : number;
          fileMeta : FileMeta;
          metadata : Map<string,string>;
        }
    
        interface FileMeta{
      key : string;
      userKey : string;
      blobKey : string;
      name : string;
      url : string;
      size : number;
      contentType : string;
      thumbnailUrl : string;
      createdAtTime : number;
    }
    

    Properties of a contact object:

    interface Contact{
      firstName : string;
      middleName : string;
      lastName : string;
      emailIdLists : string[];
      contactNumberLists : string[];
      phoneNumbers : Map<string, string>;
      contactNumber : string;
      formattedContactNumber : string;
      contactId : number;
      fullName : string;
      userId : string;
      imageURL : string;
      localImageUrl : string;
      emailId : string;
      applicationId : string;
      connected : boolean;
      lastSeenAtTime : number;
      checked  : boolean;
      unreadCount : number;
      blocked : boolean;
      blockedBy : boolean;
      status : string;
      userTypeId : number;
      contactType : number;
      deletedAtTime : number;
      latestMessageAtTime : number;
    }
    

    Properties of a Channel object:

    interface Channel{
      key : number;
      parentKey : number;
      clientGroupId : string;
      name : string;
      adminKey : string;
      type : number;
      unreadCount : number;
      userCount : number;
      subGroupCount : number;
      imageUrl : string;
      notificationAfterTime : number;
      localImageUri : string;
      contacts : Contact[];
    }