最近在开发项目的过程中需要接入消息推送,综合各种情况后最终选择使用环信的消息透传来实现
其中,遇到一个问题,就是不管推送多少条,信息栏中只显示最后一条信息,最终查阅资料后,找到了原因
实现的过程如下(前提是app内部已接入环信,并且配置好了环信)
** * 获取透传信息 */ public void getTouChuanXinxi() { // 注册一个cmd消息的BroadcastReceiver IntentFilter cmdIntentFilter = new IntentFilter(EMChatManager.getInstance().getCmdMessageBroadcastAction()); mContext.registerReceiver(cmdMessageReceiver, cmdIntentFilter); } /** * cmd消息BroadcastReceiver */ private BroadcastReceiver cmdMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //获取cmd message对象 EMMessage message = intent.getParcelableExtra("message"); //获取消息body CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody(); String aciton = cmdMsgBody.action;//获取自定义action i++; //获取扩展属性 try { String type = message.getStringAttribute("msgid"); JSONObject js = message.getJSONObjectAttribute("msg"); Log.i("*********透传消息*****", js.toString()); touChuanMessage = JSON.parseObject(js.toString(), TouChuanMessage.class); //消息通知栏 sendNotification(type, touChuanMessage); } catch (EaseMobException e) { e.printStackTrace(); } } };这是我在application中实现的环信消息透传配置,其中的type值是我和服务器约定好的参数值,不同的值代表着不同类型的推送消息,接下来根据不同的情况,向通知栏发送消息
/** * 向通知栏发送消息 * * @param msgid * @param */ public void sendNotification(String msgid, TouChuanMessage touChuanMessage) { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String edtTitle = ""; Intent notificationIntent = null; // 点击该通知后要跳转的Activity if (StringUtils.isEquals(msgid, "系统消息")) { if (!AppUtils.isBackground(mContext)) { notificationIntent = new Intent(mContext, MainActivity.class); // 点击该通知后要跳转的Activity } edtTitle = touChuanMessage.getContent(); } else if (StringUtils.isEquals(msgid, "最新评论消息")) { notificationIntent = new Intent(mContext, DetailsPageActivity.class); // 点击该通知后要跳转的Activity Bundle bundle = new Bundle(); bundle.putString("topicId", touChuanMessage.getTopicId() + ""); notificationIntent.putExtras(bundle); edtTitle = touChuanMessage.getUserName() + " 评论了您的活动"; }可以根据自己的情况定义更多类型的通知,然后对消息进行设置
其中箭头标识的地方的i值其实是id值,必须对不同的消息设置不同到的id才可以显示多条消息,并且跳转进入不同的界面,两个地方缺一不可。