BaiduPushNodejsServerSDK
百度推送node.js接口
感谢xiariqingquan,在其基础上补充了高级API接口和文档
安装
npm install baidu-push-sdk
npm install https://github.com/wangzheng422/BaiduPushNodejsServerSDK/tarball/master
基础API
1.queryBindList
功能:查询设备、应用、用户与百度Channel的绑定关系。
函数:
queryBindList(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 否 | 用户标识,不超过256字节 |
start | uint | 否 | 查询起始页码,默认为0。 |
limit | uint | 否 | 一次查询条数,默认为10。 |
device_type | uint | 否 | 设备的类型, 3:Andriod设备; 4:iOS设备; 如果存在此字段,则只返回该设备类型的绑定关系。 默认不区分设备类型。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
total_num | uint | 系统返回的消息个数。 |
amount | uint | 本次查询绑定数量。 |
channel_id | uint | 通道标识。 |
user_id | string | channel绑定的user标识。 |
device_id | uint | channel绑定的设备编号。 |
device_type | uint | channel绑定的设备类型。 |
device_name | string | channel绑定的设备描述。 |
bind_name | string | channel绑定名称。 |
bind_time | string | channel绑定时间。 |
info | string | channel绑定附加信息。 |
device_type | uint | channel绑定的设备类型。 |
bind_status | uint | 绑定状态,0:绑定在线; 1:绑定离线。 |
online_status | string | 应用在线状态,on:在线;off:离线。 |
online_timestamp | uint | 连接创建时间,仅在在线状态时返回。 |
online_expires | uint | 连接超时时,仅在在线状态时返回。 |
2.pushMsg
功能:推送消息,该接口可用于推送单个人、一群人、所有人以及固定设备的使用场景。
函数:
pushMsg(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 否 | 用户标识,在Android里,channel_id + userid指定某一个特定client。不超过256字节,如果存在此字段,则只推送给此用户。 |
push_type | uint | 是 | 推送类型,取值范围为:1~3; 1:单个人,必须指定user_id 和 channel_id (指定用户的指定设备)或者user_id(指定用户的所有设备) 2:一群人,必须指定 tag 3:所有人,无需指定tag、user_id、channel_id |
channel_id | uint | 否 | 通道标识 |
tag | string | 否 | 标签名称,不超过128字节 |
device_type | uint | 否 | 设备类型, 3:Andriod设备; 4:iOS设备; 如果存在此字段,则向指定的设备类型推送消息。 默认为android设备类型。 |
message_type | uint | 否 | 消息类型 0:消息(透传给应用的消息体) 1:通知(对应设备上的消息通知) 默认值为0。 |
messages | string | 是 | 指定消息内容,单个消息为单独字符串。如果有二进制的消息内容,请先做 BASE64 的编码。
当message_type为1 (通知类型),请按以下格式指定消息内容。 通知消息格式及默认值: { //android必选,ios可选 "title" : "hello" , “description: "hello world" //android特有字段,可选 "notification_builder_id": 0, "notification_basic_style": 7, "open_type":0, "net_support" : 1, "user_confirm": 0, "url": "http://developer.baidu.com", "pkg_content":"", "pkg_name" : "com.baidu.bccsclient", "pkg_version":"0.1", //android自定义字段 "custom_content": { "key1":"value1", "key2":"value2" }, //ios特有字段,可选 "aps": { "alert":"Message From Baidu Push", "Sound":"", "Badge":0 }, //ios的自定义字段 "key1":"value1", "key2":"value2" } 注意: 当description与alert同时存在时,ios推送以alert内容作为通知内容 当custom_content与 ios的自定义字段"key":"value"同时存在时,ios推送的自定义字段内容会将以上两个内容合并,但推送内容整体长度不能大于256B,否则有被截断的风险。 此格式兼容Android和ios原生通知格式的推送。 |
msg_keys | string | 是 | 消息标识。 指定消息标识,必须和messages一一对应。相同消息标识的消息会自动覆盖。特别提醒:该功能只支持android设备。 |
message_expires | uint | 否 | 指定消息的过期时间,默认为86400秒。必须和messages一一对应。 |
deploy_status | uint | 否 | 部署状态。指定应用当前的部署状态,可取值: 1:开发状态 2:生产状态 若不指定,则默认设置为生产状态。特别提醒:该功能只支持ios设备类型。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
示例
var baidu_push = require("baidu-push-sdk");
var client = new baidu_push({
ak: 'your ak here',
sk: 'your sk here'
});
client.pushMsg({
user_id:"your user id here",
channel_id:"your channel id here",
push_type: 1,
device_type:4,
messages: JSON.stringify({title:'title',description:'description',aps:{alert:'aps message',sound:'',badge:0}}),
msg_keys: JSON.stringify(["key0"]),
message_type:1,
deploy_status:1
}, function(err, result){
console.log(result);
})
高级API
3.verifyBind
功能:判断设备、应用、用户与Channel的绑定关系是否存在。
函数:
verifyBind(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 是 | 用户标识,不超过256字节 |
device_type | string | 否 | 设备的类型编号如下: 3:Andriod设备; 4:iOS设备; 如果存在此字段,则只返回该设备类型的绑定关系。 默认不区分设备类型。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
4.fetchMsg
功能:查询离线消息。
函数:
fetchMsg(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 是 | 用户标识,不超过256字节 |
start | uint | 否 | 查询起始页码,默认为0。 |
limit | uint | 否 | 一次查询条数,默认为10。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
total_num | uint | 系统返回的消息个数。 |
channel_id | uint | 通道标识。 |
msg_id | uint | 系统返回的消息id。 |
msg_key | string | 消息标识,用于覆盖消息内容。 |
message | string | 消息内容。 |
mssage_length | uint | 系统返回的消息长度。 |
message_type | uint | 消息类型。 |
message_expires | uint | 消息过期时间。 |
5.fetchMsgCount
功能:查询离线消息的个数。
函数:
fetchMsgCount(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 是 | 用户标识,不超过256字节 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
total_num | uint | 系统返回的消息总数。 |
6.deleteMsg
功能:删除离线消息。
函数:
deleteMsg(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 是 | 用户标识,不超过256字节 |
msg_ids | string | 是 | 删除的消息id列表,由一个或多个msg_id组成,多个用json数组表示。如:123 或 [123, 456]。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
success_amount | uint | 成功删除条数。 |
msg_id | uint | 删除消息ID。 |
result | uint | 消息删除结果,有以下两个值: 0:成功; 1:失败。 |
7.setTag
功能:服务器端设置用户标签。当该标签不存在时,服务端将会创建该标签。特别地,当user_id被提交时,服务端将会完成用户和tag的绑定操作。
函数:
setTag(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 否 | 用户标识,不超过256字节 |
tag | string | 是 | 标签名,最长128字节。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
8.fetchTag
功能:App Server查询应用标签。
函数:
fetchTag(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
name | string | 否 | 标签名称。 |
start | uint | 否 | 查询起始页码,默认为0。 |
limit | uint | 否 | 一次查询条数,默认为10。 |
说明:name若被指定,则获取该标签的详细信息;否则,获取按页获取应用的所有标签。
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
total_num | uint | 系统返回的消息总数。 |
amount | uint | 本次查询绑定数量。 |
tags | string | 标签数组,其中包含: tid:标签ID name:标签名称 info:标签信息 type:标签类型 create_time:标签创建时间 |
9.deleteTag
功能:服务端删除用户标签。特别地,当user_id被提交时,服务端将只会完成解除该用户与tag绑定关系的操作。
注意:该操作不可恢复,请谨慎使用。
函数:
deleteTag(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 否 | 用户标识,不超过256字节 |
tag | string | 是 | 标签名,最长128字节。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
10.queryUserTag
功能:App Server查询用户所属的标签列表。
函数:
queryUserTag(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
user_id | string | 是 | 用户标识,不超过256字节 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
tag_num | uint | 标签数 |
tags | string | 标签数组,其中包含: tid:标签ID name:标签名称 info:标签信息 type:标签类型 create_time:标签创建时间 |
11.queryDeviceTag
功能:根据channel_id查询设备类型。
函数:
queryDeviceTag(opt, callback)
参数:
opt Object , 属性如下表:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
channel_id | uint | 是 | 通道标识,系统返回的channel_id。 |
callback 回调函数,其中参数
参数名称 | 类型 | 描述 |
---|---|---|
err | Object | 错误码,成功时返回null;失败时,抛出Error对象 |
result | Object | 返回值 |
result包含response_params,response_params属性是一个二级json,由n个包含key和value属性的对象组成。表示API返回的数据内容。
response_params中包含以下字段:
字段 | 类型 | 描述 |
---|---|---|
device_type | uint | 设备的类型, 3:Andriod设备; 4:iOS设备; |