Exemplo n.º 1
0
 public static TextMessage buildForSend(
     String content, UserEntity fromUser, PeerEntity peerEntity) {
   TextMessage textMessage = new TextMessage();
   int nowTime = (int) (System.currentTimeMillis() / 1000);
   textMessage.setFromId(fromUser.getPeerId());
   textMessage.setToId(peerEntity.getPeerId());
   textMessage.setUpdated(nowTime);
   textMessage.setCreated(nowTime);
   textMessage.setDisplayType(DBConstant.SHOW_ORIGIN_TEXT_TYPE);
   textMessage.setGIfEmo(true);
   int peerType = peerEntity.getType();
   int msgType =
       peerType == DBConstant.SESSION_TYPE_GROUP
           ? DBConstant.MSG_TYPE_GROUP_TEXT
           : DBConstant.MSG_TYPE_SINGLE_TEXT;
   textMessage.setMsgType(msgType);
   textMessage.setStatus(MessageConstant.MSG_SENDING);
   // 内容的设定
   textMessage.setContent(content);
   textMessage.buildSessionKey(true);
   return textMessage;
 }
  private void showNotification(final UnreadEntity unreadEntity) {
    // todo eric need to set the exact size of the big icon
    // 服务端有些特定的支持 尺寸是不是要调整一下 todo 100*100  下面的就可以不要了
    ImageSize targetSize = new ImageSize(80, 80);
    int peerId = unreadEntity.getPeerId();
    int sessionType = unreadEntity.getSessionType();
    String avatarUrl = "";
    String title = "";
    String content = unreadEntity.getLatestMsgData();
    String unit = ctx.getString(R.string.msg_cnt_unit);
    int totalUnread = unreadEntity.getUnReadCnt();

    if (unreadEntity.getSessionType() == DBConstant.SESSION_TYPE_SINGLE) {
      UserEntity contact = IMContactManager.instance().findContact(peerId);
      if (contact != null) {
        title = contact.getMainName();
        avatarUrl = contact.getAvatar();
      } else {
        title = "User_" + peerId;
        avatarUrl = "";
      }

    } else {
      GroupEntity group = IMGroupManager.instance().findGroup(peerId);
      if (group != null) {
        title = group.getMainName();
        avatarUrl = group.getAvatar();
      } else {
        title = "Group_" + peerId;
        avatarUrl = "";
      }
    }
    // 获取头像
    avatarUrl = IMUIHelper.getRealAvatarUrl(avatarUrl);
    final String ticker = String.format("[%d%s]%s: %s", totalUnread, unit, title, content);
    final int notificationId = getSessionNotificationId(unreadEntity.getSessionKey());
    final Intent intent = new Intent(ctx, MessageActivity.class);
    intent.putExtra(IntentConstant.KEY_SESSION_KEY, unreadEntity.getSessionKey());

    logger.d("notification#notification avatarUrl:%s", avatarUrl);
    final String finalTitle = title;
    ImageLoader.getInstance()
        .loadImage(
            avatarUrl,
            targetSize,
            null,
            new SimpleImageLoadingListener() {

              @Override
              public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                logger.d("notification#icon onLoadComplete");
                // holder.image.setImageBitmap(loadedImage);
                showInNotificationBar(finalTitle, ticker, loadedImage, notificationId, intent);
              }

              @Override
              public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                logger.d("notification#icon onLoadFailed");
                // 服务器支持的格式有哪些
                // todo eric default avatar is too small, need big size(128 * 128)
                Bitmap defaultBitmap =
                    BitmapFactory.decodeResource(
                        ctx.getResources(),
                        IMUIHelper.getDefaultAvatarResId(unreadEntity.getSessionType()));
                showInNotificationBar(finalTitle, ticker, defaultBitmap, notificationId, intent);
              }
            });
  }