漂流的老妖怪

导航

 

实体类:

import java.io.Serializable;
import javax.persistence.Entity;

/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : Android的消息体
 * @Date : 2017年11月9日 下午4:26:45
 */
@Entity
public class AndroidMessage implements Serializable{

    private static final long serialVersionUID = -4962099586824544583L;
    private String title;
    private String description;
    private String notification_builder_id;
    private String notification_basic_style;
    private String open_type;
    private String url;
    private String pkg_content;
    /** "custom_content":{"key":"value"};jsonobject转String*/
    private String custom_content;
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getNotification_builder_id() {
        return notification_builder_id;
    }
    public void setNotification_builder_id(String notification_builder_id) {
        this.notification_builder_id = notification_builder_id;
    }
    public String getNotification_basic_style() {
        return notification_basic_style;
    }
    public void setNotification_basic_style(String notification_basic_style) {
        this.notification_basic_style = notification_basic_style;
    }
    public String getOpen_type() {
        return open_type;
    }
    public void setOpen_type(String open_type) {
        this.open_type = open_type;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getPkg_content() {
        return pkg_content;
    }
    public void setPkg_content(String pkg_content) {
        this.pkg_content = pkg_content;
    }
    public String getCustom_content() {
        return custom_content;
    }
    public void setCustom_content(String custom_content) {
        this.custom_content = custom_content;
    }
    @Override
    public String toString() {
        return "AndroidMessage [title=" + title + ", description="
                + description + ", notification_builder_id="
                + notification_builder_id + ", notification_basic_style="
                + notification_basic_style + ", open_type=" + open_type
                + ", url=" + url + ", pkg_content=" + pkg_content
                + ", custom_content=" + custom_content + "]";
    }
}
import java.io.Serializable;
import javax.persistence.Entity;

/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : IOS的消息体APS部分
 * @Date : 2017年11月9日 下午4:26:45
 */
@Entity
public class IOSMessageAps implements Serializable{

    private static final long serialVersionUID = -1266567518675955286L;

    private String alert;
    
    private String sound;
    
    private Integer badge;

    public String getAlert() {
        return alert;
    }

    public void setAlert(String alert) {
        this.alert = alert;
    }

    public String getSound() {
        return sound;
    }

    public void setSound(String sound) {
        this.sound = sound;
    }

    public Integer getBadge() {
        return badge;
    }

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

    @Override
    public String toString() {
        return "IOSMessageAps [alert=" + alert + ", sound=" + sound
                + ", badge=" + badge + "]";
    }
}
import java.io.Serializable;
import javax.persistence.Entity;

/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : IOS消息体
 * @Date : 2017年11月9日 下午4:26:24
 */
@Entity
public class IOSMessage implements Serializable{

    private static final long serialVersionUID = -1919005824363589473L;

    private IOSMessageAps aps;

    private String key1;

    private String key2;

    public IOSMessageAps getAps() {
        return aps;
    }

    public void setAps(IOSMessageAps aps) {
        this.aps = aps;
    }

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }

    public String getKey2() {
        return key2;
    }

    public void setKey2(String key2) {
        this.key2 = key2;
    }

    @Override
    public String toString() {
        return "IOSMessage [aps=" + aps + ", key1=" + key1 + ", key2=" + key2
                + "]";
    }
}
import java.io.Serializable;
import javax.persistence.Entity;


/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : 消息推送注册信息
 * @Date : 2017年11月9日 下午4:27:00
 */
@Entity
public class Notification implements Serializable{

    private static final long serialVersionUID = 3977093167924038038L;

    /**
     * 必须为客户端初始化成功之后返回的channelId,默认null。 唯一对应一台设备
     */
    private String channelId;

    /**
     * 设置消息的有效时间,单位秒,默认3600*5.
     */
    private Integer msgExpires;

    /**
     * 消息类型:0:透传消息 1:通知 默认值为0
     */
    private Integer messageType;

    /**
     * 仅IOS应用推送时使用,默认值为null,取值如下:1:开发状态 2:生产状态
     */
    private Integer deployStatus;

    /**
     * 类别主题:指定的topic变量. 长度限制 1 – 128字节,由数字,字母和下划线组成。
     */
    private String topicId;

    /**
     * 设备类型:1 for web, 2 for pc, 3 for android, 4 for ios, 5 for wp.
     */
    private Integer deviceType;

    /**
     * 设置定时推送时间,必需超过当前时间一分钟,单位秒.
     */
    private Long sendTime;
    
    
    

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public Integer getMsgExpires() {
        return msgExpires;
    }

    public void setMsgExpires(Integer msgExpires) {
        this.msgExpires = msgExpires;
    }

    public Integer getMessageType() {
        return messageType;
    }

    public void setMessageType(Integer messageType) {
        this.messageType = messageType;
    }

    public Integer getDeployStatus() {
        return deployStatus;
    }

    public void setDeployStatus(Integer deployStatus) {
        this.deployStatus = deployStatus;
    }

    public String getTopicId() {
        return topicId;
    }

    public void setTopicId(String topicId) {
        this.topicId = topicId;
    }

    public Integer getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(Integer deviceType) {
        this.deviceType = deviceType;
    }

    public Long getSendTime() {
        return sendTime;
    }

    public void setSendTime(Long sendTime) {
        this.sendTime = sendTime;
    }

    @Override
    public String toString() {
        return "Notification [channelId=" + channelId + ", msgExpires="
                + msgExpires + ", messageType=" + messageType
                + ", deployStatus=" + deployStatus + ", topicId=" + topicId
                + ", deviceType=" + deviceType + ", sendTime=" + sendTime + "]";
    }
}
import java.io.Serializable;
import javax.persistence.Entity;

/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : 需要发送的消息体
 * @Date : 2017年11月9日 下午4:26:45
 */
@Entity
public class SendMessage implements Serializable{

    private static final long serialVersionUID = 5964317197661586233L;
    
    
    private AndroidMessage androidMessage;
    private IOSMessage iosMessage;
    
    
    public AndroidMessage getAndroidMessage() {
        return androidMessage;
    }
    public void setAndroidMessage(AndroidMessage androidMessage) {
        this.androidMessage = androidMessage;
    }
    public IOSMessage getIosMessage() {
        return iosMessage;
    }
    public void setIosMessage(IOSMessage iosMessage) {
        this.iosMessage = iosMessage;
    }
    @Override
    public String toString() {
        return "SendMessage [androidMessage=" + androidMessage
                + ", iosMessage=" + iosMessage + "]";
    }
}

百度推送:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import com.centit.server.webmgr.baidupush.po.AndroidMessage;
import com.centit.server.webmgr.baidupush.po.IOSMessage;
import com.centit.server.webmgr.baidupush.po.IOSMessageAps;
import com.centit.server.webmgr.baidupush.po.Notification;
import com.centit.server.webmgr.baidupush.po.SendMessage;import com.centit.server.webmgr.common.DingTalkProperties;

/**
 * 
 * @Version : 1.0
 * @Author : li_hao
 * @Description : 百度推送
 * @Date : 2017年11月9日 下午4:22:04
 */

public class NotificationPusher {
    
    public static final Log log = LogFactory.getLog(NotificationPusher.class);


    /**
     * 推送消息到单个设备
     */
    public static String pushNotificationToSingleDevice(SendMessage sendMessage, Notification notification) throws PushClientException, PushServerException {
        Integer deviceType = notification.getDeviceType();
        
        String apiKey = null;
        String secretKey = null;
        if(deviceType == 3){
            apiKey = "你的android_api_key";
            secretKey = "你的android_secret_key";
        }else if(deviceType == 4){
            apiKey = "你的ios_api_key";
            secretKey = "你的ios_secret_key";
        }
        
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);

        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                log.info(String.format("推送回调日志:%s", event.getMessage()));
            }
        });

        try {
            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest();
            
            String message = null;
            if(deviceType == 3){
                message = JSON.toJSONString(sendMessage.getAndroidMessage());
            }else if(deviceType == 4){
                message = JSON.toJSONString(sendMessage.getIosMessage());
                
                request.addDeployStatus(notification.getDeployStatus()); // IOS的部署状态:Developer,2:Production.
            }
            
            request.addChannelId(notification.getChannelId());
            request.addMsgExpires(notification.getMsgExpires()); // 设置message的有效时间
            request.addMessageType(notification.getMessageType());// 消息类型:1:通知,0:透传消息.默认为0注:IOS只有通知.
            request.addDeviceType(deviceType); // 设备类型:3:android,4:ios
            
            
            JSONObject jsonMessage = JSONObject.parseObject(message);
            request.addMessage(jsonMessage.toString());

            // 发起请求
            PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice(request);
            // 请求结果解析
            log.info(String.format("推送成功信息:msgId=%s,sendTime=", response.getMsgId(), response.getSendTime()));
            return response.getMsgId();
        } catch (PushClientException e) {
            // ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,'true' 表示抛出, 'false' 表示捕获。
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                log.error(String.format("推送客户端异常:%s" ,e.getMessage()));
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                log.error(String.format("推送服务端异常:requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
        return null;
    }
}

 

posted on 2017-12-22 18:42  漂流的老妖怪  阅读(408)  评论(0编辑  收藏  举报