首页 > 其他分享 >HarmonyOS 通知意图

HarmonyOS 通知意图

时间:2024-03-20 19:34:06浏览次数:22  
标签:notificationManager console 意图 wantAgent 通知 ids HarmonyOS import

之前的文章 我们讲了 harmonyos 中的 基础和进度条通知
那么 今天 我们来说说 任何给通知添加意图

通知意图 简单说 就是 当我们点击某个通知 如下图
在这里插入图片描述
然后 就会拉起某个 应用
在这里插入图片描述
就例如说 我们某个微信好友发消息给我们 我们 点击系统通知 可以直接跳到你们的聊天界面

好 回到编辑器 我们编写组件代码如下

import notificationManager from "@ohos.notificationManager"
@Entry
@Component
struct Dom {

  @State ids:number = 0;

  aboutToAppear(){
  }

  build() {
    Column({space: 30}) {
      Button("发送基本文本通知").onClick(()=>{
        let request: notificationManager.NotificationRequest = {
          id: this.ids++,
          content:{
            contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
            normal:{
              title:'通知标题'+ this.ids,
              text:'通知内容详情',
              additionalText: '通知附加内容'
            }
          }
        }
        notificationManager.publish(request).then(()=> {
          console.log('发送通知成功')
        }).catch(reason => {
          console.log('发送通知失败',JSON.stringify(reason))
        })
      })
    }
    .width('100%')
    .height('100%')
  }
}

这就是 最基本的 一个通知模板 之前我们也讲过了

然后 我们改写代码如下

import notificationManager from "@ohos.notificationManager"
import wantAgent, { WantAgent } from "@ohos.app.ability.wantAgent";
@Entry
@Component
struct Dom {

  @State ids:number = 0;
  wantAgentInstance: WantAgent
  aboutToAppear(){
  }

  build() {
    Column({space: 30}) {
      Button("发送基本文本通知").onClick(async ()=>{
        let wantInfo:wantAgent.WantAgentInfo = {
            wants:[{
              bundleName: "com.example.sediment",
              abilityName:'EntryAbility'
            }],
            requestCode:0,
            operationType:wantAgent.OperationType.START_ABILITY,
            wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
        }
        let wantAgentInstance = await wantAgent.getWantAgent(wantInfo);
        let request: notificationManager.NotificationRequest = {
          id: this.ids++,
          wantAgent: wantAgentInstance,
          content:{
            contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
            normal:{
              title:'通知标题'+ this.ids,
              text:'通知内容详情',
              additionalText: '通知附加内容'
            }
          }
        }
        notificationManager.publish(request).then(()=> {
          console.log('发送通知成功')
        }).catch(reason => {
          console.log('发送通知失败',JSON.stringify(reason))
        })
      })
    }
    .width('100%')
    .height('100%')
  }
}

首先 是导包

import wantAgent, { WantAgent } from "@ohos.app.ability.wantAgent";

然后 我们声明对象
bundleName 你要哪个应用?
我们应用的 app.json5 中 就有
在这里插入图片描述
然后 abilityName 要用 哪个 Ability 写入口文件的名字上去就好了
在这里插入图片描述
按我这样写 绑定给通知 请求对象的 wantAgent属性就好了

然后 我们运行代码

然后 我们点出通知 后 将应用切到后台 然后点击通知
在这里插入图片描述
你会发现 直接就跳转进来了
在这里插入图片描述

标签:notificationManager,console,意图,wantAgent,通知,ids,HarmonyOS,import
From: https://blog.csdn.net/weixin_45966674/article/details/136847402

相关文章

  • HarmonyOS鸿蒙开发常用组件详细说明(图片、文本、按钮、弹窗、进度条、文本框)
    常用组件一直会分享,虽然鸿蒙目前来没有多大发展,但不可否然以后发展,华为的技术是一大突破,存在即合理可以现在没有多大发展。但不可否定未来的发展。关注’猿来编码‘,微信订阅号,回复’组件‘,获取文章目录常用组件关注’猿来编码‘,微信订阅号,回复’组件‘,获取1、图片......
  • 鸿蒙HarmonyOS实战-工具安装和Helloworld案例
    ......
  • 3.19 HarmonyOS 网络请求工具类
    网络请求部分的代码实在是太麻烦了,所以把他封装成一个工具类使用起来方便些今天先封装两个,一个是post把集合传给后端,一个是get查询全部importhttpfrom'@ohos.net.http';classAxiosUtil{privatehttpRequest=http.createHttp();constructor(){//用于订阅HTTP......
  • HarmonyOS应用开发实战 - Api9 拍照、拍视频、选择图片、选择视频、选择文件工具类
    鸿蒙开发过程中,经常会进行系统调用,拍照、拍视频、选择图库图片、选择图库视频、选择文件。今天就给大家分享一个工具类。1.话不多说,先展示样式2.设计思路根据官方提供的指南开发工具类,基础的拍照、拍视频、图库选照片、选文件不过多缀述,图库选择这里设计成集合形式,可返......
  • HarmonyOS-基础之内置组件学习
    1、Image图片组件鸿蒙内置组件Image的4种写法//鸿蒙内置Image图片组件案例@Entry@ComponentstructImagePage{build(){Column({space:20}){//1、图片的第一种写法media文件夹下Image($r('app.media.pig')).width(200).height(200);//2......
  • 零基础小白如何入门HarmonyOS鸿蒙应用开发学习?
    HarmonyOS鸿蒙应用开发是当前非常热门的一个领域,许多人都想入门学习这个技术。但是,对于零基础的人来说,如何入门确实是一个问题。下面,我将从以下几个方面来介绍如何零基础入门HarmonyOS鸿蒙应用开发学习。一、了解HarmonyOS鸿蒙系统首先,我们需要了解HarmonyOS鸿蒙系统的一些......
  • 微信小程序/公众号推送服务通知功能实现(公众号)
    #微信小程序/公众号推送服务通知   第一部分只介绍公众号,小程序的在第二部分再行介绍A、前期准备工作        1、准备好已经通审核及认证成功公众号    2、登录公众号平台    3、打开设置与开发中的基本配置        4、记录好开发者......
  • HarmonyOS 与 ArkTS | 自定义组件
    HarmonyOS与ArkTS|自定义组件原效果:代码:@Entry@ComponentstructIndex{build(){Column(){Row(){Column(){this.ItemCard($rawfile('ic_controlcenter_eyeconfort_filled.png'))Divider().margin({top......
  • HarmonyOS 与 ArkTS | ForEach 循环渲染 + List 实现滑动视频列表
    HarmonyOS与ArkTS|ForEach循环渲染+List实现滑动视频列表本文为记录,内容较简单,无注释。实现效果:代码:importimagefrom'@ohos.multimedia.image'classItem{name:stringclassification:stringimage:ResourceStrconstructor(name:string,classi......
  • MediaBox音视频终端SDK已适配鸿蒙星河版(HarmonyOS NEXT)
    2024年1月,HarmonyOS NEXT 鸿蒙星河版系统开发者预览版开放申请,该系统将只能安装为鸿蒙开发的原生应用,而不再兼容安卓应用。对此,阿里云MediaBox音视频终端SDK产品已实现功能的鸿蒙化迁移和重构,全面适配鸿蒙系统HarmonyOS NEXT系统。 当前, 阿里云播放器SDK预览版已面向所有......