package com.ifish.jpush.push.model.notification; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.ifish.jpush.push.model.PushModel; import com.ifish.jpush.utils.Preconditions; public abstract class PlatformNotification implements PushModel { public static final String ALERT = "alert"; private static final String EXTRAS = "extras"; protected static final Logger LOG = LoggerFactory.getLogger(PlatformNotification.class); private Object alert; private final Map extras; private final Map numberExtras; private final Map booleanExtras; private final Map jsonExtras; public PlatformNotification(Object alert, Map extras, Map numberExtras, Map booleanExtras, Map jsonExtras) { this.alert = alert; this.extras = extras; this.numberExtras = numberExtras; this.booleanExtras = booleanExtras; this.jsonExtras = jsonExtras; } @Override public JsonElement toJSON() { JsonObject json = new JsonObject(); if (null != alert) { if ( alert instanceof JsonObject) { json.add(ALERT, (JsonObject) alert); } else if (alert instanceof IosAlert) { json.add(ALERT, ((IosAlert) alert).toJSON()); } else { json.add(ALERT, new JsonPrimitive(alert.toString())); } } JsonObject extrasObject = null; if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) { extrasObject = new JsonObject(); } if (null != extras) { String value = null; for (String key : extras.keySet()) { value = extras.get(key); if (null != value) { extrasObject.add(key, new JsonPrimitive(value)); } } } if (null != numberExtras) { Number value = null; for (String key : numberExtras.keySet()) { value = numberExtras.get(key); if (null != value) { extrasObject.add(key, new JsonPrimitive(value)); } } } if (null != booleanExtras) { Boolean value = null; for (String key : booleanExtras.keySet()) { value = booleanExtras.get(key); if (null != value) { extrasObject.add(key, new JsonPrimitive(value)); } } } if (null != jsonExtras) { JsonObject value = null; for (String key : jsonExtras.keySet()) { value = jsonExtras.get(key); if (null != value) { extrasObject.add(key, value); } } } if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) { json.add(EXTRAS, extrasObject); } return json; } protected Object getAlert() { return this.alert; } protected void setAlert(Object alert) { this.alert = alert; } protected abstract String getPlatform(); protected abstract static class Builder> { private B theBuilder; protected Object alert; protected Map extrasBuilder; protected Map numberExtrasBuilder; protected Map booleanExtrasBuilder; protected Map jsonExtrasBuilder; public Builder () { theBuilder = getThis(); } protected abstract B getThis(); public abstract B setAlert(Object alert); public B addExtra(String key, String value) { Preconditions.checkArgument(! (null == key), "Key should not be null."); if (null == value) { LOG.debug("Extra value is null, throw away it."); return theBuilder; } if (null == extrasBuilder) { extrasBuilder = new HashMap(); } extrasBuilder.put(key, value); return theBuilder; } public B addExtras(Map extras) { if (null == extras) { LOG.warn("Null extras param. Throw away it."); return theBuilder; } if (null == extrasBuilder) { extrasBuilder = new HashMap(); } for (String key : extras.keySet()) { extrasBuilder.put(key, extras.get(key)); } return theBuilder; } public B addExtra(String key, Number value) { Preconditions.checkArgument(! (null == key), "Key should not be null."); if (null == value) { LOG.debug("Extra value is null, throw away it."); return theBuilder; } if (null == numberExtrasBuilder) { numberExtrasBuilder = new HashMap(); } numberExtrasBuilder.put(key, value); return theBuilder; } public B addExtra(String key, Boolean value) { Preconditions.checkArgument(! (null == key), "Key should not be null."); if (null == value) { LOG.debug("Extra value is null, throw away it."); return theBuilder; } if (null == booleanExtrasBuilder) { booleanExtrasBuilder = new HashMap(); } booleanExtrasBuilder.put(key, value); return theBuilder; } public B addExtra(String key, JsonObject value) { Preconditions.checkArgument(! (null == key), "Key should not be null."); if (null == value) { LOG.debug("Extra value is null, throw away it."); return theBuilder; } if (null == jsonExtrasBuilder) { jsonExtrasBuilder = new HashMap(); } jsonExtrasBuilder.put(key, value); return theBuilder; } public abstract T build(); } }