Skip to content

Commit a87b5d0

Browse files
author
zhengjh
committed
* 推送API,Notification里Android增加channel_id
* 增加批量单推接口 * 增加接口: 送达统计详情(新)/v3/received/detail * 增加接口: 送达状态查询 /v3/status/message * 增加接口: 消息统计详情(VIP 专属接口,新)/v3/messages/detail
1 parent 9043048 commit a87b5d0

16 files changed

+256
-50
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,5 @@ typings/
9494

9595
\.idea/
9696
package-lock.json
97+
/examples/Conf.js
98+
/test/BaseTest.js

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ client.push().setPlatform('ios', 'android')
6161
```js
6262
// 在构建 JPushClient 对象的时候, 指定 isDebug 参数。
6363
var client = JPush.buildClient({
64-
appKey:'47a3ddda34b2602fa9e17c01',
65-
masterSecret:'d94f733358cca97b18b2cb98',
64+
appKey:'your appKey',
65+
masterSecret:'your masterSecret',
6666
isDebug:false
6767
});
6868
// or
69-
var client = JPush.buildClient('47a3ddda34b2602fa9e17c01', 'd94f733358cca97b18b2cb98', null, false);
69+
var client = JPush.buildClient('your appKey', 'your masterSecret', null, false);
7070
```
7171

7272
> 目前使用了 debug 模块来控制日志输出,若要查看 JPush 的相关日志信息,请先配置 DEBUG 环境变量 'jpush'。

examples/BatchPushExample.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var JPush = require("../index.js").JPush;
2+
var Conf = require("./Conf.js");
3+
4+
console.log(Conf.appKey);
5+
console.log(Conf.masterSecret);
6+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
7+
console.log(client.appkey);
8+
console.log(client.masterSecret);
9+
10+
var singlePayloads = [
11+
{"platform":"all", "target":"regid1", "notification":{"alert":"alert title"}},
12+
{"platform":"all", "target":"regid2", "notification":{"alert":"alert title"}},
13+
];
14+
15+
client.batchPushByRegid(singlePayloads, function(err, res) {
16+
console.log(err);
17+
console.log(res);
18+
});

examples/Conf.js.example

Whitespace-only changes.

examples/DeviceAsyncExamples.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var JPush = require("../index.js").JPushAsync;
2+
var Conf = require("./Conf.js");
23

3-
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
45

56
async function fun() {
67
try {

examples/DeviceExample.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var JPush = require("../index.js").JPush;
2+
var Conf = require("./Conf.js");
23

3-
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
45

56
client.getDeviceTagAlias('0900e8d85ef', function (err, res) {
67
if (err) {

examples/PushAsyncExample.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var JPush = require("../index.js").JPushAsync;
2-
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
2+
var Conf = require("./Conf.js");
3+
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
35

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

79
// easy push.
810
client.push().setPlatform(JPush.ALL)

examples/PushExample.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var JPush = require("../index.js").JPush;
2-
var client = JPush.buildClient('a1703c14b186a68a66ef86c1', '9dabdf8bb704b421759cb49c')
2+
var Conf = require("./Conf.js");
3+
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
35

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

79
// easy push.
810
client.push().setPlatform(JPush.ALL)

examples/ReportExample.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
var JPush = require("../index.js").JPush;
2+
var Conf = require("./Conf.js");
23

3-
var client = JPush.buildClient('96261ea4bcaae3f4a167a495', 'db1abbffbb619458b7785164')
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
45

5-
client.getReportReceiveds('746522674,344076897', function (err, res) {
6-
if (err) {
7-
if (err instanceof JPush.APIConnectionError) {
8-
console.log(err.message)
9-
// Response Timeout means your request to the server may have already received,
10-
// please check whether or not to push
11-
console.log(err.isResponseTimeout)
12-
} else if (err instanceof JPush.APIRequestError) {
13-
console.log(err.message)
6+
function getReportReceiveds() {
7+
client.getReportReceiveds('746522674,344076897', function (err, res) {
8+
if (err) {
9+
if (err instanceof JPush.APIConnectionError) {
10+
console.log(err.message)
11+
// Response Timeout means your request to the server may have already received,
12+
// please check whether or not to push
13+
console.log(err.isResponseTimeout)
14+
} else if (err instanceof JPush.APIRequestError) {
15+
console.log(err.message)
16+
}
17+
} else {
18+
for (var i = 0; i < res.length; i++) {
19+
console.log(res[i].android_received)
20+
console.log(res[i].ios_apns_sent)
21+
console.log(res[i].msg_id)
22+
console.log('------------')
23+
}
1424
}
15-
} else {
16-
for (var i = 0; i < res.length; i++) {
17-
console.log(res[i].android_received)
18-
console.log(res[i].ios_apns_sent)
19-
console.log(res[i].msg_id)
20-
console.log('------------')
21-
}
22-
}
25+
});
26+
}
27+
28+
client.getReportReceivedDetail('746522674,344076897', function(err, res) {
29+
console.log(err);
30+
console.log(res);
31+
})
32+
33+
client.getReportStatusMessage(746522674, ['regid1', 'regid2'], null, function(err, res) {
34+
console.log(err);
35+
console.log(res);
36+
})
37+
38+
client.getReportMessagesDetail('746522674,344076897', function(err, res) {
39+
console.log(err);
40+
console.log(res);
2341
})

examples/ScheduleExample.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var JPush = require("../index.js").JPush;
2-
var client = JPush.buildClient('96261ea4bcaae3f4a167a495', 'db1abbffbb619458b7785164')
2+
var Conf = require("./Conf.js");
3+
4+
var client = JPush.buildClient(Conf.appKey, Conf.masterSecret);
35

46
// 设置定时任务。
57
client.push().setPlatform(JPush.ALL)

lib/JPush/JPush.js

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ var GROUP_API_URL = 'https://api.jpush.cn/v3/grouppush'
1616
var REPORT_API_URL = 'https://report.jpush.cn/v3'
1717
var SCHEDULE_API_URL = 'https://api.jpush.cn/v3/schedules' // 定时任务
1818
var REPORT_RECEIVED = '/received'
19+
var REPORT_RECEIVED_DETAIL = '/received/detail'
20+
var REPORT_STATUS_MESSAGE = '/status/message'
1921
var REPORT_USER = '/users'
2022
var REPORT_MESSAGE = '/messages'
23+
var REPORT_MESSAGE_DETAIL = '/messages/detail'
2124
var HOST_NAME_SSL = 'https://device.jpush.cn'
2225
var DEVICE_PATH = '/v3/devices'
2326
var TAG_PATH = '/v3/tags'
@@ -96,7 +99,7 @@ function sendPush (payload, callback) {
9699
return _request(this, this.isGroup === true ? GROUP_API_URL : PUSH_API_URL, payload, 'POST', callback)
97100
}
98101

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

108-
function getReportMessages (msgIds, callback) {
111+
function getReportReceivedDetail(msgIds, callback) {
112+
if (MSG_IDS_PATTERNS.test(msgIds)) {
113+
throw new JError.InvalidArgumentError(
114+
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
115+
}
116+
var url = REPORT_API_URL + REPORT_RECEIVED_DETAIL + '?msg_ids=' + msgIds
117+
return _request(this, url, null, 'GET', callback)
118+
}
119+
120+
function getReportStatusMessage(msgId, registrationIds, date, callback) {
121+
if (msgId == null) {
122+
throw new JError.InvalidArgumentError('msgId is null!');
123+
}
124+
if (typeof(msgId) != 'number') {
125+
throw new JError.InvalidArgumentError('msgId is not number type!');
126+
}
127+
if (registrationIds == null) {
128+
throw new JError.InvalidArgumentError('registrationIds is null!');
129+
}
130+
var json = {
131+
"msg_id": msgId,
132+
"registration_ids": registrationIds
133+
};
134+
if (date) {
135+
json.date = date;
136+
}
137+
var url = REPORT_API_URL + REPORT_STATUS_MESSAGE;
138+
return _request(this, url, JSON.stringify(json), 'POST', callback);
139+
}
140+
141+
function getReportMessages(msgIds, callback) {
109142
if (MSG_IDS_PATTERNS.test(msgIds)) {
110143
throw new JError.InvalidArgumentError(
111144
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
@@ -114,6 +147,15 @@ function getReportMessages (msgIds, callback) {
114147
return _request(this, url, null, 'GET', callback)
115148
}
116149

150+
function getReportMessagesDetail(msgIds, callback) {
151+
if (MSG_IDS_PATTERNS.test(msgIds)) {
152+
throw new JError.InvalidArgumentError(
153+
'Invalid msg_ids, msg_ids should be composed with alphabet and comma.')
154+
}
155+
var url = REPORT_API_URL + REPORT_MESSAGE_DETAIL + '?msg_ids=' + msgIds
156+
return _request(this, url, null, 'GET', callback)
157+
}
158+
117159
function getReportUsers (timeUnit, start, duration, callback) {
118160
var url = REPORT_API_URL + REPORT_USER + '?time_unit=' + timeUnit + '&start=' + start + '&duration=' + duration
119161
return _request(this, url, null, 'GET', callback)
@@ -265,6 +307,60 @@ function getScheduleMsgIds (scheduleId, callback) {
265307
return _request(this, url, null, 'GET', callback)
266308
}
267309

310+
/**
311+
* 获取推送唯一标识符
312+
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid
313+
* @param {*} count 可选参数。数值类型,不传则默认为 1。范围为 [1, 1000]
314+
* @param {*} type 可选参数。CID 类型。取值:push(默认),schedule
315+
* @param {*} callback
316+
*/
317+
function getCid(count, type, callback) {
318+
if (!count) {
319+
count = 1;
320+
}
321+
if (!type) {
322+
type = 'push';
323+
}
324+
var url = PUSH_API_URL + '/cid?count=' + count + '&type=' + type;
325+
return _request(this, url, null, 'GET', callback);
326+
}
327+
328+
/**
329+
* 针对RegID方式批量单推(VIP专属接口)
330+
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
331+
* @param {*} singlePayloads 单推payload数组
332+
* @param {*} callback
333+
*/
334+
function batchPushByRegid(singlePayloads, callback) {
335+
var url = PUSH_API_URL + '/batch/regid/single';
336+
return batchPush.call(this, url, singlePayloads, callback);
337+
}
338+
339+
/**
340+
* 针对Alias方式批量单推(VIP专属接口)s
341+
* http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
342+
* @param {*} singlePayloads 单推payload数组
343+
* @param {*} callback
344+
*/
345+
function batchPushByAlias(singlePayloads, callback) {
346+
var url = PUSH_API_URL + '/batch/alias/single';
347+
return batchPush.call(this, url, singlePayloads, callback);
348+
}
349+
350+
function batchPush(url, singlePayloads, callback) {
351+
var client = this;
352+
return getCid.call(client, singlePayloads.length, 'push', function(err, res) {
353+
if (err) {
354+
return callback(err);
355+
}
356+
var body = {"pushlist":{}};
357+
for (var i = 0; i < singlePayloads.length; i++) {
358+
body.pushlist[res.cidlist[i]] = singlePayloads[i];
359+
}
360+
return _request(client, url, JSON.stringify(body), 'POST', callback);
361+
});
362+
}
363+
268364
// 定时任务 end
269365

270366
// Proxy start
@@ -324,7 +420,6 @@ function _request (client, url, body, method, callback, times = 1) {
324420
callback(new JError.APIRequestError(res.statusCode, body))
325421
}
326422
}
327-
328423
Request[method.toLowerCase()]({
329424
url: url,
330425
body: body,
@@ -339,6 +434,9 @@ function _request (client, url, body, method, callback, times = 1) {
339434

340435
JPushClient.prototype.sendPush = sendPush
341436
JPushClient.prototype.getReportReceiveds = getReportReceiveds
437+
JPushClient.prototype.getReportReceivedDetail = getReportReceivedDetail
438+
JPushClient.prototype.getReportStatusMessage = getReportStatusMessage
439+
JPushClient.prototype.getReportMessagesDetail = getReportMessagesDetail
342440
JPushClient.prototype.push = push
343441
JPushClient.prototype.setMobile = setMobile
344442
JPushClient.prototype.getDeviceTagAlias = getDeviceTagAlias
@@ -357,6 +455,9 @@ JPushClient.prototype.getSchedule = getSchedule
357455
JPushClient.prototype.delSchedule = delSchedule
358456
JPushClient.prototype.setSchedule = setSchedule
359457
JPushClient.prototype.updateSchedule = updateSchedule
458+
JPushClient.prototype.getCid = getCid
459+
JPushClient.prototype.batchPushByRegid = batchPushByRegid
460+
JPushClient.prototype.batchPushByAlias = batchPushByAlias
360461

361462
// exports constants and methods
362463
exports.ALL = JModel.ALL

0 commit comments

Comments
 (0)