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

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(false);

    String ticker = CommonUtils.getMessageDigest(message, this);
    String st = getResources().getString(R.string.expression);
    if (message.getType() == EMMessage.Type.TXT) ticker = ticker.replaceAll("\\[.{2,3}\\]", st);
    // 设置状态栏提示
    mBuilder.setTicker(nick + ": " + ticker);
    mBuilder.setContentText("查看新的未读消息");
    mBuilder.setContentTitle(message.getStringAttribute(Const.Easemob.FROM_USER_NICK, "新消息"));

    // 必须设置pendingintent,否则在2.3的机器上会有bug
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Const.Intent.HX_NTF_TO_MAIN, true);
    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, notifiId, intent, PendingIntent.FLAG_ONE_SHOT);
    mBuilder.setContentIntent(pendingIntent);
    Notification notification = mBuilder.build();
    notificationManager.notify(notifiId, notification);
    //        notificationManager.cancel(notifiId);
  }
Пример #2
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);
  }
Пример #3
0
 public synchronized void onNewMesg(List<EMMessage> messages) {
   if (EMChatManager.getInstance().isSlientMessage(messages.get(messages.size() - 1))) {
     return;
   }
   // 判断app是否在后台
   if (!EasyUtils.isAppRunningForeground(appContext)) {
     EMLog.d(TAG, "app is running in backgroud");
     sendNotification(messages, false);
   } else {
     sendNotification(messages, true);
   }
   viberateAndPlayTone(messages.get(messages.size() - 1));
 }