ref |> 新增推送管理类
Signed-off-by: xugenyuan <xugenyuan@wondertek.com.cn>
Showing
5 changed files
with
147 additions
and
3 deletions
| 1 | export { add } from "./src/main/ets/utils/Calc" | 1 | export { add } from "./src/main/ets/utils/Calc" |
| 2 | 2 | ||
| 3 | -export { HWLocationUtils } from './src/main/ets/location/HWLocationUtils' | ||
| 3 | +export { HWLocationUtils } from './src/main/ets/location/HWLocationUtils' | ||
| 4 | + | ||
| 5 | +export { WDPushNotificationManager } from "./src/main/ets/notification/WDPushNotificationManager" |
sight_harmony/features/wdHwAbility/src/main/ets/notification/WDPushNotificationManager.ets
0 → 100644
| 1 | + | ||
| 2 | +import { pushCommon, pushService } from '@kit.PushKit'; | ||
| 3 | +import { AAID } from '@kit.PushKit'; | ||
| 4 | +import { AccountManagerUtils, Logger, SPHelper, UserDataLocal } from 'wdKit/Index'; | ||
| 5 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 6 | +import notificationManager from '@ohos.notificationManager'; | ||
| 7 | +import { common, Want } from '@kit.AbilityKit'; | ||
| 8 | + | ||
| 9 | +const TAG = "NotificationManager" | ||
| 10 | + | ||
| 11 | +/* | ||
| 12 | + * 远程推送通知管理类 | ||
| 13 | + * Push Kit: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/push-introduction-0000001726287974 | ||
| 14 | + * Notification Kit: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/notification-overview-0000001822162741 | ||
| 15 | + * */ | ||
| 16 | +export class WDPushNotificationManager { | ||
| 17 | + | ||
| 18 | + private static instance: WDPushNotificationManager | ||
| 19 | + static getInstance() : WDPushNotificationManager { | ||
| 20 | + if (!WDPushNotificationManager.instance) { | ||
| 21 | + WDPushNotificationManager.instance = new WDPushNotificationManager() | ||
| 22 | + } | ||
| 23 | + return WDPushNotificationManager.instance | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + async requestEnableNotifications(context: common.UIAbilityContext) : Promise<boolean> { | ||
| 27 | + let enabled = await notificationManager.isNotificationEnabled() | ||
| 28 | + if (!enabled) { | ||
| 29 | + try { | ||
| 30 | + await notificationManager.requestEnableNotification(context) | ||
| 31 | + enabled = true | ||
| 32 | + } catch (err) { | ||
| 33 | + Logger.error(TAG, "请求通知权限报错: " + JSON.stringify(err)) | ||
| 34 | + let error = err as BusinessError | ||
| 35 | + if (error.code == 1600004) { | ||
| 36 | + Logger.error(TAG, "请求通知权限 - 用户已拒绝") | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + Logger.info(TAG, "推送 enabled " + enabled) | ||
| 41 | + return enabled | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + async fetchTokenAndBindProfileId() { | ||
| 45 | + try { | ||
| 46 | + const pushToken: string = await pushService.getToken(); | ||
| 47 | + Logger.info(TAG, "获取推送token: " + pushToken) | ||
| 48 | + | ||
| 49 | + //TODO: pushToken 上传至服务器 | ||
| 50 | + SPHelper.default.save("devicePushToken", pushToken) | ||
| 51 | + | ||
| 52 | + if (AccountManagerUtils.isLoginSync()) { | ||
| 53 | + this.bindUserProfileId(UserDataLocal.getUserId()) | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + } catch (err) { | ||
| 57 | + Logger.error(TAG, "获取推送token失败: " + JSON.stringify(err)) | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + // 禁止推送 | ||
| 62 | + stopPush() { | ||
| 63 | + pushService.deleteToken() | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /// 应用匿名标识符 | ||
| 67 | + async getAAID() { | ||
| 68 | + let aaid: string = "" | ||
| 69 | + try { | ||
| 70 | + aaid = await AAID.getAAID(); | ||
| 71 | + Logger.info(TAG, "获取应用匿名标识符AAID: " + aaid) | ||
| 72 | + } catch (err) { | ||
| 73 | + Logger.error(TAG, "获取应用匿名标识符AAID失败: " + JSON.stringify(err)) | ||
| 74 | + } | ||
| 75 | + return aaid | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + // TODO: 登录时调用 | ||
| 79 | + bindUserProfileId(profileId: string) { | ||
| 80 | + pushService.bindAppProfileId(pushCommon.AppProfileType.PROFILE_TYPE_APPLICATION_ACCOUNT, profileId).then(() => { | ||
| 81 | + Logger.info(TAG, "推送绑定profileId 成功: " + profileId) | ||
| 82 | + }).catch((err: BusinessError) => { | ||
| 83 | + Logger.error(TAG, "推送绑定profileId失败: " + profileId + " " + JSON.stringify(err)) | ||
| 84 | + }); | ||
| 85 | + } | ||
| 86 | + unbindUserProfileId(profileId: string) { | ||
| 87 | + pushService.unbindAppProfileId(profileId).then(() => { | ||
| 88 | + Logger.info(TAG, "推送解绑profileId 成功: " + profileId) | ||
| 89 | + }).catch((err: BusinessError) => { | ||
| 90 | + Logger.error(TAG, "推送解绑profileId失败: " + profileId + " " + JSON.stringify(err)) | ||
| 91 | + }); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + sendLocalNotification() { | ||
| 95 | + let notificationRequest: notificationManager.NotificationRequest = { | ||
| 96 | + id: 1, | ||
| 97 | + content: { | ||
| 98 | + notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, | ||
| 99 | + normal: { | ||
| 100 | + title: "test_title", | ||
| 101 | + text: "test_text", | ||
| 102 | + additionalText: "test_additionalText" | ||
| 103 | + } | ||
| 104 | + } | ||
| 105 | + }; | ||
| 106 | + notificationManager.publish(notificationRequest).then(() => { | ||
| 107 | + console.info("publish success"); | ||
| 108 | + }).catch((err: BusinessError) => { | ||
| 109 | + console.error(`publish fail: ${JSON.stringify(err)}`); | ||
| 110 | + }); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + setBadgeNumber(number: number) : Promise<void> { | ||
| 114 | + return notificationManager.setBadgeNumber(number) | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + // 接收推送数据,包括启动和二次点击拉起 | ||
| 118 | + // 参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/push-dev-0000001727885258 | ||
| 119 | + onWant(want: Want) { | ||
| 120 | + Logger.info(TAG, "接收到推送?: " + JSON.stringify(want.parameters)) | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | +} |
| @@ -17,6 +17,7 @@ import { | @@ -17,6 +17,7 @@ import { | ||
| 17 | import { HostEnum, HostManager, WDHttp } from 'wdNetwork'; | 17 | import { HostEnum, HostManager, WDHttp } from 'wdNetwork'; |
| 18 | import { LoginModule } from 'wdLogin/src/main/ets/LoginModule'; | 18 | import { LoginModule } from 'wdLogin/src/main/ets/LoginModule'; |
| 19 | import { ConfigurationConstant } from '@kit.AbilityKit'; | 19 | import { ConfigurationConstant } from '@kit.AbilityKit'; |
| 20 | +import { WDPushNotificationManager } from 'wdHwAbility/Index'; | ||
| 20 | 21 | ||
| 21 | export default class EntryAbility extends UIAbility { | 22 | export default class EntryAbility extends UIAbility { |
| 22 | onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { | 23 | onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { |
| @@ -44,6 +45,13 @@ export default class EntryAbility extends UIAbility { | @@ -44,6 +45,13 @@ export default class EntryAbility extends UIAbility { | ||
| 44 | EmitterUtils.receiveEvent(EmitterEventId.NETWORK_DISCONNECTED, (() => { | 45 | EmitterUtils.receiveEvent(EmitterEventId.NETWORK_DISCONNECTED, (() => { |
| 45 | Logger.info('network disconnected') | 46 | Logger.info('network disconnected') |
| 46 | })) | 47 | })) |
| 48 | + | ||
| 49 | + WDPushNotificationManager.getInstance().onWant(want) | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + // App活着情况下,点击推送通知进入 | ||
| 53 | + onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { | ||
| 54 | + WDPushNotificationManager.getInstance().onWant(want) | ||
| 47 | } | 55 | } |
| 48 | 56 | ||
| 49 | onDestroy(): void { | 57 | onDestroy(): void { |
| @@ -4,7 +4,8 @@ import { BreakpointConstants } from 'wdConstant'; | @@ -4,7 +4,8 @@ import { BreakpointConstants } from 'wdConstant'; | ||
| 4 | import { BreakpointSystem, EmitterEventId, EmitterUtils, Logger } from 'wdKit'; | 4 | import { BreakpointSystem, EmitterEventId, EmitterUtils, Logger } from 'wdKit'; |
| 5 | import router from '@ohos.router'; | 5 | import router from '@ohos.router'; |
| 6 | import { promptAction } from '@kit.ArkUI'; | 6 | import { promptAction } from '@kit.ArkUI'; |
| 7 | -import { HWLocationUtils } from 'wdHwAbility/Index'; | 7 | +import { HWLocationUtils, WDPushNotificationManager } from 'wdHwAbility/Index'; |
| 8 | +import { common } from '@kit.AbilityKit'; | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | const TAG = 'MainPage'; | 11 | const TAG = 'MainPage'; |
| @@ -24,6 +25,16 @@ struct MainPage { | @@ -24,6 +25,16 @@ struct MainPage { | ||
| 24 | aboutToAppear() { | 25 | aboutToAppear() { |
| 25 | HWLocationUtils.startLocationService() | 26 | HWLocationUtils.startLocationService() |
| 26 | this.breakpointSystem.register() | 27 | this.breakpointSystem.register() |
| 28 | + | ||
| 29 | + let context = getContext(this) as common.UIAbilityContext | ||
| 30 | + WDPushNotificationManager.getInstance().requestEnableNotifications(context).then((enabled) => { | ||
| 31 | + if (enabled) { | ||
| 32 | + WDPushNotificationManager.getInstance().fetchTokenAndBindProfileId() | ||
| 33 | + | ||
| 34 | + // WDPushNotificationManager.getInstance().sendLocalNotification() | ||
| 35 | + } | ||
| 36 | + }) | ||
| 37 | + | ||
| 27 | Logger.info(TAG, `aboutToAppear `); | 38 | Logger.info(TAG, `aboutToAppear `); |
| 28 | EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => { | 39 | EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => { |
| 29 | LogoutViewModel.clearLoginInfo() | 40 | LogoutViewModel.clearLoginInfo() |
-
Please register or login to post a comment