コード例 #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
  @Override
  public void onEvent(EMNotifierEvent event) {
    switch (event.getEvent()) {
      case EventNewMessage: // 普通消息
        {
          EMMessage message = (EMMessage) event.getData();
          try {
            showLogD("MESSAGE " + message.getStringAttribute("groupName"));
          } catch (EaseMobException e) {
            e.printStackTrace();
          }
          // 提示新消息
          HXSDKHelper.getInstance().getNotifier().onNewMsg(message);

          refreshUI();
          break;
        }

      case EventOfflineMessage:
        {
          refreshUI();
          break;
        }

      case EventConversationListChanged:
        {
          refreshUI();
          break;
        }

      default:
        break;
    }
  }
コード例 #3
0
 @Override
 public void onReceive(Context context, Intent intent) {
   // 获取cmd message对象
   String msgId = intent.getStringExtra("msgid");
   EMMessage message = intent.getParcelableExtra("message");
   // 获取消息body
   CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody();
   String aciton = cmdMsgBody.action; // 获取自定义action
   // 获取扩展属性
   try {
     String attr = message.getStringAttribute("photoUrl");
     ImageLoader.getInstance().getMemoryCache().remove(attr);
     ImageLoader.getInstance().getDiskCache().remove(attr);
     showToast("清除缓存");
     System.out.println("清除缓存");
   } catch (EaseMobException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }