问题描述
我用的是EaseUI那个Library,然后仿EaseUISimpleDemo写的,可是这个里面没有广播接收消息,程序在后台的时候不能收到发过来的消息,有没有教程?本人小白,求大神指导下。。。感谢。
解决方案
我是copy过来的代码。帮忙看下,下面的对么 那个msgid不用自己设置么,? //初始化环信 private void initHuanXin() { EaseUI.getInstance().init(getApplicationContext());//环信聊天 EMChat.getInstance().setAppInited(); //注册接收新消息的监听广播 //只有注册了广播才能接收到新消息,目前离线消息,在线消息都是走接收消息的广播(离线消息目前无法监听,在登录以后,接收消息广播会执行一次拿到所有的离线消息) NewMessageBroadcastReceiver msgReceiver = new NewMessageBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(EMChatManager.getInstance().getNewMessageBroadcastAction()); intentFilter.setPriority(3); registerReceiver(msgReceiver, intentFilter); //注册接收ack回执消息的BroadcastReceiver EMChatManager.getInstance().getChatOptions().setRequireAck(true);//如果用到已读的回执需要把这个flag设置成true IntentFilter ackMessageIntentFilter = new IntentFilter(EMChatManager.getInstance().getAckMessageBroadcastAction()); ackMessageIntentFilter.setPriority(3); registerReceiver(ackMessageReceiver, ackMessageIntentFilter); } //注册接收ack回执消息的BroadcastReceiver private BroadcastReceiver ackMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { abortBroadcast(); String msgid = intent.getStringExtra("msgid"); String from = intent.getStringExtra(openid); EMConversation conversation = EMChatManager.getInstance().getConversation(from); if (conversation != null) { // 把message设为已读 EMMessage msg = conversation.getMessage(msgid); if (msg != null) { msg.isAcked = true; } } } }; //注册接收新消息的监听广播 private class NewMessageBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 注销广播 abortBroadcast(); // 消息id(每条消息都会生成唯一的一个id,目前是SDK生成) String msgId = intent.getStringExtra("msgid"); //发送方 String username = intent.getStringExtra(openid);//openid为用户ID // 收到这个广播的时候,message已经在db和内存里了,可以通过id获取mesage对象 EMMessage message = EMChatManager.getInstance().getMessage(msgId); EMConversation conversation = EMChatManager.getInstance().getConversation(username); // 如果是群聊消息,获取到group id if (message.getChatType() == ChatType.GroupChat) { username = message.getTo(); } if (!username.equals(username)) { // 消息不是发给当前会话,return return; } } }
解决方案二:
在applicaiton中注册消息监听: /** * 全局事件监听 * 因为可能会有UI页面先处理到这个消息,所以一般如果UI页面已经处理,这里就不需要再次处理 * activityList.size() <= 0 意味着所有页面都已经在后台运行,或者已经离开Activity Stack */ protected void registerEventListener() { eventListener = new EMEventListener() { private BroadcastReceiver broadCastReceiver = null; @Override public void onEvent(EMNotifierEvent event) { EMMessage message = null; if(event.getData() instanceof EMMessage){ message = (EMMessage)event.getData(); EMLog.d(TAG, "receive the event : " + event.getEvent() + ",id : " + message.getMsgId()); } switch (event.getEvent()) { case EventNewMessage: //应用在后台,不需要刷新UI,通知栏提示新消息 if(!easeUI.hasForegroundActivies()){ getNotifier().onNewMsg(message); } break; case EventOfflineMessage: if(!easeUI.hasForegroundActivies()){ EMLog.d(TAG, "received offline messages"); List<EMMessage> messages = (List<EMMessage>) event.getData(); getNotifier().onNewMesg(messages); } break; // below is just giving a example to show a cmd toast, the app should not follow this // so be careful of this case EventNewCMDMessage: { EMLog.d(TAG, "收到透传消息"); //获取消息body CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody(); final String action = cmdMsgBody.action;//获取自定义action //获取扩展属性 此处省略 //message.getStringAttribute(""); EMLog.d(TAG, String.format("透传消息:action:%s,message:%s", action,message.toString())); final String str = appContext.getString(R.string.receive_the_passthrough); final String CMD_TOAST_BROADCAST = "easemob.demo.cmd.toast"; IntentFilter cmdFilter = new IntentFilter(CMD_TOAST_BROADCAST); if(broadCastReceiver == null){ broadCastReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(appContext, intent.getStringExtra("cmd_value"), Toast.LENGTH_SHORT).show(); } }; //注册广播接收者 appContext.registerReceiver(broadCastReceiver,cmdFilter); } Intent broadcastIntent = new Intent(CMD_TOAST_BROADCAST); broadcastIntent.putExtra("cmd_value", str+action); appContext.sendBroadcast(broadcastIntent, null); break; } case EventDeliveryAck: message.setDelivered(true); break; case EventReadAck: message.setAcked(true); break; // add other events in case you are interested in default: break; } } }; EMChatManager.getInstance().registerEventListener(eventListener); }
解决方案三:
不管用。。