Ejemplo n.º 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));
    }
  }
Ejemplo n.º 2
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;
  }