Пример #1
0
  @Override
  public void run() {
    String appKey = ConfigReader.getProp("appKey");
    String appSecret = ConfigReader.getProp("appSecret");
    if (selector == null) {
      selector = Selectors.all();
    }

    APIClient apiClient = APIClient.newBuilder().setKey(appKey).setSecret(appSecret).build();

    Type badgetype = IOSBadgeData.Type.VALUE;
    if (badge == null || badge == 0) {
      badgetype = IOSBadgeData.Type.INCREMENT;
      badge = 1;
    }

    IOSBadgeData badgeData = IOSBadgeData.newBuilder().setValue(badge).setType(badgetype).build();

    IOSDevicePayload iosPayload;
    AndroidDevicePayload androidPayload;
    if (entries != null) {
      iosPayload =
          IOSDevicePayload.newBuilder()
              .setAlert(message)
              .setBadge(badgeData)
              .setSound("default")
              .addAllExtraEntries(entries)
              .build();
      androidPayload =
          AndroidDevicePayload.newBuilder().setAlert(message).addAllExtraEntries(entries).build();
    } else {
      iosPayload =
          IOSDevicePayload.newBuilder()
              .setAlert(message)
              .setBadge(badgeData)
              .setSound("default")
              .build();
      androidPayload = AndroidDevicePayload.newBuilder().setAlert(message).build();
    }

    Notification notification = Notifications.notification(iosPayload, androidPayload);

    PushPayload payload =
        PushPayload.newBuilder()
            .setAudience(selector)
            .setNotification(notification)
            .setPlatforms(PlatformData.of(Platform.IOS, Platform.ANDROID))
            .build();

    try {
      APIClientResponse<APIPushResponse> response = apiClient.push(payload);
      System.out.println("Send the message");
      System.out.println(response);
    } catch (APIRequestException ex) {
      System.out.println(String.format("APIRequestException " + ex));
      System.out.println("EXCEPTION " + ex.toString());
      APIError apiError = ex.getError().get();
      System.out.println("Error " + apiError.getError());
      if (apiError.getDetails().isPresent()) {
        APIErrorDetails apiErrorDetails = apiError.getDetails().get();
        System.out.println("Error details " + apiErrorDetails.getError());
      }
    } catch (IOException e) {
      System.out.println("IOException in API request " + e.getMessage());
    }
  }