Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add notification builders

  • Loading branch information...
commit b311a20f119210a582247dba0e63de5be4672111 1 parent b7cf285
@notnoop authored
View
35 src/main/java/com/notnoop/mpns/DeliveryClass.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns;
+
+public enum DeliveryClass {
+ IMMEDIATELY, WITHIN_450, WITHIN_900;
+}
View
16 src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java
@@ -30,6 +30,22 @@
*/
package com.notnoop.mpns;
+import com.notnoop.mpns.notifications.RawNotification;
+import com.notnoop.mpns.notifications.TileNotification;
+import com.notnoop.mpns.notifications.ToastNotification;
+
public class MpnsNotificationBuilder {
+ public MpnsNotificationBuilder() {}
+
+ public TileNotification.Builder tile() {
+ return new TileNotification.Builder();
+ }
+
+ public ToastNotification.Builder toast() {
+ return new ToastNotification.Builder();
+ }
+ public RawNotification.Builder raw() {
+ return new RawNotification.Builder();
+ }
}
View
1  src/main/java/com/notnoop/mpns/MpnsServiceBuilder.java
@@ -40,7 +40,6 @@
import org.apache.http.impl.client.DefaultHttpClient;
import com.notnoop.mpns.internal.*;
-import com.notnoop.mpns.exceptions.RuntimeIOException;
/**
* The class is used to create instances of {@link ApnsService}.
View
59 src/main/java/com/notnoop/mpns/internal/Pair.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns.internal;
+
+import java.util.Map;
+
+public class Pair<K, V> implements Map.Entry<K, V> {
+ public final K key;
+ public final V value;
+
+ public Pair(K k, V v) {
+ this.key = k;
+ this.value = v;
+ }
+
+ public K getKey() {
+ return key;
+ }
+
+ public V getValue() {
+ return value;
+ }
+
+ public V setValue(V value) {
+ throw new UnsupportedOperationException("Pair is immutable!");
+ }
+
+ public static <K, V> Pair<K, V> of(K k, V v) {
+ return new Pair<K, V>(k, v);
+ }
+}
View
2  src/main/java/com/notnoop/mpns/internal/Utilities.java
@@ -88,7 +88,7 @@ public static String escapeXml(String value) {
default: sb.append(ch);
}
}
-
+
return sb.toString();
}
}
View
67 src/main/java/com/notnoop/mpns/notifications/AbstractNotificationBuilder.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2011, Mahmood Ali.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Mahmood Ali. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.notnoop.mpns.notifications;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map.Entry;
+
+import com.notnoop.mpns.DeliveryClass;
+import com.notnoop.mpns.internal.Pair;
+
+@SuppressWarnings("unchecked")
+abstract class AbstractNotificationBuilder<A extends AbstractNotificationBuilder<A, B>, B> {
+ protected List<Entry<String, String>> headers = new ArrayList<Entry<String, String>>();
+
+ public A messageId(String messageId) {
+ this.headers.add(Pair.of("X-MessageId", messageId));
+ return (A)this;
+ }
+
+ public A notificationClass(DeliveryClass delivery) {
+ this.headers.add(Pair.of("X-NotificationClass", String.valueOf(deliveryValueOf(delivery))));
+ return (A)this;
+ }
+
+ public A notificationType(String type) {
+ this.headers.add(Pair.of("X-WindowsPhone-Target", type));
+ return (A)this;
+ }
+
+ public A callbackUri(String callbackUri) {
+ this.headers.add(Pair.of("X-CallbackURI", callbackUri));
+ return (A)this;
+ }
+
+ protected abstract int deliveryValueOf(DeliveryClass delivery);
+
+ protected abstract B build();
+}
View
31 src/main/java/com/notnoop/mpns/notifications/RawNotification.java
@@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map.Entry;
+import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
public class RawNotification implements MpnsNotification {
@@ -55,4 +56,34 @@ public String getRequestBody() {
public List<? extends Entry<String, String>> getHttpHeaders() {
return Collections.unmodifiableList(this.headers);
}
+
+ public static class Builder extends AbstractNotificationBuilder<Builder, RawNotification> {
+ private String xml;
+
+ public Builder() {
+ super();
+ this.notificationType("raw");
+ }
+
+ public Builder xml(String xml) {
+ this.xml = xml;
+ return this;
+ }
+
+ @Override
+ protected int deliveryValueOf(DeliveryClass delivery) {
+ switch (delivery) {
+ case IMMEDIATELY: return 3;
+ case WITHIN_450: return 13;
+ case WITHIN_900: return 23;
+ default:
+ throw new AssertionError("Unknown Value: " + delivery);
+ }
+ }
+
+ @Override
+ protected RawNotification build() {
+ return new RawNotification(xml, headers);
+ }
+ }
}
View
59 src/main/java/com/notnoop/mpns/notifications/TileNotification.java
@@ -34,7 +34,9 @@
import java.util.List;
import java.util.Map.Entry;
+import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
+
import static com.notnoop.mpns.internal.Utilities.ifNonNull;
import static com.notnoop.mpns.internal.Utilities.escapeXml;
@@ -83,4 +85,61 @@ public String getRequestBody() {
public List<? extends Entry<String, String>> getHttpHeaders() {
return Collections.unmodifiableList(this.headers);
}
+
+ public static class Builder extends AbstractNotificationBuilder<Builder, TileNotification> {
+ private String backgroundImage, title, backBackgroundImage, backTitle, backContent;
+ private int count;
+
+ public Builder() {
+ super();
+ this.notificationType("token"); // TODO: Check whether it is "tile"
+ }
+
+ public Builder backgroundImage(String backgroundImage) {
+ this.backgroundImage = backgroundImage;
+ return this;
+ }
+
+ public Builder title(String title) {
+ this.title = title;
+ return this;
+ }
+
+ public Builder backBackgroundImage(String backBackgroundImage) {
+ this.backBackgroundImage = backBackgroundImage;
+ return this;
+ }
+
+ public Builder backTitle(String backTitle) {
+ this.backTitle = backTitle;
+ return this;
+ }
+
+ public Builder backContent(String backContent) {
+ this.backContent = backContent;
+ 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
+ protected TileNotification build() {
+ return new TileNotification(backgroundImage, title, count,
+ backBackgroundImage, backTitle, backContent,headers);
+ }
+ }
}
View
42 src/main/java/com/notnoop/mpns/notifications/ToastNotification.java
@@ -34,7 +34,9 @@
import java.util.List;
import java.util.Map.Entry;
+import com.notnoop.mpns.DeliveryClass;
import com.notnoop.mpns.MpnsNotification;
+
import static com.notnoop.mpns.internal.Utilities.ifNonNull;
import static com.notnoop.mpns.internal.Utilities.escapeXml;
@@ -71,4 +73,44 @@ public String getRequestBody() {
public List<? extends Entry<String, String>> getHttpHeaders() {
return Collections.unmodifiableList(this.headers);
}
+
+ public static class Builder extends AbstractNotificationBuilder<Builder, ToastNotification> {
+ private String title, subtitle, parameter;
+
+ public Builder() {
+ super();
+ this.notificationType("toast");
+ }
+
+ public Builder title(String title) {
+ this.title = title;
+ return this;
+ }
+
+ public Builder subtitle(String subtitle) {
+ this.subtitle = subtitle;
+ return this;
+ }
+
+ public Builder parameter(String parameter) {
+ this.parameter = parameter;
+ return this;
+ }
+
+ @Override
+ protected int deliveryValueOf(DeliveryClass delivery) {
+ switch (delivery) {
+ case IMMEDIATELY: return 2;
+ case WITHIN_450: return 12;
+ case WITHIN_900: return 22;
+ default:
+ throw new AssertionError("Unknown Value: " + delivery);
+ }
+ }
+
+ @Override
+ protected ToastNotification build() {
+ return new ToastNotification(title, subtitle, parameter, headers);
+ }
+ }
}

0 comments on commit b311a20

Please sign in to comment.
Something went wrong with that request. Please try again.