Exemple #1
0
  /**
   * 当应用在前台时,如果当前消息不是属于当前会话,在状态栏提示一下 如果不需要,注释掉即可
   *
   * @param message
   */
  public void notifyNewMessage(EMMessage message) {
    // 如果是设置了不提醒只显示数目的群组(这个是app里保存这个数据的,demo里不做判断)
    // 以及设置了setShowNotificationInbackgroup:false(设为false后,后台时sdk也发送广播)
    if (!EasyUtils.isAppRunningForeground(this)) {
      return;
    }

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.zhxy_logo)
            .setContentTitle("新消息")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true);

    String ticker = CommonUtils.getMessageDigest(message, this);
    String st = getResources().getString(R.string.expression);
    if (message.getType() == Type.TXT) ticker = ticker.replaceAll("\\[.{2,3}\\]", st);
    // 设置状态栏提示
    mBuilder.setTicker(message.getFrom() + ": " + ticker);

    // 必须设置pendingintent,否则在2.3的机器上会有bug
    Intent intent = new Intent(this, ChatActivity.class);
    // 程序内 跳转 单聊
    if (message.getChatType() == ChatType.Chat) {
      intent.putExtra(Constants.TEL, message.getFrom());
      intent.putExtra(Constants.USNAME, message.getUserName());
      Log.e("TAG", "收到的手机号 = " + message.getFrom());
      Log.e("得到的用户名为:", message.getUserName());
    }
    // 程序内 跳转 群聊
    if (message.getChatType() == ChatType.GroupChat) {
      intent.putExtra(Constants.TEL, message.getTo());
      intent.putExtra("chatType", Constants.CHATTYPE_GROUP);
      intent.putExtra(
          Constants.CLASS_SHORT_NAME,
          SharedPreferencesUtil.getClassShortName(getApplicationContext()));
      Log.e(
          "TAG",
          "收到的群组id = "
              + message.getTo()
              + "传递的 班级名称 = "
              + SharedPreferencesUtil.getClassShortName(getApplicationContext()));
    }

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, notifiId, intent, PendingIntent.FLAG_ONE_SHOT);
    mBuilder.setContentIntent(pendingIntent);

    Notification notification = mBuilder.build();
    notificationManager.notify(notifiId, notification);

    Intent intent2 = new Intent();
    intent2.setAction(Constants.AREA_UNREAD);
    intent2.putExtra(Constants.MSG_FROM, message.getFrom());
    sendOrderedBroadcast(intent2, null);
  }
        @Override
        public boolean onNewMessage(EMMessage message) {
          if (stoped) {
            return false;
          }
          EMUserInfo userinfo = EMUserInfo.createEMUserInfo(message);
          PendingIntent pendingIntent = null;
          if (message.getChatType() == EMMessage.ChatType.Chat) {
            String toUserName =
                message.getChatType() == EMMessage.ChatType.Chat
                    ? message.getFrom()
                    : message.getTo();
            Intent intent =
                new Intent(self, SingleChatActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .putExtra(EaseConstant.EXTRA_USER_ID, toUserName);
            pendingIntent = PendingIntent.getActivity(self, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            NotificationHelper.showNotification(
                self,
                EMChatHelper.EMCHAT_NEW_MESSAGE_NOTIFY_ID,
                userinfo.getUserNick() + "发来一条新信息",
                EMMessageHelper.getMessageBody(message),
                pendingIntent);
          } else if (message.getChatType() == EMMessage.ChatType.ChatRoom) {
            return true;
          }
          return true;
        }
Exemple #3
0
    @Override
    public void onReceive(Context context, Intent intent) {
      // 主页面收到消息后,主要为了提示未读,实际消息内容需要到chat页面查看
      String from = intent.getStringExtra("from");
      // 消息id
      String msgId = intent.getStringExtra("msgid");
      EMMessage message = EMChatManager.getInstance().getMessage(msgId);
      // 2014-10-22 修复在某些机器上,在聊天页面对方发消息过来时不立即显示内容的bug
      if (ChatActivity.activityInstance != null) {
        if (message.getChatType() == ChatType.GroupChat) {
          if (message.getTo().equals(ChatActivity.activityInstance.getToChatUsername())) return;
        } else {
          if (from.equals(ChatActivity.activityInstance.getToChatUsername())) return;
        }
      }

      // 注销广播接收者,否则在ChatActivity中会收到这个广播
      abortBroadcast();

      notifyNewMessage(message);

      // 刷新bottom bar消息未读
      // updateUnreadLabel();
    }