Beispiel #1
0
 @Override
 public void testConnection() throws ConnectionException {
   WnsToast toast = new WnsToastBuilder().bindingTemplateToastText01("test").build();
   try {
     // this fails every time due to jax error which is ok
     service.pushToast(
         "s-1-15-2-2411381248-444863693-3819932088-4077691928-1194867744-112853457-373132695",
         toast);
   } catch (ClientHandlerException e) {
     LOG.info("Windows Phone notifier added: " + e.toString());
   }
 }
Beispiel #2
0
  @Override
  public void sendNotification(
      String providerId, Object payload, Notification notification, TaskTracker tracker)
      throws Exception {
    try {
      List<TranslatedNotification> translatedNotifications = (List<TranslatedNotification>) payload;
      for (TranslatedNotification translatedNotification : translatedNotifications) {

        // set the optional TTL value used when pushing notifications
        WnsNotificationRequestOptional opt = new WnsNotificationRequestOptional();
        opt.ttl = String.valueOf(notification.getExpireTTLSeconds());

        switch (translatedNotification.getType()) {
          case "toast":
            WnsToast toast =
                new WnsToastBuilder()
                    .bindingTemplateToastText01(translatedNotification.getMessage().toString())
                    .build();
            service.pushToast(providerId, opt, toast);
            break;
          case "badge":
            WnsBadge badge;
            if (translatedNotification.getMessage() instanceof Integer) {
              badge =
                  new WnsBadgeBuilder()
                      .value((Integer) translatedNotification.getMessage())
                      .build();
            } else {
              badge =
                  new WnsBadgeBuilder()
                      .value(translatedNotification.getMessage().toString())
                      .build();
            }
            service.pushBadge(providerId, opt, badge);
            break;
          case "raw":
            Object message = translatedNotification.getMessage();
            if (message instanceof String) {
              WnsRaw raw = new WnsRawBuilder().stream(((String) message).getBytes()).build();

              // set additional optional parameter for raw notifications
              opt.cachePolicy = "cache";
              opt.requestForStatus = "true";

              WnsNotificationResponse response = service.pushRaw(providerId, opt, raw);
              if (!response.notificationStatus.equals(
                  "received")) { // https://msdn.microsoft.com/en-us/library/windows/apps/hh465435.aspx#pncodes_x_wns_notification
                throw new Exception(
                    String.format(
                        "Notification failed status:%s, devicesStatus:%s, description:%s, debug flag:%s",
                        response.notificationStatus,
                        response.deviceConnectionStatus,
                        response.errorDescription,
                        response.debugTrace));
              }
            } else {
              throw new IllegalArgumentException(
                  "You must send a string in the raw body. instead got this: "
                      + message.getClass().getName());
            }
            break;
          default:
            throw new IllegalArgumentException(
                translatedNotification.getType()
                    + " does not match a valid notification type (toast,badge).");
        }
      }
      tracker.completed();
    } catch (Exception e) {
      tracker.failed(0, e.toString());
      LOG.error("Failed to send notification", e);
    }
  }