From 2c7c208bc4f09a6bc3bef7444f9882dff74cc93c Mon Sep 17 00:00:00 2001 From: PeeyushBsb Date: Thu, 18 Apr 2013 16:13:26 +0530 Subject: [PATCH] Added IconicTileNotification --- .../notnoop/mpns/MpnsNotificationBuilder.java | 10 + .../notifications/IconicTileNotification.java | 172 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 src/main/java/com/notnoop/mpns/notifications/IconicTileNotification.java diff --git a/src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java b/src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java index 3d4c23e..92a05b3 100644 --- a/src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java +++ b/src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java @@ -30,6 +30,7 @@ */ package com.notnoop.mpns; +import com.notnoop.mpns.notifications.IconicTileNotification; import com.notnoop.mpns.notifications.RawNotification; import com.notnoop.mpns.notifications.TileNotification; import com.notnoop.mpns.notifications.ToastNotification; @@ -61,6 +62,15 @@ public TileNotification.Builder tile() { public ToastNotification.Builder toast() { return new ToastNotification.Builder(); } + + /** + * Sets the notification type to a Iconic Tile notification + * + * @return a iconic tile notification builder + */ + public IconicTileNotification.Builder iconicTile() { + return new IconicTileNotification.Builder(); + } /** * Sets the notification type to a Raw notification diff --git a/src/main/java/com/notnoop/mpns/notifications/IconicTileNotification.java b/src/main/java/com/notnoop/mpns/notifications/IconicTileNotification.java new file mode 100644 index 0000000..d0cc6db --- /dev/null +++ b/src/main/java/com/notnoop/mpns/notifications/IconicTileNotification.java @@ -0,0 +1,172 @@ +package com.notnoop.mpns.notifications; + +import static com.notnoop.mpns.internal.Utilities.escapeXml; +import static com.notnoop.mpns.internal.Utilities.ifNonNull; + +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; + +import com.notnoop.mpns.DeliveryClass; +import com.notnoop.mpns.MpnsNotification; +import com.notnoop.mpns.internal.Utilities; + +public class IconicTileNotification implements MpnsNotification { + + private final String tileId; + private final String title; + private final int count; + + private final String backgroundColor; + private final String iconImage; + private final String smallIconImage; + private final String firstRowOfContent; + private final String secondRowOfContent; + private final String thirdRowOfContent; + + private final boolean isclear; + + private final List> headers; + + public IconicTileNotification(String tileId, String title, int count, + String backgroundColor, String iconImage, String smallIconImage, + String firstRowOfContent, String secondRowOfContent, + String thirdRowOfContent, boolean isclear, + List> headers) { + this.tileId = tileId; + this.title = title; + this.count = count; + + this.backgroundColor = backgroundColor; + this.iconImage = iconImage; + this.smallIconImage = smallIconImage; + this.firstRowOfContent = firstRowOfContent; + this.secondRowOfContent = secondRowOfContent; + this.thirdRowOfContent = thirdRowOfContent; + + this.isclear = isclear; + + this.headers = headers; + } + + public byte[] getRequestBody() { + String tileMessage; + if(isclear) { + tileMessage = + "" + + "" + + "" + + ifNonNull(smallIconImage, "" + escapeXml(smallIconImage) + "") + + ifNonNull(iconImage, "" + escapeXml(iconImage) + "") + + "" + + "" + + "" + + ifNonNull(count, "" + count + "") + + ifNonNull(title, "" + escapeXml(title) + "") + + ifNonNull(backgroundColor, "" + escapeXml(backgroundColor) + "") + + " " + + ""; + } else { + tileMessage = + "" + + "" + + "" + + ifNonNull(smallIconImage, "" + escapeXml(smallIconImage) + "") + + ifNonNull(iconImage, "" + escapeXml(iconImage) + "") + + ifNonNull(firstRowOfContent, "" + escapeXml(firstRowOfContent) + "") + + ifNonNull(secondRowOfContent, "" + escapeXml(secondRowOfContent) + "") + + ifNonNull(thirdRowOfContent, "" + escapeXml(thirdRowOfContent) + "") + + ifNonNull(count, "" + count + "") + + ifNonNull(title, "" + escapeXml(title) + "") + + ifNonNull(backgroundColor, "" + escapeXml(backgroundColor) + "") + + " " + + ""; + } + + return Utilities.toUTF8(tileMessage); + } + + public List> getHttpHeaders() { + return Collections.unmodifiableList(this.headers); + } + + public static class Builder extends AbstractNotificationBuilder { + private String tileId, title, backgroundColor, iconImage, smallIconImage, firstRowOfContent, secondRowOfContent, thirdRowOfContent; + private int count; + private boolean isClear = false; + + public Builder() { + super("token"); // TODO: Check whether it is "tile" + contentType(Utilities.XML_CONTENT_TYPE); + } + + public Builder backgroundColor(String backgroundColor) { + this.backgroundColor = backgroundColor; + return this; + } + + public Builder title(String title) { + this.title = title; + return this; + } + + public Builder tileId(String tileId) { + this.tileId = tileId; + return this; + } + + public Builder iconImage(String iconImage) { + this.iconImage = iconImage; + return this; + } + + public Builder firstRowOfContent(String firstRowOfContent) { + this.firstRowOfContent = firstRowOfContent; + return this; + } + + public Builder secondRowOfContent(String secondRowOfContent) { + this.secondRowOfContent = secondRowOfContent; + return this; + } + + public Builder thirdRowOfContent(String thirdRowOfContent) { + this.thirdRowOfContent = thirdRowOfContent; + return this; + } + + public Builder smallIconImage(String smallIconImage) { + this.smallIconImage = smallIconImage; + return this; + } + + public Builder clear(boolean clear) { + this.isClear = clear; + return this; + } + + public Builder count(int count) { + this.count = count; + return this; + } + + @Override + protected int deliveryValueOf(DeliveryClass delivery) { + switch (delivery) { + case IMMEDIATELY: return 1; + case WITHIN_450: return 11; + case WITHIN_900: return 21; + default: + throw new AssertionError("Unknown Value: " + delivery); + } + } + + @Override + public IconicTileNotification build() { + return new IconicTileNotification(tileId, title, count, + backgroundColor, iconImage, smallIconImage, firstRowOfContent, + secondRowOfContent, thirdRowOfContent, isClear, headers); + } + } + +}