GetuiPush.ets
16.1 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { common, Want } from '@kit.AbilityKit';
import PushManager, {
BindAliasCmdMessage,
GTCmdMessage, GTNotificationMessage, GTTransmitMessage, PushConst,
SetTagCmdMessage,
Tag,
UnBindAliasCmdMessage } from 'library';
import { AppUtils, DeviceUtil, EmitterEventId, EmitterUtils, Logger, SPHelper } from 'wdKit/Index';
import { HostEnum, HostManager, HttpBizUtil, HttpUrlUtils, HttpUtils, ResponseDTO } from 'wdNetwork/Index';
import { notificationManager } from '@kit.NotificationKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { SpConstants } from 'wdConstant/Index';
import { PushContentBean, PushContentParser } from './PushContentParser';
import { ParamType, Tracking } from 'wdTracking/Index';
import { PushTransmitMessageBean, PushTransmitMessagePayloadBean } from './PushTransmitMessageBean';
import { JSON } from '@kit.ArkTS';
import wantAgent from '@ohos.app.ability.wantAgent';
import { WantAgent } from '@ohos.app.ability.wantAgent';
const TAG = "GetuiPush"
export class GetuiPush {
private static GETUI_APPID_ONLINE = "sMkzgp09Ov82nU1MGk7Ae6"
private static GETUI_APPID_TEST = "ZMi5AAXYHE84VN9p55YpW2"
private readonly ALIAS_SN_USERID = "userID"
private readonly TAG_SN_TAG = "tag1"
private readonly TAG_PD_ZH = "peopledaily_zh"
private cid: string = ""
private deviceToken: string = ""
private currentUserId = ""
private lastPushContent?: PushContentBean
private initialed = false
private hasEnterMain = false
private constructor() {
}
private static manager: GetuiPush
static sharedInstance() : GetuiPush {
if (!GetuiPush.manager) {
GetuiPush.manager = new GetuiPush()
}
return GetuiPush.manager
}
init(context: common.UIAbilityContext) {
Logger.debug(TAG, "个推SDK开始初始化....")
const isOnlineEnv = HostManager.getHost() === HostEnum.HOST_PRODUCT
PushManager.initialize({
appId:isOnlineEnv ? GetuiPush.GETUI_APPID_ONLINE : GetuiPush.GETUI_APPID_TEST,
context: context,
onSuccess: (cid:string) => {
Logger.debug(TAG, "个推SDK初始化成功,cid = " + cid)
this.initialed = true
this.cid = cid
this.registerEvents()
this.checkSetup()
if (this.hasEnterMain) {
this.comsumeLastPushContent()
} else {
Logger.debug(TAG, "等待进入主页")
}
},
onFailed: (error:string) => {
Logger.error(TAG, "个推SDK初始化失败,error = " + error)
}
})
}
async requestEnableNotifications(context: common.UIAbilityContext) : Promise<boolean> {
let enabled = await notificationManager.isNotificationEnabled()
if (!enabled) {
try {
await notificationManager.requestEnableNotification(context)
enabled = true
} catch (err) {
Logger.error(TAG, "请求通知权限报错: " + JSON.stringify(err))
let error = err as BusinessError
if (error.code == 1600004) {
Logger.error(TAG, "请求通知权限 - 用户已拒绝")
}
}
}
Logger.info(TAG, "推送 enabled " + enabled)
return enabled
}
checkSetup() {
if (!this.initialed) { return }
if (HttpUtils.isLogin()) {
const userId = SPHelper.default.getSync(SpConstants.USER_ID, "") as string
this.currentUserId = userId + ""
this.setAlias(true, userId)
}
let tags = [this.TAG_PD_ZH, AppUtils.getAppVersionCode()]
this.setTags(tags)
}
// 默认是开启的
switchPush(turnOn: boolean) {
turnOn ? PushManager.turnOnPush() : PushManager.turnOffPush()
}
registerEvents() {
// 登录和退出
EmitterUtils.receiveEvent(EmitterEventId.LOGIN_SUCCESS, () => {
const userId = SPHelper.default.getSync(SpConstants.USER_ID, "") as string
this.currentUserId = userId + ""
this.setAlias(true, userId)
})
EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => {
if (this.currentUserId.length) {
this.setAlias(false, this.currentUserId)
}
})
PushManager.setPushCallback({
// cid
onReceiveClientId: (clientId: string) => {
Logger.debug(TAG, "推送 接收到 clientId = " + clientId)
this.cid = PushManager.getClientId()
SPHelper.default.save(SpConstants.GETUI_PUSH_CID, this.cid)
this.uploadPushInfo(this.cid)
},
//接收⼚商token
onReceiveDeviceToken: (deviceToken:string) => {
Logger.debug(TAG, "推送 deviceToken = " + deviceToken)
this.deviceToken = deviceToken;
SPHelper.default.save(SpConstants.GETUI_PUSH_DEVICE_TOKEN, this.deviceToken)
},
// cid 离线上线通知
onReceiveOnlineState: (onLine:boolean) => {
Logger.debug(TAG, "推送 onLine State = " + onLine)
},
//命令相应回复
onReceiveCommandResult: (result: GTCmdMessage) => {
Logger.debug(TAG, "推送 Cmd Message = " + JSON.stringify(result))
this.dealWithCmdMessage(result)
},
//sdk 收到透传消息
onReceiveMessageData: (message: GTTransmitMessage) => {
Logger.debug(TAG, "推送 透传 Message = " + JSON.stringify(message))
this.dealWithTransmitMessage(message)
},
//通知到达回调
onNotificationMessageArrived: (message: GTNotificationMessage) => {
Logger.debug(TAG, "推送 通知到达回调 " + JSON.stringify(message))
},
//通知点击回调, 需要配合PushManager.setClickWant(want)使⽤
onNotificationMessageClicked: (message: GTNotificationMessage) => {
Logger.debug(TAG, "推送 通知 点击 回调 " + JSON.stringify(message))
},
})
}
setAlias(bind: boolean, alias: string, sn: string = this.ALIAS_SN_USERID) {
if (typeof alias == "number") {
alias = `${alias}`
}
if (!this.initialed) { return }
if (bind) {
Logger.debug(TAG, "推送 绑定别名 " + alias)
PushManager.bindAlias(alias, sn)
} else {
Logger.debug(TAG, "推送 解绑别名 " + alias)
PushManager.unBindAlias(alias, true, sn)
}
}
setTags(tags: string[], sn: string = this.TAG_SN_TAG) {
if (!this.initialed) { return }
Logger.debug(TAG, "推送 设置标签 " + tags)
PushManager.setTag(tags.map((tag) => {
return new Tag().setName(tag)
}), sn)
}
setBadgeNumber(number: number) {
Logger.debug(TAG, "推送 设置角标 " + number)
PushManager.setBadgeNum(number)
}
// 接收推送数据,包括启动和二次点击拉起
// 参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/push-dev-0000001727885258
onWant(want: Want) {
this.onNewWant(want, true)
}
onNewWant(want: Want, startup: boolean = false) {
Logger.debug(TAG, "want: " + JSON.stringify(want))
this.lastPushContent = undefined
let pushContent = PushContentParser.getPushLinkFromWant(want)
if (pushContent && pushContent.isPush) {
Logger.debug(TAG, "接收到推送: " + JSON.stringify(want.parameters))
this.lastPushContent = pushContent
if (this.initialed && this.hasEnterMain) {
this.comsumeLastPushContent()
} else {
Logger.debug(TAG, "等待初始化完成 或 进入主页")
}
}
}
// 首次进入主页,即可解析跳转推送
onReachMainPage() {
this.hasEnterMain = true
if (this.initialed) {
this.comsumeLastPushContent()
} else {
// 这里可能还没来得及初始化完成。所以不能清空
// this.lastPushContent = undefined
Logger.debug(TAG, "等待初始化完成")
}
}
comsumeLastPushContent() {
Logger.debug(TAG, "尝试消费推送: " + this.lastPushContent)
if (!this.lastPushContent) {
return
}
if (this.lastPushContent.online) {
if (this.lastPushContent.localNotify == true) {
Logger.debug(TAG, "推送 回执???: " + this.lastPushContent.want)
let gtactionid = 60002;//gtactionid传⼊60002表示个推渠道消息点击了
PushManager.sendFeedbackMessage(this.lastPushContent.messageId || "", this.lastPushContent.taskId || "", gtactionid)
} else if (this.lastPushContent.want) {
Logger.debug(TAG, "推送 回执: " + this.lastPushContent.want)
PushManager.setClickWant(this.lastPushContent.want)
}
} else if (this.lastPushContent.want) {
Logger.debug(TAG, "推送 回执: " + this.lastPushContent.want)
PushManager.setClickWant(this.lastPushContent.want)
}
if (this.lastPushContent.pushLink) {
Logger.debug(TAG, "跳转对应页面: " + this.lastPushContent.pushLink)
PushContentParser.jumpWithPushLink(this.lastPushContent.pushLink)
}
this.lastPushContent = undefined
}
private trackingClick(content: PushContentBean) {
let param: ParamType = {
"pushResourceId": "",
"pushTitle": content.notifyTitle || "",
"pushContent": content.notifyContent || "",
}
Tracking.event("push_click", param)
}
private dealWithCmdMessage(result: GTCmdMessage) {
let action: Number = result.action;
if (action === PushConst.BIND_ALIAS_RESULT) {
let bindAliasCmdMessage = result as BindAliasCmdMessage;
/* code 结果说明
0:成功
10099:SDK 未初始化成功
30001:绑定别名失败,频率过快,两次调⽤的间隔需⼤于 1s
30002:绑定别名失败,参数错误
30003:当前 cid 绑定别名次数超限
30004:绑定别名失败,未知异常
30005:绑定别名时,cid 未获取到
30006:绑定别名时,发⽣⽹络错误
30007:别名⽆效
30008:sn ⽆效 */
let code = bindAliasCmdMessage.code
if (code == 0) {
Logger.debug(TAG, "推送 Cmd BindAlias 成功 " + bindAliasCmdMessage.sn)
} else {
Logger.debug(TAG, "推送 Cmd BindAlias 失败 "
+ ",sn = "+bindAliasCmdMessage.sn
+ ",clinetId = "+bindAliasCmdMessage.clientId
+ ",pkgname = "+bindAliasCmdMessage.pkgName
+ ",appId = "+bindAliasCmdMessage.appId
+ ",action = "+result.action
+ ",code ="+bindAliasCmdMessage.code)
}
} else if (action === PushConst.UNBIND_ALIAS_RESULT) {
let unBindAliasCmdMessage = result as UnBindAliasCmdMessage
/* code 结果说明
0:成功
10099:SDK 未初始化成功
30001:解绑别名失败,频率过快,两次调⽤的间隔需⼤于 1s
30002:解绑别名失败,参数错误
30003:当前 cid 解绑别名次数超限
30004:解绑别名失败,未知异常
30005:解绑别名时,cid 未获取到
30006:解绑别名时,发⽣⽹络错误
30007:别名⽆效
30008:sn ⽆效*/
let code = unBindAliasCmdMessage.code
if (code == 0) {
Logger.debug(TAG, "推送 Cmd UnBindAlias 成功 " + unBindAliasCmdMessage.sn)
} else {
Logger.debug(TAG, "推送 Cmd UnBindAlias 失败 "
+ ",sn = "+unBindAliasCmdMessage.sn
+ ",clinetId = "+unBindAliasCmdMessage.clientId
+ ",pkgname = "+unBindAliasCmdMessage.pkgName
+ ",appId = "+unBindAliasCmdMessage.appId
+ ",action = "+result.action
+ ",code ="+unBindAliasCmdMessage.code)
}
} else if (action === PushConst.SET_TAG_RESULT) {
let setTagCmdMessage = result as SetTagCmdMessage
/* code 值说明
0:成功
10099:SDK 未初始化成功
20001:tag 数量过⼤(单次设置的 tag 数量不超过 100)
20002:调⽤次数超限(默认⼀天只能成功设置⼀次)
20003:标签重复
20004:服务初始化失败
20005:setTag 异常
20006:tag 为空
20007:sn 为空
20008:离线,还未登陆成功
20009:该 appid 已经在⿊名单列表(请联系技术⽀持处理)
20010:已存 tag 数⽬超限
20011:tag 内容格式不正确 */
let code = setTagCmdMessage.code
if (code == 0) {
Logger.debug(TAG, "推送 Cmd SetTag 成功 " + setTagCmdMessage.sn)
} else {
Logger.debug(TAG, "推送 Cmd SetTag 失败 "
+ ",sn = "+setTagCmdMessage.sn
+ ",clinetId = "+setTagCmdMessage.clientId
+ ",pkgname = "+setTagCmdMessage.pkgName
+ ",appId = "+setTagCmdMessage.appId
+ ",action = "+result.action
+ ",code ="+setTagCmdMessage.code)
}
}
}
private dealWithTransmitMessage(message: GTTransmitMessage) {
// const taskid = message.taskId
// const messageId = message.messageId
// /* 上报个推透传消息的展示回执。如果透传消息本地创建通知栏消息“展示”了,则调⽤此⽅法上报。
// */
// int gtactionid = 60001;//gtactionid传⼊60001表示个推渠道消息展示了
// boolean result = PushManager.sendFeedbackMessag(taskid, messageid, gtactionid);
// /**
// * 上报个推透传消息的点击回执。如果透传消息本地创建通知栏消息“点击”了,则调⽤此⽅法上报。
// */
// int gtactionid = 60002;//gtactionid传⼊60002表示个推渠道消息点击了
// boolean result = PushManager.sendFeedbackMessage(taskid, messageid, gtactionid);
this.sendLocalNotification(message)
}
private sendLocalNotification(message: GTTransmitMessage) {
let jsonMsgObj = JSON.parse(message.payload) as PushTransmitMessageBean
if (!jsonMsgObj) {
return
}
if (jsonMsgObj && jsonMsgObj.payload) {
jsonMsgObj.payloadObj = JSON.parse(jsonMsgObj.payload) as PushTransmitMessagePayloadBean
}
let pushLink = jsonMsgObj.payloadObj?.pushLink || ""
let params: Record<string, string> = {
"title": jsonMsgObj.title,
"content": jsonMsgObj.body,
"pushLink": pushLink,
"taskid": message.taskId,
"messageId": message.messageId,
"gtTransmitMsgLocalNotify": "1",
}
this.getWantAgent(params, (error, want) => {
if (error) {
return
}
let notificationRequest: notificationManager.NotificationRequest = {
id: Date.now(),
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: jsonMsgObj.title,
text: jsonMsgObj.body,
}
},
notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION,
wantAgent:want,
};
notificationManager.publish(notificationRequest).then(() => {
Logger.debug(TAG, "本地发送系统通知完成")
}).catch((err: BusinessError) => {
Logger.debug(TAG, "本地发送系统通知失败:" + JSON.stringify(err))
});
})
}
private getWantAgent(parameters:Record<string, string>, callback: AsyncCallback<WantAgent>) {
// 通过WantAgentInfo的operationType设置动作类型
let wantAgentInfo:wantAgent.WantAgentInfo = {
wants: [
{
deviceId: '',
bundleName: 'com.peopledailychina.hosactivity',
abilityName: 'EntryAbility',
action: 'com.test.pushaction',
entities: [],
uri: "rmrbapp://rmrb.app:8080/openwith",
parameters: parameters
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo, (err:BusinessError, data:WantAgent) => {
if (err) {
Logger.error(TAG, `Failed to get want agent. Code is ${err.code}, message is ${err.message}`)
}
if (callback) {
callback(err, data)
}
});
}
private uploadPushInfo(cid: string) {
const url = HttpUrlUtils.getUploadPushInfoUrl()
let bean: Record<string, string | number> = {}
bean["deviceId"] = DeviceUtil.clientId()
bean["cid"] = cid
bean["userId"] = SPHelper.default.getSync(SpConstants.USER_ID, "") as string
bean["appVersion"] = AppUtils.getAppVersionName()
bean["platform"] = 3
HttpBizUtil.post<ResponseDTO<string>>(url, bean).then((data) => {
Logger.debug(TAG, "上传cid成功" + JSON.stringify(data))
}).catch((e: BusinessError) => {
Logger.debug(TAG, "上传cid失败" + JSON.stringify(e))
})
}
}