@Test
  public void testAlert() throws Exception {

    IOSDevicePayload payload = IOSDevicePayload.newBuilder().setAlert("iOS override").build();

    String expected = "\"iOS override\"";

    String json = mapper.writeValueAsString(payload.getAlert().get());

    assertEquals(expected, json);
  }
  @Test
  public void testEmptyAlert() throws Exception {
    IOSDevicePayload payload =
        IOSDevicePayload.newBuilder()
            .setAlert(IOSAlertData.newBuilder().build())
            .setBadge(
                IOSBadgeData.newBuilder().setType(IOSBadgeData.Type.VALUE).setValue(1).build())
            .build();

    String json = mapper.writeValueAsString(payload);

    String expected = "{\"badge\":1}";

    assertEquals(expected, json);
  }
  @Test
  public void testCompoundAlert() throws Exception {

    IOSDevicePayload payload =
        IOSDevicePayload.newBuilder()
            .setAlert(
                IOSAlertData.newBuilder()
                    .setBody("B")
                    .setActionLocKey("ALK")
                    .setLocKey("LK")
                    .setLocArgs(ImmutableList.of("arg1", "arg2"))
                    .setLaunchImage("LI")
                    .build())
            .build();

    String json = mapper.writeValueAsString(payload);

    String expected = "{\"alert\":\"B\"}";

    assertEquals(expected, json);
  }
예제 #4
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());
    }
  }