ionic2 - Applozic Ionic 获取用户的最后一条消息

标签 ionic2 chat ionic3 applozic

在 Applozic/Ionic 集成应用程序中,我需要获取用户或群组的最后一条消息和聊天时间

我已阅读 Applozic - Ionic 集成的文档,但尚未找到上述解决方案。下面只提到

//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){
    });

最佳答案

目前没有方法可以为特定用户或组提供最新消息。但是您可以获取用户发起聊天的所有联系人和组的最新消息,或者获取特定联系人或组的所有消息。为此,插件中有一个函数 - getConversationList()。

>>获取特定联系人/组的对话列表:

按照以下步骤获取特定联系人/群组的消息:

1)创建messageListModel对象:

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) 将此对象传递给 getConversationList() 函数:

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

您将在onSuccess回调函数中收到一个conversationList。

3)对话对象有三个对象:

a) 消息 - 给特定联系人/组的消息

b) 联系人 - 如果消息来自群组,则为空

c) channel - 如果消息是针对联系人的,则为空

因此,在您的情况下,您将收到与您在 messageListModel 对象中传递的相同联系人/ channel 的对话列表。列表中的最后一个对话就是您要查找的内容。

>>>获取所有联系人/组的最新消息:

您还可以获取用户发起聊天的所有联系人/群组的最新消息。就像 Whatsapp 主屏幕一样。

1)创建messageListModel对象:

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) 将此对象传递给 getConversationList() 函数:

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

您将在onSuccess回调函数中收到一个conversationList。

3)对话对象有三个对象:

a) 消息 - 联系人/群组的最新消息

b) 联系人 - 如果消息来自群组,则为空

c) channel - 如果消息是针对联系人的,则为空

您可以在此列表中查找联系人/ channel 并获取相应的消息。 对话列表按消息创建时间的降序排列。就像您在 Whatsapp 主屏幕中看到的一样。最新消息位于顶部。因此,如果您的联系人不在前 60 个对话中,您需要再次调用电话,但这次在消息列表模型对象中传递最新消息的 createAtTime,如下所示,这将为您提供接下来 60 个对话的批处理:

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;
      };

如何获取消息的时间:

To get the time of the message you could do:

conversationList[index].message.createdAtTime;

以下是上述使用的对象的所有属性,以方便您使用。

对话对象的属性:

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

消息对象的属性:

    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;
}

联系人对象的属性:

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;
}

channel 对象的属性:

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[];
}

关于ionic2 - Applozic Ionic 获取用户的最后一条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46680309/

相关文章:

ionic-framework - 访问pwa地址的url参数

cordova - 如何在 ionic cordova 上删除 iPhone X 顶部视口(viewport)角的空白区域

javascript - Angular 2如何从嵌套的 promise 中返回对象数组

angular - Ionic 2 - 模型驱动形式的设置值

ios - 在 QBVideoChat 中公开 AVAudioSession

javascript - 单例 javascript appendChild

java - 使用 websocket 从多个设备聊天

image - ionic 3 : Print image using Bluetooth printer

Angular 5 ng-repeat 实现

angular - Ionic 2 中的谷歌字体