예제 #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
    @Override
    public void handleRejectedNotification(
        final PushManager<? extends SimpleApnsPushNotification> pushManager,
        final SimpleApnsPushNotification notification,
        final RejectedNotificationReason reason) {

      logger.error(
          "[%s] %s was rejected with rejection reason %s\n",
          pushManager.getName(), notification, reason);
    }
예제 #3
0
    @Override
    public void handleFailedConnection(
        final PushManager<? extends SimpleApnsPushNotification> pushManager,
        final Throwable cause) {

      logger.error(pushManager.getName() + " failed to connect Apple APNS server. ", cause);

      if (cause instanceof SSLHandshakeException) {
        // This is probably a permanent failure, and we should shut down
        // the PushManager.
      }
    }
예제 #4
0
  public PushManager get(Product product) {

    if (StringUtils.isBlank(product.getDevCertPath())
        || StringUtils.isBlank(product.getDevCertPass())
        || StringUtils.isBlank(product.getCertPath())
        || StringUtils.isBlank(product.getCertPass())) {
      logger.error("Product iOS Push Service Miss Cert Path and Password. {}", product);
      return null;
    }

    PushManager service = mapping.get(product.getId());
    if (service == null) {

      ApnsEnvironment apnsEnvironment = null;
      SSLContext sslContext = null;

      try {
        if (sandBox) {
          apnsEnvironment = ApnsEnvironment.getSandboxEnvironment();
          sslContext =
              SSLContextUtil.createDefaultSSLContext(
                  product.getDevCertPath(), product.getDevCertPass());
        } else {
          apnsEnvironment = ApnsEnvironment.getProductionEnvironment();
          sslContext =
              SSLContextUtil.createDefaultSSLContext(product.getCertPath(), product.getCertPass());
        }
      } catch (KeyStoreException e) {
        logger.error(e.getMessage(), e);
      } catch (NoSuchAlgorithmException e) {
        logger.error(e.getMessage(), e);
      } catch (CertificateException e) {
        logger.error(e.getMessage(), e);
      } catch (UnrecoverableKeyException e) {
        logger.error(e.getMessage(), e);
      } catch (KeyManagementException e) {
        logger.error(e.getMessage(), e);
      } catch (IOException e) {
        logger.error(e.getMessage(), e);
      }

      PushManagerConfiguration configuration = new PushManagerConfiguration();
      configuration.setConcurrentConnectionCount(1);

      final PushManager<SimpleApnsPushNotification> pushManager =
          new PushManager<SimpleApnsPushNotification>(
              apnsEnvironment,
              sslContext,
              null, // Optional: custom event loop group
              null, // Optional: custom ExecutorService for calling listeners
              null, // Optional: custom BlockingQueue implementation
              configuration,
              "ApnsPushManager-" + product.getId());

      pushManager.registerRejectedNotificationListener(new PushRejectedNotificationListener());
      pushManager.registerFailedConnectionListener(new PushFailedConnectionListener());

      pushManager.start();

      //             ApnsServiceBuilder builder =  APNS.newService();
      //            if (sandBox){
      //                builder.withCert(product.getDevCertPath(), product.getDevCertPass());
      //                builder.withSandboxDestination();
      //            }else{
      //                builder.withCert(product.getCertPath(), product.getCertPass());
      //                builder.withProductionDestination();
      //            }
      //            service =
      // builder.asPool(10).withCacheLength(Integer.MAX_VALUE).withDelegate(delegateAdapter).asQueued().build();

      mapping.put(product.getId(), pushManager);
      service = pushManager;
    }

    return service;
  }