Java SDK简介

为方便开发者使用服务端Java SDK,本文对SDK中主要接口的功能、参数及属性、返回值,以及SDK与服务端交互过程中存在的错误进行介绍。另外,为使开发者能够较快的熟悉和使用SDK接口,给出了pushMsgToSingleDevice接口的调用流程实例。如果开发者想了解更多接口的使用方法,可查看com.baidu.yun.push.sample包中的实例。

常见异常和错误码

用户使用SDK与后端server端交互中,通常存在多种错误。依据交互的成功与否,将错误分阶段进行梳理,并给出相应的错误码、错误信息和相关描述,以便分析问题时进行查找。

SDK抛出的异常

交互失败时,常见的原因包括SDK参数不完整、网络错误、server端返回错误的json串导致解析异常等。针对这些错误,SDK抛出异常,抛出的异常有PushClientException,PushServerException和YunHttpClientException,其中包含的错误信息,开发者可以查看。

错误码

交互成功,REST API返回的Http Status Code非200时,server端将返回错误的提示信息,具体见错误码表

快速入门

快速入门帮组用户快速使用Java sdk,熟悉API接口的调用流程。具体见Java sdk 快速入门

Java SDK API

公共类

公共类是开发者使用Java SDK 接口时,必须首先初始化和使用的类型。包括PushKeyPair和BaiduPushClient两种类型。

PushKeyPair

该类用于接收分配给开发者app的apiKey 和 secretKey。其构造函数的参数定义如下:

参数名 类型 必需 限制 描述
apiKey String 访问令牌,可通过该值获得开发者app的信息
secretKey String 与apiKey成对出现,用于app的合法身份验证

BaiduPushClient

该类提供了所有的面向用户使用的接口。该类的构造函数有一个参数,即类PushKeyPair的对象实例。

参数名 类型 必需 限制 描述
pair PushKeyPair 必须是apiKey和secretKey构造的对象 包含apiKey和secretKey成员
host String 建议使用推荐的域名 访问域名

注:目前可使用的域名为
api.push.baidu.com;api.tuisong.baidu.com;channel.api.duapp.com(老域名), 建议使用前两个新域名。

getRequestId

获取本次与server端交互的请求id,该值由WebServer返回给用户,当交互失败时,可通过该id进行问题追踪和定位。该接口属于PushServerException类。

原型
public long getRequestId ();

getErrorCode

SDK 与 服务交互成功,但REST API返回的Http Status Code非200,此时,server给出的错误码。该接口属于PushServerException类。

原型
public int getErrorCode ();

getErrorMsg

SDK 与 server端交互成功,但REST API返回的Http Status Code非200,此时,server给出的错误信息。该接口属于PushServerException类,常与getErrorCode配对使用。

原型
public String getErrorMsg ();

pushMsgToSingleDevice

向单个设备推送消息。

原型
public PushMsgToSingleDeviceResponse pushMsgToSingleDevice (PushMsgToSingleDeviceRequest request) throws PushClientException,PushServerException;
参数
PushMsgToSingleDeviceRequest类的实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
channelId String 必须为客户端初始化成功之后返回的channelId,默认null。 唯一对应一台设备
msgType Integer 十进制取值如下:
0:透传消息
1:通知
默认值为0
消息类型
message String 详细见消息/通知格式定义,默认null。 消息内容,json格式,详见通知数据格式
msgExpires Integer 取值:(0, 86400 x 7],默认值为3600 x 5 相对于当前时间的消息过期时间,单位为秒
deployStatus Integer 仅IOS应用推送时使用,默认值为null,
取值如下:
1:开发状态
2:生产状态
topicId String 指定的topic变量. 长度限制 1 – 128字节,
由数字,字母和下划线组成。
类别主题
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回PushMsgToSingleDeviceResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
msgId String 消息id
sendTime long unix timestamp,消息发送时间,
定时消息为消息待发送时间。
接口调用实例
package com.baidu.yun.push.sample;

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;

public class PushMsgToSingleDevice {
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        /*1. 创建PushKeyPair
         *用于app的合法身份认证
         *apikey和secretKey可在应用详情中获取
         */
        String apiKey = “xxxxxxxxxxxxxxxxxxxxxx”;
        String secretKey = “xxxxxxxxxxxxxxxxxxxxxx”;
        PushKeyPair pair = new PushKeyPair(apiKey,secretKey);

        // 2. 创建BaiduPushClient,访问SDK接口
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. 注册YunLogHandler,获取本次请求的交互信息
        pushClient.setChannelLogHandler (new YunLogHandler () {
            @Override
            public void onHandle (YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
        // 4. 设置请求参数,创建请求实例
            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest().
                addChannelId("xxxxxxxxxxxxxxxxxx").
                addMsgExpires(new Integer(3600)).   //设置消息的有效时间,单位秒,默认3600*5.
                addMessageType(1).              //设置消息类型,0表示透传消息,1表示通知,默认为0.
                add("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\"}").
                addDeviceType(3);      //设置设备类型,deviceType => 1 for web, 2 for pc, 
                                       //3 for android, 4 for ios, 5 for wp.
        // 5. 执行Http请求
            PushMsgToSingleDeviceResponse response = pushClient.
                pushMsgToSingleDevice(request);
        // 6. Http请求返回值解析
            System.out.println("msgId: " + response.getMsgId()
                    + ",sendTime: " + response.getSendTime());
        } catch (PushClientException e) {
            //ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,
            //'true' 表示抛出, 'false' 表示捕获。
            if (BaiduPushConstants.ERROROPTTYPE) { 
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}   
  • 失败
    通过设置BaiduPushConstants.ERROROPTTYPE决定SDK抛出或捕获异常。该变量设置为true时,抛出异常,由开发者处理该异常;设置为false时,捕获异常,如果是PushClientException,则打印异常信息,如果是PushServerException,则打印错误码和错误信息。以下每个接口同理。
返回值解析后样例
msgId: 1845554807597775963,sendTime: 1426482263

pushMsgToAll

推送消息给所有设备,即广播推送。

原型
public PushMsgToAllResponse pushMsgToAll (PushMsgToAllRequest request) throws PushClientException,PushServerException;
参数
PushMsgToAllRequest类的实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
msgType Integer 取值如下:
0:透传消息
1:通知
默认值为0
消息类型
message String 详细见消息/通知格式定义 消息内容,json格式,详见通知数据格式
msgExpires Integer 取值:(0, 86400 x 7],默认值为3600 x 5 相对于当前时间的消息过期时间,单位为秒
deployStatus Integer 仅IOS应用推送时使用,默认值为null,
取值如下:
1:开发状态
2:生产状态
sendTime Long 待发送时间戳,必须在当前时间60s以外,1年(31536000s)以内,默认为null。 用于定时消息推送
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回PushMsgToAllResponse类实例对象。该类包含的成员变量:

变量 类型 必需 描述
msgId String 消息id
timerId String 推送定时消息时,返回该字段,
标识定时任务。
sendTime long unix timestamp,消息发送时间,
定时消息为消息待发送时间。
接口调用实例
package com.baidu.yun.push.sample;

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.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToAllResponse;

public class PushMsgToAll {  
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            PushMsgToAllRequest request = new PushMsgToAllRequest()
                    .addMsgExpires(new Integer(3600)).addMessageType(0)
                    .add("Hello Baidu Push.")
                    // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例70秒后推送
                    .addSendTime(System.currentTimeMillis() / 1000 + 70).
                    addDeviceType(3);
            // 5. http request
            PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
            // Http请求返回值解析
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: "    
                    + response.getSendTime() + ",timerId: "
                    + response.getTimerId());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例

非定时任务

msgId: 5260418817295928211,sendTime: 1426498139,timerId: null

定时任务

msgId: 919224673810388259,sendTime: 1426498320,timerId: 2149178636194514179

pushMsgToTag

向绑定到tag中的用户推送消息,即普通组播。

原型
public PushMsgToTagResponse pushMsgToTag (PushMsgToTagRequest request) throws PushClientException,PushServerException;
参数
PushMsgToTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 1~128字节。注:tag的值
不允许为"default",否则判错。
标签名
msgType Integer 取值如下:
0:透传消息
1:通知
默认值为0
消息类型
message String 消息内容 详见通知数据格式
msgExpires Integer 取值:(0, 86400 x 7],默认值为3600 x 5 相对于当前时间的消息过期时间,单位为秒
deployStatus Integer 仅IOS应用推送时使用,默认值为null,
取值如下:
1:开发状态
2:生产状态
sendTime Long 待发送时间戳,必须在当前时间60s以外,1年(31536000s)以内,默认为null。 定时消息推送时间
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

    返回PushMsgToTagResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
msgId String 消息id
timerId String 推送定时消息时,返回该字段,
标识定时任务。
sendTime long unix timestamp,消息发送时间,
定时消息为消息待发送时间。
接口调用实例
package com.baidu.yun.push.sample;

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.PushMsgToTagRequest;
import com.baidu.yun.push.model.PushMsgToTagResponse;

public class PushMsgToTag {  
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            // pushTagTpye = 1 for common tag pushing
            PushMsgToTagRequest request = new PushMsgToTagRequest()
                    .addTagName("xxxxx")
                    .addMsgExpires(new Integer(3600))
                    .addMessageType(1)
                    // .addSendTime(System.currentTimeMillis() / 1000 + 70).
                    .addMessage("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\"}")
                    .addDeviceType(3);
            // 5. http request
            PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
            // Http请求返回值解析
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
                    + response.getSendTime() + ",timerId: "
                    + response.getTimerId());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }   
}
返回值的解析样例

非定时任务

msgId: 102696170400890979,sendTime: 1426496078,timerId: null

定时任务

msgId: 3595081265201046643,sendTime: 1426496348,timerId: 1449208101943652843

pushMsgToSmartTag

推送消息给指定的标签组。注:目前开放组合运算功能,仅对Android平台有效。

原型
public PushMsgToSmartTagResponse pushMsgToSmartTag (PushMsgToSmartTagRequest request) throws PushClientException,PushServerException;
参数
PushMsgToSmartTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
selector String 组合运算表达式 格式见实例
msgType Integer 取值如下:
0:透传消息
1:通知
默认值为0
消息类型
message String 消息内容 详见通知数据格式
msgExpires Integer 取值:(0, 86400 x 7],默认值为3600 x 5 相对于当前时间的消息过期时间,单位为秒
deployStatus Integer 仅IOS应用推送时使用,默认值为null,
取值如下:
1:开发状态
2:生产状态
sendTime Long 待发送时间戳,必须在当前时间60s以外,1年(31536000s)以内,默认为null。 用于定时消息推送
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回PushMsgToSmartTagResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
msgId String 消息id
timerId String 推送定时消息时,返回该字段,
标识定时任务。
sendTime long unix timestamp,消息发送时间,
定时消息为消息待发送时间。
接口调用实例
package com.baidu.yun.push.sample;

import net.sf.json.JSONArray;
import net.sf.json.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.PushMsgToSmartTagRequest;
import com.baidu.yun.push.model.PushMsgToSmartTagResponse;

public class PushMsgToSmartTag {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {               
            // tag operation pushing
            // example.1 {"OR":{"tag":["xxxx","xxxx"]}}
            JSONObject selector = new JSONObject();
            JSONObject tmpJson = new JSONObject();
            JSONArray tagArray = new JSONArray();
            tagArray.add(0, "xxxxx");
            tagArray.add(1, "xxxxx");
            tmpJson.put("tag", tagArray);
            selector.put("OR", tmpJson); // "AND":交,"OR":并,"DIFF":差

            // example.2 {"OR":[{"tag":"xxxx"},{"tag":"xxxx"}]}
            // JSONObject firstTag = new JSONObject();
            // firstTag.put("tag", "xxxx");
            // JSONObject secondTag = new JSONObject();
            // secondTag.put("tag", "xxxx");
            // JSONArray tagArray = new JSONArray();
            // tagArray.add(0, firstTag);
            // tagArray.add(1, secondTag);
            // selector.put("OR", tagArray);

            PushMsgToSmartTagRequest request = new PushMsgToSmartTagRequest()
                    .addSelector(selector.toString())
                    .addMsgExpires(new Integer(3600))
                    .addMessageType(1)
                    .addMessage("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\"}")
                    .addDeviceType(3);
            // 5. http request
            PushMsgToSmartTagResponse response = pushClient
                    .pushMsgToSmartTag(request);
            // Http请求返回值解析
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
                    + response.getSendTime() + ",timerId: "
                    + response.getTimerId());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例

非定时任务

msgId: 363098401823324515,sendTime: 1426498450,timerId: null

定时任务

msgId: 7817030162251044755,sendTime: 1426498712,timerId: 4492866703711133835

pushBatchUniMsg

推送消息给批量设备(批量单播)。

原型
public PushBatchUniMsgResponse pushBatchUniMsg (PushBatchUniMsgRequest request) throws PushClientException,PushServerException;
参数
PushBatchUniMsgRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
channelIds String 内容为JSONArray格式,多个设备的channelId,格式为:[channelId1,channelId2,…],最多一万个
msgType Integer 0:透传消息, 1:通知,默认值为0 消息类型
message String 消息内容 详见通知数据格式
msgExpires Integer 取值:(0, 86400 x 7],默认值为3600 x 5 相对于当前时间的消息过期时间,单位为秒
topicId String 1~128字节 类别主题
deviceType Integer 3:Android 设备类型
返回值
  • 成功

    返回PushBatchUniMsgResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
msgId String 消息id
sendTime long unix timestamp,消息发送时间,
定时消息为消息待发送时间
接口调用实例
package com.baidu.yun.push.sample;

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.PushBatchUniMsgRequest;
import com.baidu.yun.push.model.PushBatchUniMsgResponse;

public class PushBatchUniMsg {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            String[] channelIds = { "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxx" };
            PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
                    .addChannelIds(channelIds)
                    .addMsgExpires(new Integer(3600))
                    .addMessageType(1)
                    .addMessage("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\"}")
                    .addDeviceType(3).addTopicId("BaiduPush");// 设置类别主题
            // 5. http request
            PushBatchUniMsgResponse response = pushClient
                    .pushBatchUniMsg(request);
            // Http请求返回值解析
            System.out.println(String.format("msgId: %s, sendTime: %d",
                    response.getMsgId(), response.getSendTime()));
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
msgId: 8359916630390886579, sendTime: 1426497933

queryMsgStatus

查询消息推送状态,包括成功、失败、待发送、发送中4种状态。

原型
public QueryMsgStatusResponse queryMsgStatus (QueryMsgStatusRequest request) throws PushClientException,PushServerException;
参数
QueryMsgStatusRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
msgId String 推送接口返回的msg_id或是msg_id的数组。
当查询单个msg的状态时,传入消息的String 类型的id,
当查询批量消息的状态时,需要传入消息id的String数组。
消息id
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryMsgStatusResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
totalNum int 消息推送的个数
msgSendInfos List<MsgSendInfo> 消息推送后的状态

类MsgSendInfo的成员变量:

变量 类型 必需 描述
msgId String 消息ID
msgStatus Integer 取值如下:
0:已发送
1:待发送
2:正在发送
3:失败
sendTime long unix timestamp
successCount Integer 成功到达数
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.KeyValueForAck;
import com.baidu.yun.push.model.MsgSendInfo;
import com.baidu.yun.push.model.QueryMsgStatusRequest;
import com.baidu.yun.push.model.QueryMsgStatusResponse;

public class QueryMsgStatus {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            String[] msgIds = { "xxxxxxxxxxxxxxxxxx" };
            QueryMsgStatusRequest request = new QueryMsgStatusRequest()
                    .addMsgIds(msgIds)
                    .addDeviceType(3);
            // 5. http request
            QueryMsgStatusResponse response = pushClient
                    .queryMsgStatus(request);
            // Http请求返回值解析
            System.out.println("totalNum: " + response.getTotalNum() + "\n"
                    + "result:");
            if (null != response) {
                List<?> list = response.getMsgSendInfos();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    if (object instanceof MsgSendInfo) {
                        MsgSendInfo msgSendInfo = (MsgSendInfo) object;
                        StringBuilder strBuilder = new StringBuilder();
                        strBuilder.append("List[" + i + "]: {" + "msgId = "
                                + msgSendInfo.getMsgId() + ",status = "
                                + msgSendInfo.getMsgStatus() + ",sendTime = "
                                + msgSendInfo.getSendTime() + ",successCount = "
                                + msgSendInfo.getSuccessCount());
                        strBuilder.append("}\n");
                        System.out.println(strBuilder.toString());
                    }
                }
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
totalNum: 4
result:
List[0]: {msgId = 8703472462034295699,status = 0,sendTime = 1426499473,successCount = 2}
List[1]: {msgId = 1149561062811585027,status = 0,sendTime = 1426499489,successCount = 5}
List[2]: {msgId = 6659987791096849155,status = 0,sendTime = 1426499501,successCount = 63}
List[3]: {msgId = 5578973573761499459,status = 0,sendTime = 1426499512,successCount = 3}

queryTimerRecords

根据 timerId,获取一个定时的消息推送记录。

原型
public QueryTimerRecordsResponse queryTimerRecords (QueryTimerRecordsRequest request) throws PushClientException,PushServerException;
参数
QueryTimerRecordsRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
timerId String 推送接口返回的timer_id 定时任务id
start Integer 32位整数,默认值为0 起始索引位置
limit Integer 整数:[1,100],默认值为100 一次查询的记录条数
rangeStart Long 指定查询起始时间范围,以消息记录中sendTime为准,
默认取值:现在所在时间往前数七天的0点时间戳。
unix timestamp
rangeEnd Long 指定查询时间范围,以消息记录中sendTime为准,
默认时间为当前时间
unix timestamp
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryTimerRecordsResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
timerId String 定时任务id
timerRecords List<Record> timerRecords属性是List,
内部存放该timer触发的
所有消息状态。

类Record的成员变量如下:

变量 类型 必需 描述
msgId String 消息Id
status Integer 取值如下:
0:已发送
1:待发送
2:正在发送
3:失败
sendTime long unix timestamp
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.QueryTimerRecordsRequest;
import com.baidu.yun.push.model.QueryTimerRecordsResponse;
import com.baidu.yun.push.model.Record;

public class QueryTimerRecords {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryTimerRecordsRequest request = new QueryTimerRecordsRequest()
                    .addTimerId("xxxxxxxxxxxx").addStart(0).// 设置索引起始位置
                    addLimit(10).// 设置查询记录条数
                    // addRangeStart(new Long(xxxxxxxx)).//设置查询开始时间
                    // addRangeEnd(System.currentTimeMillis() / 1000).//设置查询结束时间
                    addDeviceType(3);
            // 5. http request
            QueryTimerRecordsResponse response = pushClient
                    .queryTimerRecords(request);
            // Http请求返回值解析
            System.out.println("timerId: " + response.getTimerId()
                    + "\nresult: \n");
            if (null != response) {
                List<?> list = response.getTimerRecords();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    if (object instanceof Record) {
                        Record record = (Record) object;
                        StringBuilder strBuilder = new StringBuilder();
                        strBuilder.append("List[" + i + "]: {" + "msgId = "
                                + record.getMsgId() + ",status = "
                                + record.getMsgStatus() + ",sendTime = "
                                + record.getSendTime() + "}");
                        System.out.println(strBuilder.toString());
                    }
                }
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
timerId: 6824860712119335531
result: 
List[0]: {msgId = 8703472462034295699,status = 0,sendTime = 1426499473}

queryTopicRecords

根据 topic_id, 获取相应时间范围内的消息推送记录。

原型
public QueryTopicRecordsResponse queryTopicRecords (QueryTopicRecordsRequest request) throws PushClientException,PushServerException;
参数
QueryTopicRecordsRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
topicId String 指定的topic变量. 长度限制 1 – 128字节,
由数字,字母和下划线组成。
统计一批单播或一次批量单播的到达情况
start Integer 32位整数,默认值为0 起始索引位置
limit Integer 整数:[1,100],默认值为100 一次查询的记录条数
rangeStart Long 指定查询起始时间范围,以消息记录中sendTime为准, default: 现在所在时间前数七天的0点时间戳 unix timestamp
rangeEnd Long 指定查询时间范围,以消息记录中sendTime为准, default: 现在时刻 unix timestamp
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryTopicRecordsResponse类实例对象,该类包含的成员变量:

变量 类型 必需 描述
topicId String 定时任务id
topicRecords List<Record> topicRecords属性是List,
批量消息的到达状态。

类Record的成员变量如下:

变量 类型 必需 描述
msgId String 消息id
status Integer 取值:
0:已发送
1:待发送
2:正在发送
3:失败
sendTime long unix timestamp
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.QueryTopicRecordsRequest;
import com.baidu.yun.push.model.QueryTopicRecordsResponse;
import com.baidu.yun.push.model.Record;

public class QueryTopicRecords {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryTopicRecordsRequest request = new QueryTopicRecordsRequest()
                    .addTopicId("hellopush").addStart(0).addLimit(5).
                    // addRangeStart(new Long(xxxxxxxx)).
                    // addRangeEnd(System.currentTimeMillis() / 1000).
                    addDeviceType(3);
            // 5. http request
            QueryTopicRecordsResponse response = pushClient
                    .queryTopicRecords(request);
            // Http请求返回值解析
            StringBuilder strBuilder = new StringBuilder();
            if (null != response) {
                strBuilder.append("topicId: " + response.getTopicId() + "\n"
                        + "result:{");
                List<?> list = response.getTopicRecords();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    if (i != 0) {
                        strBuilder.append(",");
                    }
                    if (object instanceof Record) {
                        Record record = (Record) object;
                        strBuilder
                                .append("{msgId: " + record.getMsgId()
                                        + ", status: " + record.getMsgStatus()
                                        + ", sendTime: " + record.getSendTime()
                                        + "}\n");
                    }
                }
                strBuilder.append("}");
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
topicId: BaiduPush
result:{
{msgId: 2737902026941133811, status: 0, sendTime: 1426557100}
,{msgId: 8359916630390886579, status: 0, sendTime: 1426497933}
}

queryTimerList

查询定时推送任务信息列表。目前,每个应用可设置10个有效的定时任务。

原型
public QueryTimerListResponse queryTimerList (QueryTimerListRequest request) throws PushClientException,PushServerException;
参数
QueryTimerListRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
timerId String 推送接口返回的timerId 标示一个定时推送任务
start Integer 32位整数,默认值为0 起始索引位置
limit Integer 整数,范围[1,100],默认值为10 一次查询的记录条数
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryTimerListResponse类实例对象,该类包含的成员变量:

变量 类型 描述
totalNum int 定时推送任务的总数量
timerResultInfos List<TimerResultInfo> timers属性是List,内部存放
该timer触发的所有消息状态。

类TimerResultInfo的成员变量如下:

变量 类型 描述
timerId String 消息ID
sendTime long 本次定时任务推送时间,unix timestamp
message String 消息内容
msgTpye int 消息类型:
0:透传消息
1:通知
rangeType int 取值如下:
消息或通知发送的范围类型
0:tag组播
1:广播
2:批量单播
3:组合运算
4:精准推送
5:LBS推送
6:强订阅组播推送
7: 单播
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.QueryTimerListRequest;
import com.baidu.yun.push.model.QueryTimerListResponse;
import com.baidu.yun.push.model.TimerResultInfo;

public class QueryTimerList {
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryTimerListRequest request = new QueryTimerListRequest().
            // addTimerId("xxxxxxxxxxxx"). //设置查询一个定时任务
                    addStart(0).addLimit(6).addDeviceType(3);
            // 5. http request
            QueryTimerListResponse response = pushClient
                    .queryTimerList(request);
            // Http请求返回值解析
            System.out.println("totalNum: " + response.getTotalNum() + "\n"
                    + "result:");
            if (null != response) {
                List<?> list = response.getTimerResultInfos();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    StringBuilder strBuilder = new StringBuilder();
                    if (object instanceof TimerResultInfo) {
                        TimerResultInfo timerResult = (TimerResultInfo) object;
                        strBuilder.append("List[" + i + "]: " + "timerId= "
                                + timerResult.getTimerId() + ",sendTime= "
                                + timerResult.getSendTime() + ",= "
                                + timerResult.get() + ",msgType= "
                                + timerResult.getMsgType() + ",rangeType= "
                                + timerResult.getRangeType() + "\n");
                    }
                    System.out.println(strBuilder.toString());
                }
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例

IOS

totalNum: 1
result:
List[0]: timerId= 6877567086973893604,sendTime= 1426612683,= {"aps":{"alert":"Hello Baidu Push","badge":1,"sound":"ttt"},"key1":"value1","key2":"value2"},msgType= 5,rangeType= 0

Android (注:部分查询条件addStart(0).addLimit(1))

totalNum: 9
result:
List[0]: timerId= 5569743024030499907,sendTime= 1426594884,= {"title":"TEST","description":"Hello Baidu Push","notification_basic_style":4,"open_type":1,"net_support":1,"user_confirm":0,"url":"http://push.baidu.com","pkg_name":"com.baidu.bccsclient","pkg_version":"0.1","custom_content":{"key":"value"}},msgType= 5,rangeType= 0

queryTopicList

查询当前90天内消息推送中使用的类别主题的信息。

原型
public QueryTopicListResponse queryTopicList (QueryTopicListRequest request) throws PushClientException,PushServerException;
参数
QueryTopicListRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
start Integer 32位整数,默认值为0 起始索引位置
limit Integer 整数,范围[1,20],默认值为20。 一次查询的记录条数
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryTopicListResponse类实例对象,该类包含的成员变量:

变量 类型 描述
totalNum int 定时推送任务的总数量
topicResultInfos List<TopicResultInfo> topics属性是List,
内部存放topic的消息状态。

类TopicResultInfo的成员变量如下:

变量 类型 描述
topicId String 类别主题唯一标识码
firstPushTime long 第一次发送包含该topic的消息的发送时间
lastPushTime long 最后一次发送包含该topic的消息的发送时间
totalPushDevsNum int 该topic在之前90天内总的推送设备数,
其中返回值-1代表数据未处于待查询状态。
totalAckDevsNum int 本次topic在当前90天内的总的到达数,
其中返回值-1代表数据未处于待查询状态。
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.QueryTopicListRequest;
import com.baidu.yun.push.model.QueryTopicListResponse;
import com.baidu.yun.push.model.TopicResultInfo;

public class QueryTopicList {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryTopicListRequest request = new QueryTopicListRequest()
                    .addStart(0).addLimit(6).addDeviceType(3);
            // 5. http request
            QueryTopicListResponse response = pushClient
                    .queryTopicList(request);
            // Http请求返回值解析
            System.out.println("totalNum: " + response.getTotalNum() + "\n"
                    + "result:");
            if (null != response) {
                List<?> list = response.getTimerResultInfos();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    StringBuilder strBuilder = new StringBuilder();
                    if (object instanceof TopicResultInfo) {
                        TopicResultInfo topicResult = (TopicResultInfo) object;
                        strBuilder.append("List[" + i + "]: " + "topicId= "
                                + topicResult.getTopicId() + ",firstPushTime= "
                                + topicResult.getFirstPushTime()
                                + ",lastPushTime= "
                                + topicResult.getLastPushTime()
                                + ",totalPushDevsNum= "
                                + topicResult.getTotalPushDevsNum()
                                + ",totalAckDevsNum= "
                                + topicResult.getTotalAckDevsNum());
                    }
                    System.out.println(strBuilder.toString());
                }
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
totalNum: 1
result:
List[0]: topicId= BaiduPush,firstPushTime= 1426497933,lastPushTime= 1426557100,totalPushDevsNum= 1,totalAckDevsNum= 1 

queryTags

查询用户定义的标签组信息。

原型
public QueryTagsResponse queryTags (QueryTagsRequest request) throws PushClientException,PushServerException;
参数
QueryTagsRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 1-128字节。注:tag的值
不允许为"default",否则判错。
标签变量,不传则获取
app下所有标签信息。
start Integer 正整数,默认0 查询起始位置
limit Integer 正整数,[1,100],默认值为100 一次查询条数
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryTagsResponse类实例对象,该类包含的成员变量:

变量 类型 描述
totalNum int tag总数
tagsInfo List<TagInfo> tag详细信息

类TagInfo的成员变量如下:

变量 类型 描述
tagId String 标签id
tagName String 标签名
info String 用于进一步描述标签的附件信息
createTime long 标签创建时间,unix timestamp
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.QueryTagsRequest;
import com.baidu.yun.push.model.QueryTagsResponse;
import com.baidu.yun.push.model.TagInfo;

public class QueryTags {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryTagsRequest request = new QueryTagsRequest()
                    .addTagName("xxxxx").addStart(0).addLimit(10)
                    .addDeviceType(3);
            // 5. http request
            QueryTagsResponse response = pushClient.queryTags(request);
            // Http请求返回值解析
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.append("totalNum: " + response.getTotalNum() + "\n");
            if (null != response) {
                List<?> list = response.getTagsInfo();
                for (int i = 0; i < list.size(); i++) {
                    Object object = list.get(i);
                    if (object instanceof TagInfo) {
                        TagInfo tagInfo = (TagInfo) object;
                        strBuilder.append("List[" + i + "]: " + "tagId="
                                + tagInfo.getTagId() + ",tag="
                                + tagInfo.getTagName() + ",info="
                                + tagInfo.getInfo() + ",type="
                                + tagInfo.getType() + ",creatTime="
                                + tagInfo.getCreateTime() + "\n");
                    }
                }
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
totalNum: 5
List[0]: tagId=2278273722910086571,tag=default,info=default,type=0,creatTime=1426075686
List[1]: tagId=48115225,tag=pushdemo1,info=pushdemo15476398,type=2,creatTime=1426497111
List[2]: tagId=48115397,tag=pushdemo2,info=pushdemo25476398,type=2,creatTime=1426497226
List[3]: tagId=48115402,tag=pushdemo3,info=pushdemo35476398,type=2,creatTime=1426497234
List[4]: tagId=48115413,tag=pushdemo4,info=pushdemo45476398,type=2,creatTime=1426497239

createTag

创建用户自定义的标签组。

原型
public CreateTagResponse createTag (CreateTagRequest request) throws PushClientException,PushServerException;
参数
CreateTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 1-128字节。注:tag的值
不允许为"default",否则判错。
标签变量,不传则获取
应用下所有标签信息
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

    返回CreateTagResponse类实例对象,该类包含的成员变量:

变量 类型 描述
tagName String 标签变量
result int 状态值:
0:创建成功
1:创建失败
接口调用实例
package com.baidu.yun.push.sample;

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.CreateTagRequest;
import com.baidu.yun.push.model.CreateTagResponse;

public class CreateTag {
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            CreateTagRequest request = new CreateTagRequest().addTagName(
                    "xxxxx").addDeviceType(3);
            // 5. http request
            CreateTagResponse response = pushClient.createTag(request);
            System.out.println(String.format("tagName: %s, result: %d",
                    response.getTagName(), response.getResult()));
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
tagName: pushdemo, result: 0

deleteTag

删除用户自定义的标签组。

原型
public DeleteTagResponse deleteTag (DeleteTagRequest request) throws PushClientException,PushServerException;
参数
DeleteTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 1-128字节。注:tag的值
不允许为"default",否则判错。
标签变量,不传则获取
app下所有标签信息
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

    返回DeleteTagResponse类实例对象,该类包含的成员变量:

变量 类型 描述
tagName String 标签变量
result int 状态值:
0:创建成功
1:创建失败
接口调用实例
package com.baidu.yun.push.sample;

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.DeleteTagRequest;
import com.baidu.yun.push.model.DeleteTagResponse;

public class DeleteTag {
    public static void main(String[] args) 
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            DeleteTagRequest request = new DeleteTagRequest().addTagName(
                    "xxxxx").addDeviceType(new Integer(3));
            // 5. http request
            DeleteTagResponse response = pushClient.deleteTag(request);
            // Http请求返回值解析
            System.out.println(String.format("tagName: %s, result: %d",
                    response.getTagName(), response.getResult()));
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
tagName: pushdemo, result: 0

addDevicesToTag

向标签组中批量添加设备。

原型
public AddDevicesToTagResponse addDevicesToTag (AddDevicesToTagRequest request) throws PushClientException,PushServerException;
参数
AddDevicesToTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 必须是成功创建的tag,且其值不能为"default"。 标签变量
channelIds String 数组,[channelId1, channelId2,...], 最多10个。 channelId列表
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

    返回AddDevicesToTagResponse类实例对象,该类包含的成员变量:

变量 类型 描述
devicesInfoAfterAdded List<DeviceInfo> tag组内的设备标识及
添加操作的结果状态值。

类DeviceInfo中成员变量如下:

变量 类型 描述
channelId String 设备id
result int 状态值:
0:成功
1:失败
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.AddDevicesToTagRequest;
import com.baidu.yun.push.model.AddDevicesToTagResponse;
import com.baidu.yun.push.model.DeviceInfo;

public class AddDevicesToTag {
    public static void main(String[] args) throws PushClientException,
            PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacted information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            String[] channelIds = { "xxxxxxxxxxxxxxxxx" };
            AddDevicesToTagRequest request = new AddDevicesToTagRequest()
                    .addTagName("xxxxx").addChannelIds(channelIds)
                    .addDeviceType(3);
            // 5. http request
            AddDevicesToTagResponse response = pushClient
                    .addDevicesToTag(request);
            // Http请求返回值解析
            if (null != response) {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.append("devicesInTag:{");
                List<?> devicesInfo = response.getDevicesInfoAfterAdded();
                for (int i = 0; i < devicesInfo.size(); i++) {
                    Object object = devicesInfo.get(i);
                    if (i != 0) {
                        strBuilder.append(",");
                    }
                    if (object instanceof DeviceInfo) {
                        DeviceInfo deviceInfo = (DeviceInfo) object;
                        strBuilder.append("{channelId:"
                                + deviceInfo.getChannelId() + ",result:"
                                + deviceInfo.getResult() + "}");
                    }
                }
                strBuilder.append("}");
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值的解析样例
devicesInTag:{{channelId:3569104444463414374,result:0},{channelId:3775739456230007867,result:1}}   

deleteDevicesFromTag

从标签组中批量删除设备。

原型
public DeleteDevicesFromTagResponse deleteDevicesFromTag (DeleteDevicesFromTagRequest request) throws PushClientException,PushServerException;
参数
DeleteDevicesFromTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 必须是成功创建的tag,且其值不能为"default"。 标签变量
channelIds String [channelId1, channelId2, ...],最多10个。 channelId列表
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回DeleteDevicesFromTagResponse类实例对象,该类包含的成员变量:

变量 类型 描述
devicesInfoAfterDel List<DeviceInfo> tag组内的设备标识
及添加操作的结果状态值

DeviceInfo类的成员变量

变量 类型 描述
channelId String 设备id
result int 状态值:
0:成功
1:失败
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.DeleteDevicesFromTagRequest;
import com.baidu.yun.push.model.DeleteDevicesFromTagResponse;
import com.baidu.yun.push.model.DeviceInfo;

public class DeleteDevicesFromTag {
    public static void main(String[] args)
            throws PushClientException,PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            String[] channelIds = { "xxxxxxxxxxxxxxxxx" };
            DeleteDevicesFromTagRequest request = new DeleteDevicesFromTagRequest()
                    .addTagName("xxxxx").addChannelIds(channelIds)
                    .addDeviceType(3);
            // 5. http request
            DeleteDevicesFromTagResponse response = pushClient
                    .deleteDevicesFromTag(request);
            // Http请求返回值解析
            if (null != response) {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.append("devicesInfoAfterDel:{");
                List<?> list = response.getDevicesInfoAfterDel();
                for (int i = 0; i < list.size(); i++) {
                    if (i != 0) {
                        strBuilder.append(",");
                    }
                    Object object = list.get(i);
                    if (object instanceof DeviceInfo) {
                        DeviceInfo deviceInfo = (DeviceInfo) object;
                        strBuilder.append("{channelId: "
                                + deviceInfo.getChannelId() + ", result: "
                                + deviceInfo.getResult() + "}");
                    }
                }
                strBuilder.append("}");
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }

    }
}
返回值的解析样例
devicesInfoAfterDel:{{channelId: 3569104444463414374, result: 0},{channelId: 3775739456230007867, result: 1}}

queryDeviceNumInTag

查看标签组中关联的设备。

原型
public QueryDeviceNumInTagResponse queryDeviceNumInTag (QueryDeviceNumInTagRequest request) throws PushClientException,PushServerException;
参数
QueryDeviceNumInTagRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
tagName String 必须是成功创建的tag,
且其值不能为"default"。
标签变量
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回DeleteDevicesFromTagResponse类实例对象,该类包含的成员变量:

变量 类型 描述
deviceNum int 标签中设备的数量
接口调用实例
package com.baidu.yun.push.sample;

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.QueryDeviceNumInTagRequest;
import com.baidu.yun.push.model.QueryDeviceNumInTagResponse;

public class QueryDeviceNumInTag {
    public static void main(String[] args) throws PushClientException,
            PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryDeviceNumInTagRequest request = new QueryDeviceNumInTagRequest()
                    .addTagName("xxxxx").addDeviceType(3);
            // 5. http request
            QueryDeviceNumInTagResponse response = pushClient
                    .queryDeviceNumInTag(request);
            if (null != response) {
                System.out.println(String.format("deviceNum: %d",
                        response.getDeviceNum()));
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值的解析样例
deviceNum: 12

queryStatisticTopic

统计app某个分类主题的30天内的消息数。

原型
public QueryStatisticTopicResponse queryStatisticTopic (QueryStatisticTopicRequest request) throws PushClientException,PushServerException;
参数
QueryStatisticTopicRequest类实例对象

该类包含的成员变量:

变量 类型 必需 限制 描述
topicId String 长度1-128字节 将查询的目标topicId
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryStatisticTopicResponse类实例对象,该类包含的成员变量:

变量 类型 描述
totalNum int 总计结果集条数
result List<KeyValueForTopic> 本topic的ack信息

类KeyValueForTopic

变量 类型 描述
timestamp String unix timestamp
value TopicStatUnit 消息ack相关信息

类TopicStatUnit

变量 类型 描述
ack int ack数量
接口调用实例
package com.baidu.yun.push.sample;

import java.util.List;

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.KeyValueForTopic;
import com.baidu.yun.push.model.QueryStatisticTopicRequest;
import com.baidu.yun.push.model.QueryStatisticTopicResponse;

public class QueryStatisticTopic {
    public static void main(String[] args) throws PushClientException,
            PushServerException {
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryStatisticTopicRequest request = new QueryStatisticTopicRequest()
                    .addTopicId("BaiduPush").addDeviceType(3);
            // 5. http request
            QueryStatisticTopicResponse response = pushClient
                    .queryStatisticTopic(request);
            // Http请求返回值解析
            if (null != response) {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.append("totalNum: " + response.getTotalNum());
                List<KeyValueForTopic> topicStats = response.getResult();
                strBuilder.append("\nresult:{");
                for (int i = 0; i < topicStats.size(); i++) {
                    KeyValueForTopic keyValue = topicStats.get(i);
                    if (i != 0) {
                        strBuilder.append(",");
                    }
                    strBuilder.append("" + keyValue.getKey() + ":{"
                            + "ackNum: " + keyValue.getValue().getAckNum()
                            + "}");
                }
                strBuilder.append("\n}");
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
totalNum: 1
result:{1426521600:{ackNum: 1}
}      

queryStatisticDevice

统计安装了app的设备数,返回30天的所有统计项。

原型
public QueryStatisticDeviceResponse queryStatisticDevice (QueryStatisticDeviceRequest request) throws PushClientException,PushServerException;
参数

QueryStatisticDeviceRequest类实例对象

变量 类型 限制 描述
deviceType Integer 3:Android,4:IOS 设备类型
返回值
  • 成功

返回QueryStatisticDeviceResponse类实例对象,该类包含的成员变量:

变量 类型 描述
totalNum int 总计结果集条数
result List<KeyValueForDevice> 具体的信息

类KeyValueForDevice

变量 类型 描述
timestamp String unix timestamp
value DeviceStatUnit 设备统计信息

类DeviceStatUnit

变量 类型 描述
newTerm int 新增用户数
delTerm int 解绑用户数
onlineTerm int 在线用户数
addUpTerm int 累计终端数
totalTerm int 有效channelId总数,
等于addUpTerm 减去 delTerm

接口调用实例

package com.baidu.yun.push.sample;

import java.util.List;

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.KeyValueForDevice;
import com.baidu.yun.push.model.QueryStatisticDeviceRequest;
import com.baidu.yun.push.model.QueryStatisticDeviceResponse;

public class QueryStatisticDevice {
    public static void main(String[] args)
            throws PushClientException,PushServerException{
        // 1. get apiKey and secretKey from developer console
        String apiKey = "xxxxxxxxxxxxxxxxxxxx";
        String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler (new YunLogHandler () {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. specify request arguments
            QueryStatisticDeviceRequest request = new QueryStatisticDeviceRequest().
                addDeviceType(3);
            // 5. http request
            QueryStatisticDeviceResponse response = pushClient.
                    queryStatisticDevice(request);
            // Http请求返回值解析
            if (null != response) {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.append("totalNum: " + response.getTotalNum()
                        + "\n");
                List<KeyValueForDevice> deviceStats = response.getResult();
                strBuilder.append("result:{\n");
                for (int i = 0; i < deviceStats.size(); i++) {
                    KeyValueForDevice keyValue = deviceStats.get(i);
                    if (i != 0) {
                        strBuilder.append(",");
                    }
                    strBuilder.append("" + keyValue.getKey() + ":{"
                            + "newDeviceNum=" + keyValue.getValue().getNewDevNum()
                            + ",delDeviceNum=" + keyValue.getValue().getDelDevNum()
                            + ",onlineDeviceNum=" + keyValue.getValue().getOnlineDevNum()
                            + ",addUpDeviceNum=" + keyValue.getValue().getAddUpDevNum()
                            + ",totalDeviceNum=" + keyValue.getValue().getTotalDevNum()
                            + "}\n");
                }
                strBuilder.append("\n}");
                System.out.println(strBuilder.toString());
            }
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMsg: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
返回值解析后样例
totalNum: 10
result:{1426435200:{newDeviceNum=4,delDeviceNum=3,onlineDeviceNum=6,addUpDeviceNum=13,totalDeviceNum=10}
}

常见问题

在一些数据统计接口中, 鉴于0存在统计上的数值意义, 为保证类型上的统一, 所以使用了特殊的值 -1, 用于表示该统计项不被支持或尚未有统计数据产生.