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
| //《多厂商推送透传消息带通知》
| 'use strict';
| const createConfig = require('uni-config-center')
| const config = createConfig({
| pluginId: 'uni-push'
| }).config()
|
| const {
| appId,
| appkey,
| appSecret,
| mastersecret,
| packageName,
| } = config.getui
| const BaseUrl = "https://restapi.getui.com/v2/" + appId
| var token, intent, push_message, push_channel;
|
| const msgDb = uniCloud.database().collection('msg');
|
| module.exports = async function(funName, data, PushConfig) {
| token = await getToken();
| //console.log("token",token);
| let {
| title,
| content,
| payload
| } = data;
|
| if (funName == 'toSingle') {
| payload = JSON.parse(payload)
| let {
| id
| } = await msgDb.add({
| "read": false,
| ...data,
| "add_time": +new Date(),
| payload
| });
| payload.msgId = id
| payload = JSON.stringify(payload)
| }
|
| intent =
| `intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;package=${packageName};component=${packageName}/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=${title};S.content=${content};S.payload=${payload};end`;
| push_message = {
| "transmission": payload
| }
| push_channel = {
| "android": {
| "ups": {
| "notification": {
| title,
| "body": content,
| "channel_level": 3,
| "click_type": "intent",
| intent,
| "options": [{
| "constraint": "HW",
| "key": "/message/android/notification/badge/class",
| "value": "io.dcloud.PandoraEntry"
| }, {
| "constraint": "HW",
| "key": "/message/android/notification/badge/add_num",
| "value": 1
| },
| {
| "constraint": "HW",
| "key": "classification",
| "value": 1
| }
| ]
| }
| }
| },
| "ios": {
| "type": "notify",
| payload,
| "aps": {
| "alert": {
| "title": title,
| "body": content
| },
| "content-available": 0,
| "sound": "pushsound.caf"
| },
| "auto_badge": "+1",
| }
| }
| //消息发送完
| let pushRes = await eval(`${funName}`)(data);
| return pushRes
| };
|
|
| async function toApp(data) { //注:此接口频次限制100次/天,每分钟不能超过5次(推送限制和接口根据条件筛选用户推送共享限制)
| const requestData = {
| request_id: "uni" + Date.now() + "push",
| audience: "all",
| push_message,
| push_channel,
| ...data
| }
| let res = await post('/push/all', requestData)
| return res.data;
| }
| async function toSingle(data) {
| const clientid = data.clientid
| delete data.clientid
| const requestData = {
| request_id: "uni" + Date.now() + Math.ceil(Math.random() * 10000000) + "push",
| audience: {
| "cid": [clientid]
| },
| push_message,
| push_channel,
| ...data
| }
| let {
| data: pushRes
| } = await post('/push/single/cid', requestData)
| //console.log('requestData', JSON.stringify(requestData));
| if (pushRes.msg == 'success') {
| pushRes.state = pushRes.data[Object.keys(pushRes.data)[0]][clientid]
| }
| return pushRes;
| }
|
|
|
|
| async function getToken() {
| const timestamp = Date.now();
| let sign = require('crypto').createHash('sha256')
| .update(appkey + timestamp + mastersecret)
| .digest('hex'); //hex是十六进制
| let res = await uniCloud.httpclient.request(BaseUrl + '/auth', {
| method: 'POST',
| contentType: 'json',
| dataType: 'json',
| data: {
| sign,
| timestamp,
| appkey
| },
| })
| let {
| msg
| } = res.data;
| if (msg == 'success') {
| return res.data.data.token;
| } else {
| console.log('错误请检查,err:' + JSON.stringify(res.data));
| return false
| }
| }
| async function post(module, requestData) {
| return await uniCloud.httpclient.request(BaseUrl + module, {
| method: 'POST',
| headers: {
| "content-type": "application/json",
| "token": token,
| "cache-control": "no-cache"
| },
| contentType: 'json',
| dataType: "json",
| data: requestData,
| })
| }
|
|