-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
198 lines (187 loc) · 7.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var GlobalConfig = require('./GlobalConfig');
var APNPayload = require('./payload/APNPayload');
var VoIPPayload = require('./payload/VoIPPayload');
var SimpleAlertMsg = require('./payload/SimpleAlertMsg');
var Target = require('./getui/Target');
var SingleMessage = require('./getui/message/SingleMessage');
var TransmissionTemplate = require('./getui/template/TransmissionTemplate');
function pushMessageToSingle(clientId, content, alertMessage, badge, sound, ALIAS) {
var gt = GlobalConfig.gt;
var template = createTransmissionTemplate(content, alertMessage, badge, sound);
//单推消息体
var message = new SingleMessage({
isOffline: true, //是否离线
offlineExpireTime: 3600 * 1000 * 24 * 3, //离线时间,三天
data: template //设置推送消息类型
});
//接收方
var target = new Target({
appId: GlobalConfig.APPID,
clientId: clientId
});
if (ALIAS) {
target.setAppId(GlobalConfig.APPID).setAlias(ALIAS);
} else {
target.setAppId(GlobalConfig.APPID).setClientId(clientId);
}
var result = '';
return new Promise(function(resolve, reject) {
gt.pushMessageToSingle(message, target, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}).catch(function(err) {
if (err && err.exception instanceof RequestError) {
return new Promise(function(resolve, reject) {
//发送异常重传
gt.pushMessageToSingle(message, target, requestId, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
} else {
return Promise.reject(err);
}
});
}
function pushMessageToSingleVoIP(clientId, content, alertMessage, badge, sound, ALIAS) {
var gt = GlobalConfig.gt;
var template = createTransmissionTemplateVoIP(content, alertMessage, badge, sound);
//单推消息体
var message = new SingleMessage({
isOffline: true, //是否离线
offlineExpireTime: 3600 * 1000 * 24 * 3, //离线时间,三天
data: template //设置推送消息类型
});
//接收方
var target = new Target({
appId: GlobalConfig.APPID,
clientId: clientId
});
if (ALIAS) {
target.setAppId(GlobalConfig.APPID).setAlias(ALIAS);
} else {
target.setAppId(GlobalConfig.APPID).setClientId(clientId);
}
var result = '';
return new Promise(function(resolve, reject) {
gt.pushMessageToSingle(message, target, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}).catch(function(err) {
if (err && err.exception instanceof RequestError) {
return new Promise(function(resolve, reject) {
//发送异常重传
gt.pushMessageToSingle(message, target, requestId, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
} else {
return Promise.reject(err);
}
});
}
function createTransmissionTemplate(content, alertMessage, badge, sound) {
var template = new TransmissionTemplate();
template.setAppId(GlobalConfig.APPID);
template.setAppkey(GlobalConfig.APPKEY);
template.setTransmissionContent(content);
template.setTransmissionType(1);
if (alertMessage) {
var payload = new APNPayload();
payload.badge = badge;
payload.contentAvailable = 1;
payload.category = "ACTIONABLE";
payload.sound = sound;
payload.customMsg.payload = "payload";
// 简单模式使用
var alertMsg = new SimpleAlertMsg();
alertMsg.alertMsg = alertMessage;
payload.alertMsg = alertMsg;
// payload.alertMsg = getDictionaryAlertMsg();
template.setApnInfo(payload);
}
return template;
}
function createTransmissionTemplateVoIP(content, alertMessage, badge, sound) {
var template = new TransmissionTemplate();
template.setAppId(GlobalConfig.APPID);
template.setAppkey(GlobalConfig.APPKEY);
template.setTransmissionContent(content);
template.setTransmissionType(2);
if (alertMessage) {
var payload = new VoIPPayload();
payload.voIPPayload = alertMessage;
template.setApnInfo(payload);
}
return template;
}
// 字典形式的 alert,暂时不使用
function getDictionaryAlertMsg() {
var alertMsg = new DictionaryAlertMsg();
alertMsg.body = "body";
alertMsg.actionLocKey = "ActionLockey";
alertMsg.locKey = "LocKey";
alertMsg.locArgs = "loc-args";
alertMsg.launchImage = "launch-image";
// IOS8.2以上版本支持
alertMsg.title = "Title";
alertMsg.titleLocKey = "TitleLocKey";
alertMsg.titleLocArgs = "TitleLocArg";
return alertMsg;
}
/**
* 初始化个推的参数
* @function init
* @param {string} HOST 示例:http://sdk.open.api.igexin.com/apiex.htm
* @param {string} APPID 示例:JroCkPGgpF6LzFQqqoWlhA
* @param {string} APPKEY 示例:Mjv706pTKt5cTcjtqaToz8
* @param {string} MASTERSECRET 示例:uIBtmad7RK706cy5MKdfp3
* @returns {object} 返回一个新的对象,其中包括了所有 module.export.* 的方法和参数
*/
exports.init = function(HOST, APPID, APPKEY, MASTERSECRET) {
GlobalConfig.init(HOST, APPID, APPKEY, MASTERSECRET);
/**
* 给单个用户发送推送,优先以 ALIAS 为准(存在 ALIAS 则不会给 clientId 发送)
* @function pushMessageToSingle
* @param {string} clientId 接收推送的 clientId
* @param {string} content 推送内容
* @param {string} alertMessage 提示内容
* @param {int} badge iOS badge
* @param {string} sound iOS sound
* @param {string || null} ALIAS 如果传 ALIAS,则默认 clientId 失效
* @returns {Promise} Promise
*/
module.exports.pushMessageToSingle = function(clientId, content, alertMessage, badge, sound, ALIAS) {
return pushMessageToSingle(clientId, content, alertMessage, badge, sound, ALIAS);
};
/**
* 给单个用户发送推送,提醒使用VoIP,优先以 ALIAS 为准(存在 ALIAS 则不会给 clientId 发送)
* @function pushMessageToSingleVoIP
* @param {string} clientId 接收推送的 clientId
* @param {string} content 推送内容
* @param {string} alertMessage 提示内容
* @param {int} badge iOS badge
* @param {string} sound iOS sound
* @param {string || null} ALIAS 如果传 ALIAS,则默认 clientId 失效
* @returns {Promise} Promise
*/
module.exports.pushMessageToSingleVoIP = function(clientId, content, alertMessage, badge, sound, ALIAS) {
return pushMessageToSingleVoIP(clientId, content, alertMessage, badge, sound, ALIAS);
};
return this;
};