Skip to content

Commit

Permalink
* 推送API,Notification里Android增加channel_id
Browse files Browse the repository at this point in the history
* 增加批量单推接口
* 增加接口: 送达统计详情(新)/v3/received/detail
* 增加接口: 送达状态查询 /v3/status/message
* 增加接口: 消息统计详情(VIP 专属接口,新)/v3/messages/detail
  • Loading branch information
zhengjh committed Sep 4, 2019
1 parent 9043048 commit a87b5d0
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ typings/

\.idea/
package-lock.json
/examples/Conf.js
/test/BaseTest.js
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ client.push().setPlatform('ios', 'android')
```js
// 在构建 JPushClient 对象的时候, 指定 isDebug 参数。
var client = JPush.buildClient({
appKey:'47a3ddda34b2602fa9e17c01',
masterSecret:'d94f733358cca97b18b2cb98',
appKey:'your appKey',
masterSecret:'your masterSecret',
isDebug:false
});
// or
var client = JPush.buildClient('47a3ddda34b2602fa9e17c01', 'd94f733358cca97b18b2cb98', null, false);
var client = JPush.buildClient('your appKey', 'your masterSecret', null, false);
```

> 目前使用了 debug 模块来控制日志输出,若要查看 JPush 的相关日志信息,请先配置 DEBUG 环境变量 'jpush'。
18 changes: 18 additions & 0 deletions examples/BatchPushExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var JPush = require("../index.js").JPush;
var Conf = require("./Conf.js");

console.log(Conf.appKey);
console.log(Conf.masterSecret);
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
console.log(client.appkey);
console.log(client.masterSecret);

var singlePayloads = [
{"platform":"all", "target":"regid1", "notification":{"alert":"alert title"}},
{"platform":"all", "target":"regid2", "notification":{"alert":"alert title"}},
];

client.batchPushByRegid(singlePayloads, function(err, res) {
console.log(err);
console.log(res);
});
Empty file added examples/Conf.js.example
Empty file.
3 changes: 2 additions & 1 deletion examples/DeviceAsyncExamples.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var JPush = require("../index.js").JPushAsync;
var Conf = require("./Conf.js");

var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

async function fun() {
try {
Expand Down
3 changes: 2 additions & 1 deletion examples/DeviceExample.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var JPush = require("../index.js").JPush;
var Conf = require("./Conf.js");

var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

client.getDeviceTagAlias('0900e8d85ef', function (err, res) {
if (err) {
Expand Down
6 changes: 4 additions & 2 deletions examples/PushAsyncExample.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var JPush = require("../index.js").JPushAsync;
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
var Conf = require("./Conf.js");

var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

// 使用 proxy
// var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c', null, null, null,'http://192.168.8.236:3128')
// var client = JPush.buildClient(Conf.appKey, Conf.masterSecret, null, null, null,'http://192.168.8.236:3128')

// easy push.
client.push().setPlatform(JPush.ALL)
Expand Down
6 changes: 4 additions & 2 deletions examples/PushExample.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var JPush = require("../index.js").JPush;
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
var Conf = require("./Conf.js");

var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

// 使用 proxy
// var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c', null, null, null,'http://192.168.8.236:3128')
// var client = JPush.buildClient(Conf.appKey, Conf.masterSecret, null, null, null,'http://192.168.8.236:3128')

// easy push.
client.push().setPlatform(JPush.ALL)
Expand Down
54 changes: 36 additions & 18 deletions examples/ReportExample.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
var JPush = require("../index.js").JPush;
var Conf = require("./Conf.js");

var client = JPush.buildClient('96261ea4bcaae3f4a167a495', 'db1abbffbb619458b7785164')
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

client.getReportReceiveds('746522674,344076897', function (err, res) {
if (err) {
if (err instanceof JPush.APIConnectionError) {
console.log(err.message)
// Response Timeout means your request to the server may have already received,
// please check whether or not to push
console.log(err.isResponseTimeout)
} else if (err instanceof JPush.APIRequestError) {
console.log(err.message)
function getReportReceiveds() {
client.getReportReceiveds('746522674,344076897', function (err, res) {
if (err) {
if (err instanceof JPush.APIConnectionError) {
console.log(err.message)
// Response Timeout means your request to the server may have already received,
// please check whether or not to push
console.log(err.isResponseTimeout)
} else if (err instanceof JPush.APIRequestError) {
console.log(err.message)
}
} else {
for (var i = 0; i < res.length; i++) {
console.log(res[i].android_received)
console.log(res[i].ios_apns_sent)
console.log(res[i].msg_id)
console.log('------------')
}
}
} else {
for (var i = 0; i < res.length; i++) {
console.log(res[i].android_received)
console.log(res[i].ios_apns_sent)
console.log(res[i].msg_id)
console.log('------------')
}
}
});
}

client.getReportReceivedDetail('746522674,344076897', function(err, res) {
console.log(err);
console.log(res);
})

client.getReportStatusMessage(746522674, ['regid1', 'regid2'], null, function(err, res) {
console.log(err);
console.log(res);
})

client.getReportMessagesDetail('746522674,344076897', function(err, res) {
console.log(err);
console.log(res);
})
4 changes: 3 additions & 1 deletion examples/ScheduleExample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var JPush = require("../index.js").JPush;
var client = JPush.buildClient('96261ea4bcaae3f4a167a495', 'db1abbffbb619458b7785164')
var Conf = require("./Conf.js");

var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);

// 设置定时任务。
client.push().setPlatform(JPush.ALL)
Expand Down
107 changes: 104 additions & 3 deletions lib/JPush/JPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ var GROUP_API_URL = 'https://api.jpush.cn/v3/grouppush'
var REPORT_API_URL = 'https://report.jpush.cn/v3'
var SCHEDULE_API_URL = 'https://api.jpush.cn/v3/schedules' // 定时任务
var REPORT_RECEIVED = '/received'
var REPORT_RECEIVED_DETAIL = '/received/detail'
var REPORT_STATUS_MESSAGE = '/status/message'
var REPORT_USER = '/users'
var REPORT_MESSAGE = '/messages'
var REPORT_MESSAGE_DETAIL = '/messages/detail'
var HOST_NAME_SSL = 'https://device.jpush.cn'
var DEVICE_PATH = '/v3/devices'
var TAG_PATH = '/v3/tags'
Expand Down Expand Up @@ -96,7 +99,7 @@ function sendPush (payload, callback) {
return _request(this, this.isGroup === true ? GROUP_API_URL : PUSH_API_URL, payload, 'POST', callback)
}

function getReportReceiveds (msgIds, callback) {
function getReportReceiveds(msgIds, callback) {
if (MSG_IDS_PATTERNS.test(msgIds)) {
throw new JError.InvalidArgumentError(
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
Expand All @@ -105,7 +108,37 @@ function getReportReceiveds (msgIds, callback) {
return _request(this, url, null, 'GET', callback)
}

function getReportMessages (msgIds, callback) {
function getReportReceivedDetail(msgIds, callback) {
if (MSG_IDS_PATTERNS.test(msgIds)) {
throw new JError.InvalidArgumentError(
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
}
var url = REPORT_API_URL + REPORT_RECEIVED_DETAIL + '?msg_ids=' + msgIds
return _request(this, url, null, 'GET', callback)
}

function getReportStatusMessage(msgId, registrationIds, date, callback) {
if (msgId == null) {
throw new JError.InvalidArgumentError('msgId is null!');
}
if (typeof(msgId) != 'number') {
throw new JError.InvalidArgumentError('msgId is not number type!');
}
if (registrationIds == null) {
throw new JError.InvalidArgumentError('registrationIds is null!');
}
var json = {
"msg_id": msgId,
"registration_ids": registrationIds
};
if (date) {
json.date = date;
}
var url = REPORT_API_URL + REPORT_STATUS_MESSAGE;
return _request(this, url, JSON.stringify(json), 'POST', callback);
}

function getReportMessages(msgIds, callback) {
if (MSG_IDS_PATTERNS.test(msgIds)) {
throw new JError.InvalidArgumentError(
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
Expand All @@ -114,6 +147,15 @@ function getReportMessages (msgIds, callback) {
return _request(this, url, null, 'GET', callback)
}

function getReportMessagesDetail(msgIds, callback) {
if (MSG_IDS_PATTERNS.test(msgIds)) {
throw new JError.InvalidArgumentError(
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
}
var url = REPORT_API_URL + REPORT_MESSAGE_DETAIL + '?msg_ids=' + msgIds
return _request(this, url, null, 'GET', callback)
}

function getReportUsers (timeUnit, start, duration, callback) {
var url = REPORT_API_URL + REPORT_USER + '?time_unit=' + timeUnit + '&start=' + start + '&duration=' + duration
return _request(this, url, null, 'GET', callback)
Expand Down Expand Up @@ -265,6 +307,60 @@ function getScheduleMsgIds (scheduleId, callback) {
return _request(this, url, null, 'GET', callback)
}

/**
* 获取推送唯一标识符
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid
* @param {*} count 可选参数。数值类型,不传则默认为 1。范围为 [1, 1000]
* @param {*} type 可选参数。CID 类型。取值:push(默认),schedule
* @param {*} callback
*/
function getCid(count, type, callback) {
if (!count) {
count = 1;
}
if (!type) {
type = 'push';
}
var url = PUSH_API_URL + '/cid?count=' + count + '&type=' + type;
return _request(this, url, null, 'GET', callback);
}

/**
* 针对RegID方式批量单推(VIP专属接口)
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
* @param {*} singlePayloads 单推payload数组
* @param {*} callback
*/
function batchPushByRegid(singlePayloads, callback) {
var url = PUSH_API_URL + '/batch/regid/single';
return batchPush.call(this, url, singlePayloads, callback);
}

/**
* 针对Alias方式批量单推(VIP专属接口)s
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
* @param {*} singlePayloads 单推payload数组
* @param {*} callback
*/
function batchPushByAlias(singlePayloads, callback) {
var url = PUSH_API_URL + '/batch/alias/single';
return batchPush.call(this, url, singlePayloads, callback);
}

function batchPush(url, singlePayloads, callback) {
var client = this;
return getCid.call(client, singlePayloads.length, 'push', function(err, res) {
if (err) {
return callback(err);
}
var body = {"pushlist":{}};
for (var i = 0; i < singlePayloads.length; i++) {
body.pushlist[res.cidlist[i]] = singlePayloads[i];
}
return _request(client, url, JSON.stringify(body), 'POST', callback);
});
}

// 定时任务 end

// Proxy start
Expand Down Expand Up @@ -324,7 +420,6 @@ function _request (client, url, body, method, callback, times = 1) {
callback(new JError.APIRequestError(res.statusCode, body))
}
}

Request[method.toLowerCase()]({
url: url,
body: body,
Expand All @@ -339,6 +434,9 @@ function _request (client, url, body, method, callback, times = 1) {

JPushClient.prototype.sendPush = sendPush
JPushClient.prototype.getReportReceiveds = getReportReceiveds
JPushClient.prototype.getReportReceivedDetail = getReportReceivedDetail
JPushClient.prototype.getReportStatusMessage = getReportStatusMessage
JPushClient.prototype.getReportMessagesDetail = getReportMessagesDetail
JPushClient.prototype.push = push
JPushClient.prototype.setMobile = setMobile
JPushClient.prototype.getDeviceTagAlias = getDeviceTagAlias
Expand All @@ -357,6 +455,9 @@ JPushClient.prototype.getSchedule = getSchedule
JPushClient.prototype.delSchedule = delSchedule
JPushClient.prototype.setSchedule = setSchedule
JPushClient.prototype.updateSchedule = updateSchedule
JPushClient.prototype.getCid = getCid
JPushClient.prototype.batchPushByRegid = batchPushByRegid
JPushClient.prototype.batchPushByAlias = batchPushByAlias

// exports constants and methods
exports.ALL = JModel.ALL
Expand Down
Loading

0 comments on commit a87b5d0

Please sign in to comment.