关闭

百度云推送

932人阅读 评论(0) 收藏 举报
分类:
<pre name="code" class="html"><pre name="code" class="html">    private Map<String, Integer> sendMessage(MessageEntity messageEntity) {
        Map<String, Integer> result = new HashMap<String, Integer>();
        PushMessageForAdmin pushMessage = new PushMessageForAdmin();
        int errorFlag = 2;
        int sum = 0;
        int success = 0;
        int channel = messageEntity.getChannel();

        String title = messageEntity.getTitle();
        String messageString = messageEntity.getMessageString();
        int device = messageEntity.getDevice();
        String channelId = messageEntity.getChannelId();

        //指定设备推送
        if (device == 1) {
            try {


                if (channel == 1) {
                    if (StringUtils.isEmpty(channelId)) {
                        errorFlag = 3;

                    } else {
                        errorFlag = pushMessage.pushMessageToSingleDevice(messageEntity);
                    }
                }
                if (channel == 2) {

                    String[] chaId = messageEntity.getLotChannelId().split("-");
                    if (chaId == null || chaId.length <= 0) {
                        errorFlag = 3;
                    } else {

                        for (String id : chaId) {

                            sum = sum + 1;
                            messageEntity.setChannelId(id);
                            int sendFlag = pushMessage.pushMessageToSingleDevice(messageEntity);
                            if (sendFlag == 1) {
                                errorFlag = 1;
                                success = success + 1;
                            }
                        }
                    }

                }


            } catch (Exception e) {
                logger.error("pushSingle抓住异常");
                errorFlag = 4;

            }
        }


        //所有设备推送
        else if (device == 2) {
            try {
                int sendTime = messageEntity.getSendTime();
                int deviceType = messageEntity.getDeviceType();
                String sdTime = messageEntity.getSdTime();
                Long ts = null;
                if (sendTime == 1) {
                    ts = System.currentTimeMillis() / 1000 + 70;
                } else if (sendTime == 2) {

                    SimpleDateFormat SDF_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    Date pubTime = SDF_1.parse(sdTime);
                    SimpleDateFormat sb2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String t2 = sb2.format(pubTime);
                    ts = Timestamp.valueOf(t2).getTime() / 1000;


                    long subTime = ts - System.currentTimeMillis() / 1000;
                    if (subTime < 70) {
                        ts = System.currentTimeMillis() / 1000 + 70;
                    }

                }
                messageEntity.setTs(ts);

                errorFlag = pushMessage.pushMessageToAll(messageEntity);

            } catch (Exception e) {
                logger.error("pushAll抓住异常");
            }


        }
        result.put("errorFlag", errorFlag);
        result.put("channel", channel);
        result.put("sum", sum);
        result.put("success", success);
        return result;

    }



<pre name="code" class="html">package com.baidu.yunpush;


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.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToAllResponse;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PushMessageForAdmin {

    private static Logger logger = LoggerFactory.getLogger(PushMessageForAdmin.class);

    private static BaiduPushClient pushClient;

    /**
     * 根据ChannelId精准推送  支持android/ios设备
     */
    public static int pushMessageToSingleDevice(MessageEntity message) throws PushClientException, PushServerException {
        pushClient = initPushClientMaple(message);
        try {
            logger.info("推送信息:" + message.toString());

            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
                    .addChannelId(message.getChannelId())
                    .addMsgExpires(new Integer(message.getMsgExpires())). // message有效时间
                    addMessageType(message.getType()).// 1:通知,0:透传消息. 默认为0 注:IOS只有通知.
                    addMessage(message.getMessageString()).
                            addDeviceType(message.getDeviceType()).addDeployStatus(2);// deviceType => 3:android, 4:ios  deployStatus :1开发 2生产
            PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice(request);

            logger.info("推送信息:msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime());
            return 1;
        } catch (PushClientException e) {
            /*
			 * ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,'true' 表示抛出, 'false' 表示捕获。
			 */
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                return 2;
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {

                logger.info("推送信息:" + String.format("requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
                return 2;
            }
        }
    }

    public static int pushMessageToAll(MessageEntity message) throws PushClientException, PushServerException {
        pushClient = initPushClientMaple(message);

        try {
            PushMsgToAllRequest request = new PushMsgToAllRequest()
                    .addMsgExpires(new Integer(message.getMsgExpires())).addMessageType(message.getType())
                    .addMessage(message.getMessageString())
//					.addSendTime(System.currentTimeMillis() / 1000 + 120) // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例2分钟后推送
                    .addSendTime(message.getTs())
                    .addDepolyStatus(1).addDeviceType(message.getDeviceType());


            // 5. http request
            PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
            // Http请求结果解析打印
            logger.info("推送信息:msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime() + ",timerId: " + response.getTimerId());
            return 1;
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                logger.error("pushMessageToAll()异常");
                return 2;
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                logger.info("推送信息:" + String.format(
                        "requestId: %d, errorCode: %d, errorMessage: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
                return 2;
            }
        }
    }


    private static BaiduPushClient initPushClientMaple(MessageEntity message) {

        JSONObject notification = new JSONObject();
        notification.put("type", message.getTypeKey());


        if (message.getDeviceType() == 4) {
            notification.put("message", message.getMessageString());
            pushClient = PushInitForIos.getInstance();
            JSONObject jsonAPS = new JSONObject();
            jsonAPS.put("alert", message.getTitle());
            jsonAPS.put("sound", "ttt");
            notification.put("aps", jsonAPS);

            if (message.getUrlFlag() == 3) {
                notification.put("url", message.getUrl());
            }

        } else {
            notification.put("description", message.getMessageString());
            notification.put("title", message.getTitle());
            if (message.getUrlFlag() == 3) {
                JSONObject jsonCustom = new JSONObject();
                jsonCustom.put("url", message.getUrl());
                notification.put("custom_content", jsonCustom);

            }
            pushClient = PushInitForAndroid.getInstance();
        }
        message.setMessageString(notification.toString());
        return pushClient;
    }


//	public static void main(String[] args) throws PushClientException, PushServerException {
//
//		MessageEntity iosmEntity = new MessageEntity();
//		iosmEntity.setChannelId("4680797241349342110");
//		iosmEntity.setTitle("订单信息");
//		iosmEntity.setTypeKey("orderInfo");
//		iosmEntity.setPlatform("ios");
//		iosmEntity.setMessageString("你好");
//		pushMessageToSingleDevice(iosmEntity);
//
//		MessageEntity androidEntity = new MessageEntity();
//		androidEntity.setChannelId("4193505691417552139");
//		androidEntity.setTitle("订单信息");
//		androidEntity.setTypeKey("orderInfo");
//		androidEntity.setPlatform("android");
//		androidEntity.setMessageString("你好");
//		pushMessageToSingleDevice(androidEntity);
//	}
}




   
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

个推,手机推送api的使用

个推的作用:
  • wxwzy738
  • wxwzy738
  • 2014-04-25 20:50
  • 22370

使用baidu push报错

FATAL EXCEPTION: main 02-25 14:27:52.563: E/AndroidRuntime(21157): java.lang.RuntimeException: Unabl...
  • coffeeco
  • coffeeco
  • 2015-02-25 16:13
  • 2980

理解百度云推送的几个概念

之前我们android项目的IM功能和推送功能使用的是国内的环信,但是项目需要部署到欧洲,欧洲地区对个人隐私数据的保护非常严格,不允许我们将聊天等信息传到环信在国内的服务器上,所以不得不采用别的推送方...
  • aitangyong
  • aitangyong
  • 2015-12-01 17:46
  • 1226

Android推送 百度云推送 入门篇

现在app基本都有推送的功能,于是看了下百度云的推送,官方文档和Demo都很到位,记录下使用过程,目标是利用百度云推送最为服务器写个及时通讯的例子~当然了,这是第一篇入门~ 1、第一步就是在百度开发者...
  • lmj623565791
  • lmj623565791
  • 2014-05-27 20:52
  • 116163

百度云推送--分析与开发详解

一:优势分析 百度云推送,极光推送和个推各自的优势是什么? 极光推送功能优势: 1、高效稳定的推送通知 为合作伙伴提供长连接SDK,开发者的服务消息可以推送到其客户端。使你的应用程序保持高注意力,即使...
  • jun632485820
  • jun632485820
  • 2015-03-12 14:59
  • 1775

百度云推送返回10101

一、吐槽 相信大家对于消息推送这个功能不会陌生吧,现在基本上所有的项目都会有消息推送,今天就来谈下百度消息推送,我之前基于百度云推送提供的API开发,但是遇到了很多问题,简直坑的没边了,这么大的公司...
  • u013491648
  • u013491648
  • 2016-12-22 18:20
  • 287

cordova3.5整合百度云推送PushNotification(1)

http://blog.csdn.net/mansai/article/details/19073913 选择百度云推送理由:专业、稳定、免费(Google GCM在国内有时无法正常使用) 第1步...
  • gundumw100
  • gundumw100
  • 2017-04-10 18:56
  • 531

百度云推送的ios证书配置

发现百度云推送的文档写的还算可以,今天特意转载一下,留着备用。 地址:http://push.baidu.com/doc/ios/ios_cert 附录-申请推送证书 为你的 A...
  • yipanbo
  • yipanbo
  • 2015-07-24 15:23
  • 14374

自有账户体系对接百度云推送必须要弄懂的问题

 同一个设备上的同一个APP多次绑定返回的user_id相同,channel_id也相同。 同一个APP在不同的设备上绑定user_id是不相同的,chan...
  • renminzdb
  • renminzdb
  • 2015-01-27 18:03
  • 8302

百度云推送自定义通知行为

百度云推送自定义行为(Android)最近用公司的项目用到了百度云推送,总的来说百度云推送还是蛮好用的。 官方的文档地址:http://push.baidu.com/doc/guide/faq_an...
  • cen_yuan
  • cen_yuan
  • 2016-08-17 12:10
  • 2680
    个人资料
    • 访问:88870次
    • 积分:2368
    • 等级:
    • 排名:第18173名
    • 原创:83篇
    • 转载:19篇
    • 译文:0篇
    • 评论:21条