我在聊天应用程序中使用了quickblox android sdk。我知道当对手用户启动会话时QBPrivateChatManagerListener - > chatCreated()事件被触发。以下是我的代码。当chatCreated被触发时,我加载了聊天片段。在聊天片段中,我实现了QBMessageListenerImpl接口。这里的问题是我没有从processMessage()获得第一条消息。但我收到了第二条消息。所以我的问题是如何阅读对手用户发送的第一条消息?
@Override
public void chatCreated(final QBPrivateChat qbPrivateChat, boolean b) {
ShowMessage("Incoming chat!!");
if(!b)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
CreateNewChatForIncoming(qbPrivateChat);
}
});
}
}
public void CreateNewChatForIncoming(QBPrivateChat qbPrivateChat) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
PrivateChatFragment privateChatFragment = new PrivateChatFragment();
privateChatFragment.setOpponentuser(null);
privateChatFragment.setIncomingChat(qbPrivateChat);
HashMap<Integer, QBUser> allusers = applicationSingleton.getAllusers();
String title = allusers.get(qbPrivateChat.getParticipant()).getFullName();
if (title != null)
this.setTitle(title);
ft.replace(R.id.content_frame, privateChatFragment);
ft.commit();
}
/*---------------Private chat Fragment-------------------------*/
public class PrivateChatFragment extends SherlockFragment {
TextView txtmsg;
MessageAdaptor messageAdaptor;
ListView listView;
boolean ismode = true;
PrivateChat privateChat;
private QBUser opponentuser;
private QBPrivateChat incomingChat;
int opponentuserid;
ApplicationSingleton applicationSingleton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chattabfragment, container, false);
txtmsg = (TextView) view.findViewById(R.id.txtmsg);
SetFont(txtmsg);
applicationSingleton = (ApplicationSingleton)getSherlockActivity().getApplication();
messageAdaptor = new MessageAdaptor(this.getActivity().getApplicationContext(), R.layout.msg_list_item_inbound);
if (opponentuser != null) {
opponentuserid = opponentuser.getId();
HashMap<Integer, PrivateChat> allpvtchats = applicationSingleton.getAllpvtchats();
if(allpvtchats.containsKey(opponentuserid)) {
privateChat = applicationSingleton.getAllpvtchats().get(opponentuserid);
}
else {
privateChat = new PrivateChat(this, opponentuserid);
applicationSingleton.getAllpvtchats().put(opponentuserid,privateChat);
}
List<ChatMessage> allmsgs = GetMessageHistory(opponentuser);
if (allmsgs != null) {
ShowMessage("setting history");
messageAdaptor.LoadNewItems(allmsgs);
messageAdaptor.notifyDataSetChanged();
messageAdaptor.notifyDataSetInvalidated();
}
} else if (incomingChat != null) {
opponentuserid = incomingChat.getParticipant();
privateChat = new PrivateChat(this, incomingChat);
}
listView = (ListView) view.findViewById(R.id.listview_msg);
listView.setAdapter(messageAdaptor);
txtmsg.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
ChatMessage msg = new ChatMessage();
msg.setOutbound(true);
msg.setImg(R.drawable.boy1);
msg.setTime("10:30am");
msg.setMessage(txtmsg.getText().toString());
messageAdaptor.add(msg);
txtmsg.setText("");
scrolToBottom();
QBChatMessage chatMessage = new QBChatMessage();
chatMessage.setBody(msg.getMessage());
//chatMessage.setProperty("name", opponentuser.getFullName());
chatMessage.setSaveToHistory(true);
chatMessage.setMarkable(true);
try {
privateChat.sendMessage(chatMessage);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
return true;
}
return false;
}
});
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
ApplicationSingleton applicationSingleton = (ApplicationSingleton) this.getActivity().getApplication();
HashMap<Integer, List<ChatMessage>> allchats = applicationSingleton.getAllchats();
allchats.put(opponentuserid, messageAdaptor.GetAllMessages());
applicationSingleton.setAllchats(allchats);
}
private List<ChatMessage> GetMessageHistory(QBUser opnnentuser) {
ApplicationSingleton applicationSingleton = (ApplicationSingleton) this.getActivity().getApplication();
HashMap<Integer, List<ChatMessage>> allchats = applicationSingleton.getAllchats();
if (allchats.containsKey(opnnentuser.getId())) {
return allchats.get(opnnentuser.getId());
} else {
return null;
}
}
void SetFont(TextView view) {
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/LatoRegular.ttf");
view.setTypeface(font);
}
public void scrolToBottom() {
listView.post(new Runnable() {
@Override
public void run() {
listView.setSelection(messageAdaptor.getCount() - 1);
}
});
}
public void UpdateAdaptor(final ChatMessage chatMessage) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (messageAdaptor != null) {
messageAdaptor.add(chatMessage);
messageAdaptor.notifyDataSetChanged();
scrolToBottom();
}
}
});
}
public void setOpponentuser(QBUser opponentuser) {
this.opponentuser = opponentuser;
}
public void setIncomingChat(QBPrivateChat incomingChat) {
this.incomingChat = incomingChat;
}
public void SendNotification(String title, String message) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getSherlockActivity())
.setSmallIcon(R.drawable.boy6_small)
.setContentTitle(title)
.setContentText(message);
int mId = GetNotificationid();
Intent resultIntent = new Intent(getSherlockActivity(), SideMenuActivity.class);
resultIntent.putExtra(GlobalData.OP_ID, opponentuser.getId());
resultIntent.putExtra(GlobalData.NOTIFY_MODE, true);
resultIntent.putExtra(GlobalData.NOTIFY_ID, mId);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getSherlockActivity());
stackBuilder.addParentStack(SideMenuActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSherlockActivity().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
private void ShowMessage(final String msg) {
Runnable mrunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
};
getActivity().runOnUiThread(mrunnable);
}
private int GetNotificationid()
{
Calendar c = Calendar.getInstance();
int milsec = c.get(Calendar.MILLISECOND);
return milsec;
}
}
/* --------------------Privatechat class------------------- */
public class PrivateChat extends QBMessageListenerImpl<QBPrivateChat>{
private QBPrivateChatManager privateChatManager;
private QBPrivateChat privateChat;
private PrivateChatFragment currentContext;
private ApplicationSingleton applicationSingleton;
public PrivateChat(PrivateChatFragment context,Integer opponentID) {
currentContext = context;
applicationSingleton = (ApplicationSingleton)currentContext.getSherlockActivity().getApplication();
privateChatManager = applicationSingleton.getChatService().getPrivateChatManager();
// init private chat
//
privateChat = privateChatManager.getChat(opponentID);
if (privateChat == null) {
privateChat = privateChatManager.createChat(opponentID, this);
}else{
privateChat.addMessageListener(this);
}
}
public PrivateChat(PrivateChatFragment context,QBPrivateChat incomingChat) {
privateChat = incomingChat;
privateChat.addMessageListener(this);
currentContext = context;
}
public void sendMessage(QBChatMessage message) throws XMPPException, SmackException.NotConnectedException {
privateChat.sendMessage(message);
}
public void release() {
privateChat.removeMessageListener(this);
}
@Override
public void processMessage(QBPrivateChat chat, QBChatMessage message) {
Log.e("Anuradha-message",message.getBody());
ChatMessage msg = new ChatMessage();
msg.setOutbound(false);
msg.setImg(R.drawable.boy2);
msg.setTime("10:30am");
msg.setMessage(message.getBody());
currentContext.UpdateAdaptor(msg);
if(applicationSingleton.isAppBackground()) {
HashMap<Integer,QBUser> allusers = applicationSingleton.getAllusers();
QBUser user = allusers.get(message.getSenderId());
currentContext.SendNotification(user.getFullName().toString(),"says "+message.getBody());
}
}
@Override
public void processError(QBPrivateChat chat, QBChatException error, QBChatMessage originChatMessage){
}
@Override
public void processMessageDelivered(QBPrivateChat privateChat, String messageID){
}
@Override
public void processMessageRead(QBPrivateChat privateChat, String messageID){
ShowMessage("msg read : "+messageID);
}
private void ShowMessage(final String msg) {
Runnable mrunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(currentContext.getActivity(), msg, Toast.LENGTH_LONG).show();
}
};
currentContext.getActivity().runOnUiThread(mrunnable);
}
}
答案 0 :(得分:0)
尝试更改
privateChat = privateChatManager.getChat(opponentID);`enter code here` if (privateChat == null) {
privateChat = privateChatManager.createChat(opponentID, this);
}else{
privateChat.addMessageListener(this);
}
以强>
if (privateChat == null) {
privateChat = privateChatManager.createChat(opponentID, this);
}
privateChat.addMessageListener(this);