diff --git a/common/EtsNotification/README_zh.md b/common/EtsNotification/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..5a1e6db53420279214066b3e6b1e1d3ad93b6a53 --- /dev/null +++ b/common/EtsNotification/README_zh.md @@ -0,0 +1,25 @@ +# EtsNotification + + + +### 简介 + +本示例展示了在eTS中如何创建和删除Slot通道,如何发布和取消通知。 + +### 使用说明 + +1、在界面中点击**创建Slot通道**,创建完成提示"Add slot succeed"。 + +2、选择要发布的通知类型,点击**发布通知**,在通知栏会看到发布的通知,下拉通知栏后可以看到通知内容。 + +3、在发布通知后,下拉通知栏,在通知消息中,点击通知或通知中的按钮,可以跳转到本应用界面。 + +4、在应用界面点击**取消最近发布的通知**,可以取消最新发布的一条通知。 + +5、点击**取消所有通知**,可以取消所有发布的通知。 + + + +### 约束与限制 + +本示例支持标准系统上运行。 diff --git a/common/EtsNotification/build.gradle b/common/EtsNotification/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..cae0ab0036a5626c92f93fb72c5f763e9764d21b --- /dev/null +++ b/common/EtsNotification/build.gradle @@ -0,0 +1,33 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.3.4' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/common/EtsNotification/entry/build.gradle b/common/EtsNotification/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..eeb7e1258930dbfd3af9f86f229a811de106a92b --- /dev/null +++ b/common/EtsNotification/entry/build.gradle @@ -0,0 +1,16 @@ +apply plugin: 'com.huawei.ohos.hap' +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} diff --git a/common/EtsNotification/entry/src/main/config.json b/common/EtsNotification/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..3c1e1a3ea071bee403303ae59c1b4e0145fbb082 --- /dev/null +++ b/common/EtsNotification/entry/src/main/config.json @@ -0,0 +1,64 @@ +{ + "app": { + "bundleName": "ohos.samples.etsnotification", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etsnotification", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": true + } + } + ] + } +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/ets/MainAbility/app.ets b/common/EtsNotification/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..bf28e58b36cf11db491d3f00768ef2c9e41d05cf --- /dev/null +++ b/common/EtsNotification/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/ets/MainAbility/common/choose.ets b/common/EtsNotification/entry/src/main/ets/MainAbility/common/choose.ets new file mode 100644 index 0000000000000000000000000000000000000000..202829c02479a580b0d7c94e8d6d3256481e61c9 --- /dev/null +++ b/common/EtsNotification/entry/src/main/ets/MainAbility/common/choose.ets @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@Component +export struct Choose { + @Link selectIndex: number + + getImg(current) { + if (this.selectIndex === current) { + return $r("app.media.checked") + } else { + return $r("app.media.uncheck") + } + } + + build() { + Column() { + Flex({ direction: FlexDirection.Column }) { + Text($r("app.string.choose_notification_type")) + .width(300) + .padding({ top: 10 }) + .fontSize(20) + .fontColor(Color.Black) + .fontWeight(FontWeight.Bold) + Column() { + Row() { + Image(this.getImg(0)) + .height(30).width(30) + .objectFit(ImageFit.Contain) + Text($r("app.string.notification_type_normal")) + .padding(10) + .fontSize(20) + .fontColor(Color.Black) + } + .width(300) + .onClick(() => { + this.selectIndex = 0 + }) + + Row() { + Image(this.getImg(1)) + .height(30).width(30) + .objectFit(ImageFit.Contain) + Text($r("app.string.notification_type_long_text")) + .padding(10) + .fontSize(20) + .fontColor(Color.Black) + } + .width(300) + .onClick(() => { + this.selectIndex = 1 + }) + + Row() { + Image(this.getImg(2)) + .height(30).width(30) + .objectFit(ImageFit.Contain) + Text($r("app.string.notification_type_multiline")) + .padding(10) + .fontSize(20) + .fontColor(Color.Black) + }.width(300) + .onClick(() => { + this.selectIndex = 2 + console.info('this.selectIndex = ' + this.selectIndex) + }) + } + } + } + } +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/ets/MainAbility/model/notificationUtils.ets b/common/EtsNotification/entry/src/main/ets/MainAbility/model/notificationUtils.ets new file mode 100644 index 0000000000000000000000000000000000000000..9ce33fd0ff9256fd59b807c3a2e77feb975978c0 --- /dev/null +++ b/common/EtsNotification/entry/src/main/ets/MainAbility/model/notificationUtils.ets @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import notify from '@ohos.notification'; + +function getNotificationContentBasic(mWantAgent) { + let notificationRequest = { + id: 1, + slotType: notify.SlotType.CONTENT_INFORMATION, + content: { + contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, + normal: { + title: 'notification title', // 通知标题 + text: 'notification text', // 通知内容 + additionalText: 'notification additionalText', //通知次要内容,是对通知内容的补充 + } + }, + actionButtons: [ + { + title: 'button', + wantAgent: mWantAgent + } + ] + } + return notificationRequest +} + +function getNotificationContentLongText(mWantAgent) { + let notificationRequest = { + id: 2, + slotType: notify.SlotType.CONTENT_INFORMATION, + content: { + contentType: notify.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, + longText: { + title: 'notification title', + text: 'notification text', + additionalText: 'notification additionalText', //通知次要内容,是对通知内容的补充 + longText: 'notification long text', // 通知的长文本 + briefText: 'notification briefText', // 通知概要内容,是对通知内容的总结 + expandedTitle: 'notification expandedTitle', // 通知展开时的标题 + } + }, + wantAgent: mWantAgent + } + return notificationRequest +} + +function getNotificationContentMultiline(mWantAgent) { + let notificationRequest = { + id: 3, + slotType: notify.SlotType.CONTENT_INFORMATION, + content: { + contentType: notify.ContentType.NOTIFICATION_CONTENT_MULTILINE, + multiLine: { + title: 'notification title', + text: 'notification text', + additionalText: 'notification additionalText', //通知次要内容,是对通知内容的补充 + briefText: 'notification briefText', + longTitle: 'notification long title', + lines: ['line1', 'line2', 'line3'] + } + }, + wantAgent: mWantAgent + } + return notificationRequest +} + +export function getNotificationRequest(contentType, mWantAgent) { + console.info('[notification]getNotificationRequest contentType=' + contentType) + let notificationRequest + switch (contentType) { + case 0: + notificationRequest = getNotificationContentBasic(mWantAgent) + break + case 1: + notificationRequest = getNotificationContentLongText(mWantAgent) + break + case 2: + notificationRequest = getNotificationContentMultiline(mWantAgent) + break + default: + break + } + return notificationRequest +} + +export function getNotificationId(contentType) { + console.info('[notification]getNotificationId contentType=' + contentType) + let notificationId = 1 + switch (contentType) { + case 0: + notificationId = 1 + break + case 1: + notificationId = 2 + break + case 2: + notificationId = 3 + break + default: + break + } + return notificationId +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/ets/MainAbility/pages/index.ets b/common/EtsNotification/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..db8d691ed7caaeb9b5746e7bec2958e937ec2015 --- /dev/null +++ b/common/EtsNotification/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import notify from '@ohos.notification'; +import wantAgent from '@ohos.wantAgent'; +import prompt from '@system.prompt'; +import { getNotificationRequest, getNotificationId } from '../model/notificationUtils' +import { Choose } from '../common/choose' + +@Entry +@Component +struct Index { + @State contentType: number = 0 + private slotType: number = 0 + + build() { + Column() { + Text($r('app.string.app_name')) + .width('100%') + .height(50) + .backgroundColor('#0D9FFB') + .fontColor(Color.White) + .fontSize(20) + .padding({ left: 15 }) + Scroll() { + Flex({ + direction: FlexDirection.Row, + alignItems: ItemAlign.Center, + justifyContent: FlexAlign.Center, + wrap: FlexWrap.Wrap + }) { + Choose({ selectIndex: $contentType }) + Column() { + Button() { + Text($r('app.string.add_slot')) + .fontSize(25) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin(10) + .width(340) + .height(60) + .backgroundColor('#0D9FFB') + .onClick(() => { + console.info('[notification] addSlot') + + function addSlotCallBack() { + console.info("[notification]==========================>addSlotCallBack=======================>"); + prompt.showToast({ message: 'Add slot succeed' }) + } + + notify.addSlot(notify.SlotType.CONTENT_INFORMATION, addSlotCallBack) + }) + + Button() { + Text($r('app.string.publish_notification')) + .fontSize(25) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin(10) + .width(340) + .height(60) + .backgroundColor('#0D9FFB') + .onClick(() => { + console.info('[notification]publish notification') + let WantAgentInfo = { + wants: [ + { + bundleName: 'ohos.samples.etsnotification', + abilityName: 'ohos.samples.etsnotification.MainAbility', + } + ], + operationType: wantAgent.OperationType.START_ABILITY, + requestCode: 0 + } + wantAgent.getWantAgent(WantAgentInfo, (err, data) => { + if (err.code !== 0) { + console.info('[notification]getWantAgent failed,err = ' + JSON.stringify(err)) + return + } + let notificationRequest = getNotificationRequest(this.contentType, data) + notify.publish(notificationRequest).then(() => { + console.info("[notification]==========================>publishCallback=======================>"); + }); + }) + }) + + Button() { + Text($r('app.string.cancel_last_notification')) + .fontSize(25) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin(10) + .width(340) + .height(60) + .backgroundColor('#0D9FFB') + .onClick(() => { + console.info('[notification]cancel notification') + let notificationId = getNotificationId(this.contentType) + notify.cancel(notificationId, () => { + console.info("[notification]==========================>cancelAllCallback=======================>"); + }) + }) + + Button() { + Text($r('app.string.cancel_all_notification')) + .fontSize(25) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin(10) + .width(340) + .height(60) + .backgroundColor('#0D9FFB') + .onClick(() => { + console.info('[notification]cancel notification') + notify.cancelAll(() => { + console.info("[notification]==========================>cancelAllCallback=======================>"); + }) + }) + + Button() { + Text($r('app.string.delete_slot')) + .fontSize(25) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin(10) + .width(340) + .height(60) + .backgroundColor('#0D9FFB') + .onClick(() => { + console.info('[notification]delete slot') + notify.removeSlot(notify.SlotType.CONTENT_INFORMATION, () => { + console.info("[notification]==========================>removeSlotAllCallback=======================>"); + prompt.showToast({ message: 'Delete slot succeed' }) + }) + }) + } + } + .margin({ bottom: 50 }) + } + } + } +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/resources/base/element/string.json b/common/EtsNotification/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3244ba76bab65f86847d443cef8dc54513b03fe8 --- /dev/null +++ b/common/EtsNotification/entry/src/main/resources/base/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "app_name", + "value": "eTSNotification" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "choose_notification_type", + "value": "Choose notification type" + }, + { + "name": "notification_type_normal", + "value": "Notification content basic text" + }, + { + "name": "notification_type_long_text", + "value": "Notification content long text" + }, + { + "name": "notification_type_multiline", + "value": "Notification content multiline" + }, + { + "name": "add_slot", + "value": "Add slot" + }, + { + "name": "publish_notification", + "value": "Publish notification" + }, + { + "name": "cancel_last_notification", + "value": "Cancel the last notification" + }, + { + "name": "cancel_all_notification", + "value": "Cancel all notification" + }, + { + "name": "delete_slot", + "value": "Delete slot" + } + ] +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/resources/base/media/checked.png b/common/EtsNotification/entry/src/main/resources/base/media/checked.png new file mode 100644 index 0000000000000000000000000000000000000000..a77ded514ac884365ec515801bb34c68e6e5b7f8 Binary files /dev/null and b/common/EtsNotification/entry/src/main/resources/base/media/checked.png differ diff --git a/common/EtsNotification/entry/src/main/resources/base/media/icon.png b/common/EtsNotification/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/common/EtsNotification/entry/src/main/resources/base/media/icon.png differ diff --git a/common/EtsNotification/entry/src/main/resources/base/media/uncheck.png b/common/EtsNotification/entry/src/main/resources/base/media/uncheck.png new file mode 100644 index 0000000000000000000000000000000000000000..cba71b7ec168e67e261151ee8cb9e950f53e7cbb Binary files /dev/null and b/common/EtsNotification/entry/src/main/resources/base/media/uncheck.png differ diff --git a/common/EtsNotification/entry/src/main/resources/en/element/string.json b/common/EtsNotification/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3244ba76bab65f86847d443cef8dc54513b03fe8 --- /dev/null +++ b/common/EtsNotification/entry/src/main/resources/en/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "app_name", + "value": "eTSNotification" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "choose_notification_type", + "value": "Choose notification type" + }, + { + "name": "notification_type_normal", + "value": "Notification content basic text" + }, + { + "name": "notification_type_long_text", + "value": "Notification content long text" + }, + { + "name": "notification_type_multiline", + "value": "Notification content multiline" + }, + { + "name": "add_slot", + "value": "Add slot" + }, + { + "name": "publish_notification", + "value": "Publish notification" + }, + { + "name": "cancel_last_notification", + "value": "Cancel the last notification" + }, + { + "name": "cancel_all_notification", + "value": "Cancel all notification" + }, + { + "name": "delete_slot", + "value": "Delete slot" + } + ] +} \ No newline at end of file diff --git a/common/EtsNotification/entry/src/main/resources/zh/element/string.json b/common/EtsNotification/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..18360ce0566401a43ffa4cbb27ff5bea8fccdd96 --- /dev/null +++ b/common/EtsNotification/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "app_name", + "value": "eTSNotification" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "choose_notification_type", + "value": "请选择通知类型" + }, + { + "name": "notification_type_normal", + "value": "普通类型通知" + }, + { + "name": "notification_type_long_text", + "value": "长文本类型通知" + }, + { + "name": "notification_type_multiline", + "value": "多行文本类型通知" + }, + { + "name": "add_slot", + "value": "创建Slot通道" + }, + { + "name": "publish_notification", + "value": "发布通知" + }, + { + "name": "cancel_last_notification", + "value": "取消最近发布的通知" + }, + { + "name": "cancel_all_notification", + "value": "取消所有通知" + }, + { + "name": "delete_slot", + "value": "删除通道" + } + ] +} \ No newline at end of file diff --git a/common/EtsNotification/screenshots/devices/main.png b/common/EtsNotification/screenshots/devices/main.png new file mode 100644 index 0000000000000000000000000000000000000000..bc5459c7d7dd374b0524940cf8d8619fdae86614 Binary files /dev/null and b/common/EtsNotification/screenshots/devices/main.png differ diff --git a/common/EtsNotification/settings.gradle b/common/EtsNotification/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/common/EtsNotification/settings.gradle @@ -0,0 +1 @@ +include ':entry'