예제 #1
0
  /**
   * 使用APNS服务推送到苹果
   *
   * @param product
   * @param cc
   * @param message
   */
  public void push(Product product, Client cc, Payload message) {
    PushManager<SimpleApnsPushNotification> service = get(product);
    if (service != null) {
      try {
        if (StringUtils.isBlank(cc.getDeviceToken())
            || "NULL".equalsIgnoreCase(cc.getDeviceToken())) {
          message.setStatus(cc.getUserId(), new PushStatus(PushStatus.NO_DEVICE_TOKEN));
        } else {

          SimpleApnsPushNotification e = wrapPayload(cc, message);
          if (e == null) {
            message.setStatus(cc.getUserId(), new PushStatus(PushStatus.DeviceTokenInvalid));
          } else {
            service.getQueue().put(e);
            message.setStatus(cc.getUserId(), new PushStatus(PushStatus.APNSSent));
            ClientServiceImpl.instance.updateBadge(cc.getUserId(), 1);
          }
        }
      } catch (Exception e) {
        logger.error("Push Failed", e);
        message.setStatus(cc.getUserId(), new PushStatus(PushStatus.iOSPushError, e.getMessage()));
      }
    } else {
      logger.error("iOS Push Service Not Found.");
      message.setStatus(cc.getUserId(), new PushStatus(PushStatus.iOSPushConfigError));
    }
  }
예제 #2
0
 /**
  * 发送消息
  *
  * @param message
  */
 public void send(final Payload message) {
   // 组装消息包
   if (context.channel().isWritable()) {
     try {
       byte[] msg = message.asAPNSMessage().toByteArray();
       send(message, msg);
     } catch (Exception e) {
       logger.error(e.getMessage(), e);
       message.setStatus(this.userId, new PushStatus(PushStatus.UnKnown, e.getMessage()));
     }
   } else {
     message.setStatus(this.userId, new PushStatus(PushStatus.ChannelClosed, null));
     logger.error("Send Error. Channel is closed. {}, {}", context, message);
   }
 }
예제 #3
0
  /**
   * 发送消息
   *
   * @param message
   * @param msg
   */
  public void send(final Payload message, final byte[] msg) {
    try {

      final ByteBuf data = context.alloc().buffer(msg.length); // (2)
      data.writeBytes(msg);

      final ChannelFuture cf = context.writeAndFlush(data);
      cf.addListener(
          new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
              if (cf.cause() != null) {
                logger.error("{}, Send Error.", context, cf.cause());
                PayloadServiceImpl.instance.updateSendStatus(
                    message,
                    userId,
                    new PushStatus(PushStatus.WriterError, cf.cause().getMessage()));
              } else {
                updateOpTime();
                PayloadServiceImpl.instance.updateSendStatus(
                    message, userId, new PushStatus(PushStatus.Success));
                ClientServiceImpl.instance.updateBadge(userId, 1);
                if (logger.isDebugEnabled()) {
                  logger.debug("Send Done, userId={}, messageId={}", userId, message.getId());
                }
              }
            }
          });

    } catch (Exception e) {
      message.setStatus(userId, new PushStatus(PushStatus.UnKnown, e.getMessage()));
      logger.error(e.getMessage(), e);
    }
  }
예제 #4
0
  /**
   * @param cc
   * @param message
   * @return
   */
  private SimpleApnsPushNotification wrapPayload(Client cc, Payload message) {

    final byte[] token;
    try {
      token = TokenUtil.tokenStringToByteArray(cc.getDeviceToken());
    } catch (MalformedTokenStringException e) {
      logger.error("DeviceToken is Invalid. token=" + cc.getDeviceToken());
      return null;
    }

    Date expireDate = new Date(System.currentTimeMillis() + expireTime);
    SimpleApnsPushNotification notification =
        new SimpleApnsPushNotification(token, message.asJson(), expireDate);
    return notification;
  }