后台app个推

做项目会用到把消息推送给app


public class AppMessagePush {

    private static String appId = MyProperties.getByKey("appId");;
    private static String appKey = MyProperties.getByKey("appKey");;
    private static String masterSecret = MyProperties.getByKey("masterSecret");;


    public static void sendMessage(PushMsg msg) {
        if (msg.getType().equals("android")) {
            ITemplate notificationTemplate = notificationTemplateDemo(
                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());
            pushSingleMessage(msg.getCid(), notificationTemplate, false);
            ITemplate iosTransmissionTemplate = iosTransmissionTemplate(
                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload
            pushSingleMessage(msg.getCid(), iosTransmissionTemplate, false);
        } else if (msg.getType().equals("ios")) {
            // ios透传通知 安卓透传
            ITemplate iosTransmissionTemplate = iosTransmissionTemplate(
                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload
            pushSingleMessage(msg.getCid(), iosTransmissionTemplate, true);
        }
    }

    // 单个推送
    private static void pushSingleMessage(String Cid, ITemplate template,
            boolean offline) {
        IGtPush push = new IGtPush(appKey, masterSecret, true);

        SingleMessage message = new SingleMessage();

        message.setOffline(offline);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(Cid);
        IPushResult ret = null;

        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
    }

    private static NotificationTemplate notificationTemplateDemo(String title,
            String messageInfo, String badge) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 设置通知栏标题与内容
        template.setTitle(title);
        template.setText(messageInfo);
        // 配置通知栏图标
        template.setLogo("icon.png");
        // 配置通知栏网络图标
        template.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        template.setAPNInfo(getApnPayload(title, messageInfo, badge));
        template.setTransmissionType(2);
        template.setTransmissionContent("notification." + messageInfo);
        return template;
    }

    // ios透传,设置APNPayload参数
    private static TransmissionTemplate iosTransmissionTemplate(String title,
            String messageInfo, String badge) {
        TransmissionTemplate template = transmissionTemplate(messageInfo);
        APNPayload payload = getApnPayload(title, messageInfo, badge);
        // 字典模式使用下者
        // payload.setAlertMsg(getDictionaryAlertMsg());
        template.setAPNInfo(payload);
        return template;
    }

    private static APNPayload getApnPayload(String title, String messageInfo,
            String badge) {
        APNPayload payload = new APNPayload();
        // +1在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
        payload.setAutoBadge(badge);
        payload.setContentAvailable(1);
        payload.setSound("123.wav");
        payload.setCategory("$由客户端定义");
        // //简单模式APNPayload.SimpleMsg
        // payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo));
        DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg();
        dictionaryAlertMsg.setTitle(title);
        dictionaryAlertMsg.setBody(messageInfo);
        payload.setAlertMsg(dictionaryAlertMsg);
        return payload;
    }

    // 透传消息
    private static TransmissionTemplate transmissionTemplate(String messageInfo) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(messageInfo);
        template.setTransmissionType(2);
        return template;
    }

}
package com.park.util.push;

public class PushMsg {

    private String cid;
    private String type;
    private String title;
    private String messageInfo;
    private String badge;

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessageInfo() {
        return messageInfo;
    }

    public void setMessageInfo(String messageInfo) {
        this.messageInfo = messageInfo;
    }

    public String getBadge() {
        return badge;
    }

    public void setBadge(String badge) {
        this.badge = badge;
    }

    public static PushMsg initial(String cid, String type, String title,
            String messageInfo,String badge) {
        PushMsg msg = new PushMsg();
        msg.setCid(cid);
        msg.setType(type);
        msg.setTitle(title);
        msg.setMessageInfo(messageInfo);
        msg.setBadge(badge);
        return msg;
    }
}

在项目上调用就好了

                PushMsg msg = PushMsg.initial(clientId, "ios", msgTitle, msgContent, badge + "");
                AppMessagePush.sendMessage(msg);

你可能感兴趣的:(java)