HarmonyOS 鸿蒙Next eTS发布Notification后点击通知如何打开跳转指定页面

HarmonyOS 鸿蒙Next eTS发布Notification后点击通知如何打开跳转指定页面 点击如何跳转到app内部

这是我的代码

import prompt from '@system.prompt';
import Notification from '@ohos.notification';

@Entry
@Component struct NotificationTest {

  private id: number = 10086;

  private error: string = "";

  build() {
    Column({ space: 10 }) {
      Button('发布通知')
        .onClick(() => {
          this.publishNotification();
        })
      Button('取消通知')
        .onClick(() => {
          this.cancelNotification();
        })
      Button('取消所有通知')
        .onClick(() => {
          this.cancelAllNotification();
        })

      Text(this.error)
        .fontSize(20)
    }
    .width('100%')
    .height('100%')
    .padding(10)
  }

  private cancelAllNotification() {
    Notification.cancelAll(() => {
      prompt.showToast({
        message: "取消所有通知"
      })
    })
  }

  private cancelNotification() {
    this.id--;
    Notification.cancel(this.id, () => {
      prompt.showToast({
        message: "取消通知" + this.id
      })
    })
  }

  private publishNotification() {
    console.log("publish")
    let contentType = Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT;
    Notification.publish({
      content: {
        contentType: contentType,
        normal: {
          title: "测试标题" + this.id,
          text: "测试内容" + this.id,
          additionalText: "测试额外数据" + this.id
        }
      },
      id: this.id,
      color: 0xff0000,
      colorEnabled: true,
      label: "测试标签" + this.id,
      wantAgent: {
        deviceId:"",
        pkgName: "com.example.entry",
        abilityName: "com.example.entry.MainAbility"
      },
      badgeIconStyle: 20
    }, (error) => {
      if(error) {
        this.error = JSON.stringify(error);
      } else {
        this.id++;
        prompt.showToast({
          message: "发送成功"
        })
      }
    })
  }

更多关于HarmonyOS 鸿蒙Next eTS发布Notification后点击通知如何打开跳转指定页面的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

更多关于HarmonyOS 鸿蒙Next eTS发布Notification后点击通知如何打开跳转指定页面的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


WantAgent的解释有吗?需要传什么参数?

在HarmonyOS鸿蒙系统中,使用eTS(Extension TypeScript)框架发布Notification后,点击通知跳转指定页面的实现方式如下:

首先,在发布Notification时,需要设置通知的点击事件。在eTS中,这通常通过调用系统的Notification API来完成,并在设置通知内容时指定一个点击动作(例如,一个Intent或特定的点击事件处理函数)。

其次,点击动作需要被设计为能够触发应用内的页面跳转。这通常意味着在点击事件中指定目标页面的路由信息,或者通过某种方式(如全局状态管理、事件总线等)通知应用跳转到指定页面。

具体实现时,你可能会使用到HarmonyOS提供的页面跳转API,如router.push或类似方法,来根据点击事件中的信息跳转到目标页面。确保在跳转前,目标页面已经被正确注册在应用的路由表中。

最后,需要注意的是,由于应用可能处于后台或被系统杀死,点击通知时可能需要先恢复应用状态或重新创建应用实例,然后再执行页面跳转。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部