All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.yawenok.apns.http2.PayloadBuilder Maven / Gradle / Ivy

package io.github.yawenok.apns.http2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.HashMap;
import java.util.Map;

/**
 * A utility class for constructing APNs JSON payloads.
 *
 * @author yawen
 *
 * @see Generating a Remote Notification
 */
@SuppressWarnings("unchecked")
public class PayloadBuilder {
    private final Map root = new HashMap();
    private final Map aps = new HashMap();
    private final Map alert = new HashMap();

    public PayloadBuilder sound(final String sound) {
        if (sound != null) {
            aps.put("sound", sound);
        } else {
            aps.remove("sound");
        }

        return this;
    }

    public PayloadBuilder category(final String category) {
        if (category != null) {
            aps.put("category", category);
        } else {
            aps.remove("category");
        }
        return this;
    }

    public PayloadBuilder badge(final int badge) {
        aps.put("badge", badge);
        return this;
    }

    public PayloadBuilder alertBody(final String body) {
        alert.put("body", body);
        return this;
    }

    public PayloadBuilder alertTitle(final String title) {
        alert.put("title", title);
        return this;
    }

    public PayloadBuilder alertSubtitle(final String subtitle) {
        alert.put("subtitle", subtitle);
        return this;
    }

    public PayloadBuilder customField(final String key, final Object value) {
        root.put(key, value);
        return this;
    }

    /**
     * The notification service app extension flag.
     * If the value is 1, the system passes the notification to your notification service app extension before delivery.
     * Use your extension to modify the notification’s content. See Modifying Content in Newly Delivered Notifications.
     *
     * @param mutable
     * @return
     *
     * @see Modifying Content in Newly Delivered Notifications
     */
    public PayloadBuilder mutableContent(final boolean mutable) {
        if (mutable) {
            aps.put("mutable-content", 1);
        } else {
            aps.remove("mutable-content");
        }
        return this;
    }

    /**
     * The background notification flag.
     * To perform a silent background update, specify the value 1 and don't include the alert, badge, or sound keys in your payload.
     *
     * @param content
     * @return
     *
     * @see Pushing Updates to Your App Silently
     */
    public PayloadBuilder contentAvailable(final boolean content) {
        if (content) {
            aps.put("content-available", 1);
        } else {
            aps.remove("content-available");
        }
        return this;
    }

    public String build() {
        Map buildRoot = new HashMap<>(root.size());
        buildRoot.putAll(root);

        Map buildAps = new HashMap<>(aps.size());
        buildAps.putAll(aps);

        Map buildAlert = new HashMap<>(alert.size());
        buildAlert.putAll(alert);

        // 组装数据
        buildAps.put("alert", buildAlert);
        buildRoot.put("aps", buildAps);

        return JSON.toJSONString(buildRoot, SerializerFeature.DisableCircularReferenceDetect);
    }

    @Override
    public String toString() {
        return this.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy