HarmonyOS 如何用promptAction实现推送通知效果

类似推送一样,从顶部划下来一条信息,过个几秒又自己从顶部划上去消失

HarmonyOS
2024-12-25 07:57:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

参考示例如下:

//Index.ets

import {
  GlobalDialog
} from './GlobalDialog'
@Entry
@Component
export struct MainPage {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column({ space: 20 }) {
        Button('Toast显示')
          .onClick(() => {
            new GlobalDialog().showDialog(undefined, 'test !!!')
            setTimeout(() => {
              new GlobalDialog().hide()
            }, 2000)
          })

      }
      .width('100%')
    }
    .height('100%')
  }
}


// GlobalDialog.ets:

@CustomDialog
struct _GlobalDialog {
  controller: CustomDialogController
  close: () => void = () => {
  }
  image: ResourceStr | undefined = undefined
  @State msg: string = ""

  build() {
    Column() {
      Text(this.msg)
        .fontColor(Color.White)
        .width(px2vp(200))
        .height(px2vp(200))

      Image(this.image)
        .fitOriginalSize(true)
        .objectFit(ImageFit.None)
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .padding(12)
    .margin(30)
    .backgroundColor(Color.Black)
    .borderRadius(10)
    .width('100%')
    .transition(
      TransitionEffect.asymmetric(
        TransitionEffect.translate({ y: -100 }).animation({ duration: 100, curve: Curve.Sharp })
        ,
        TransitionEffect.IDENTITY
      ))
  }
}

let _dialogController: CustomDialogController | null
let _cancelCallBack: (() => void) | undefined

@Component
export struct GlobalDialog {
  showDialog(
    image: Resource | undefined,
    msg: string,
    cancelCallBack?: () => void,
    alignment: DialogAlignment = DialogAlignment.Top,
    offset: Offset = { dx: 0, dy: 0 },
    showInSubWindow: boolean = false,
    isModal: boolean = false,
  ): void {
    this.hide()
    _cancelCallBack = cancelCallBack

    let animate: AnimateParam = {
      duration: 90,
      delay: 0,
      curve: Curve.EaseInOut
    }

    _dialogController = new CustomDialogController({
      builder: _GlobalDialog({ image: image, msg: msg }),
      autoCancel: false,
      cancel: () => {
        _dialogController = null
        if (_cancelCallBack) {
          _cancelCallBack()
        }
      },
      customStyle: true,
      alignment: alignment,
      offset: offset,
      maskColor: 0x01000000,
      openAnimation: animate,
      closeAnimation: animate,
      showInSubWindow: showInSubWindow,
      isModal: isModal,
    })
    _dialogController.open()
  }

  hide() {
    if (_dialogController) {
      _dialogController.close()
      _dialogController = null
      if (_cancelCallBack) {
        _cancelCallBack()
      }
    }
  }

  build() {
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 10:30:39


相关问题
HarmonyOS系统如何实现推送通知
847浏览 • 0回复 待解决
HarmonyOS 推送通知
797浏览 • 1回复 待解决
HarmonyOS 无法收到推送通知
918浏览 • 1回复 待解决
HarmonyOS 推送通知支持图文吗
603浏览 • 1回复 待解决
HarmonyOS 通知推送解决方案
1158浏览 • 1回复 待解决
HarmonyOS 企业内部应用推送通知
811浏览 • 1回复 待解决
HarmonyOS如何用Java实现配音功能
5864浏览 • 1回复 待解决
怎么获取是否开启推送通知栏权限
3468浏览 • 1回复 待解决
HarmonyOS 如何实现定时本地通知
707浏览 • 1回复 待解决
HarmonyOS 华为push和个推push推送效果
726浏览 • 1回复 待解决