关闭应用时清除对应的通知栏,避免通知栏点击拉起错误

关闭应用时清除对应的通知栏,避免通知栏点击拉起错误

HarmonyOS
2024-05-28 21:04:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
NingJ

1.功能场景描述及使用场景

部分app在使用某些功能时会发送通知栏通知,比如文件管理的传输进度,若在传输过程中关闭app,点击通知栏重新拉起app可能会存在白屏bug,或者无法拉起的情况;从业务逻辑来讲,当我们关闭进程时是希望传输过程终止,并且清除通知栏。本demo用简单、基础的方式实现了清除通知栏。

2.使用的核心API

@ohos.notification

3.核心代码解释

//发送通知

import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; 
import Notification from '@ohos.notificationManager'; 
import CommonEventManager from '@ohos.commonEventManager'; 
const TAG = 'NotificationManager'; 
 
  /** 
   * 发送通知 
   * @param noticeId 通知ID 
   * @param messageData 通知内容参数 
   * @returns 
   */ 
  public async sentNoticeCommon(): Promise<void> { 
    let notificationRequest: Notification.NotificationRequest = await this.buildNotificationRequest(); 
    console.info(TAG, 'notification publish start'); 
    Notification.publish(notificationRequest).then(() => { 
      console.info(TAG, `notification publish success`); 
    }).catch((err: Error) => { 
      console.info(TAG, `notification publish has an error: ${JSON.stringify(err)}`); 
    }); 
  } 
private async getWantAgent(name: string, accountId: string, phoneNum: string, comId: string, threadId: number): Promise<WantAgent | null> { 
    let wantAgentInfo: wantAgent.WantAgentInfo = { 
      wants: [ 
        { 
          bundleName: 'com.example.myapplication', 
          abilityName: 'EntryAbility', 
          parameters: { 
            action: 'MSG_NOTIFY_CLICK', 
            isNotice: true, 
            name: name, 
            accountId: accountId, 
            phoneNum: phoneNum, 
            comId: comId, 
            thread_id: threadId 
          } 
        } 
      ], 
      operationType: wantAgent.OperationType.START_SERVICE, 
      requestCode: 200 
    } 
    try { 
      return await wantAgent.getWantAgent(wantAgentInfo); 
    } catch (err) { 
      console.error(TAG, `get want agent has an error: ${JSON.stringify(err)}.`); 
      return null; 
    } 
  } 
 
  private async getReplyWantAgent(name: string, accountId: string, phoneNum: string, comId: string, threadId: number): Promise<WantAgent | null> { 
    let wantAgentInfo: wantAgent.WantAgentInfo = { 
      wants: [ 
        { 
          bundleName: 'com.example.myapplication', 
          abilityName: 'EntryAbility', 
          parameters: { 
            action: 'MSG_NOTIFY_CLICK', 
            isNotice: true, 
            name: name, 
            accountId: accountId, 
            phoneNum: phoneNum, 
            comId: comId, 
            thread_id: threadId, 
            reply: true 
          } 
        } 
      ], 
      operationType: wantAgent.OperationType.START_SERVICE, 
      requestCode: 200 
    } 
    try { 
      return await wantAgent.getWantAgent(wantAgentInfo); 
    } catch (err) { 
      console.error(TAG, `get want agent has an error`); 
      return null; 
    } 
  } 
 
  private  getMarkReadWantAgent(threadId: number): Promise<WantAgent | null> { 
    let wantAgentInfo: wantAgent.WantAgentInfo = { 
      wants: [ 
        { 
          bundleName: 'com.example.myapplication', 
          abilityName: 'EntryAbility', 
          action: 'TEST_MEETIME_SERVICE_MESSAGE_NOTICE_MARK_READ', 
          parameters: { 
            threadId: threadId 
          } 
        } 
      ], 
 
 
      operationType: wantAgent.OperationType.SEND_COMMON_EVENT, 
      requestCode: 200 
    } 
 
 
    try { 
      return  wantAgent.getWantAgent(wantAgentInfo); 
    } catch (err) { 
      console.error(TAG, `get mark read want agent has an error: ${JSON.stringify(err)}.`); 
      return null; 
    } 
  } 
 
 
 
  private buildActionButtons(replyWantAgent: WantAgent | null, markReadWantAgent: WantAgent | null): Array<Notification.NotificationActionButton> { 
    let actionButtons: Array<Notification.NotificationActionButton> = new Array<Notification.NotificationActionButton>(); 
    if (replyWantAgent) { 
      actionButtons.push({ 
        title: '回复', wantAgent: replyWantAgent 
      }); 
    } 
    if (markReadWantAgent) { 
      actionButtons.push({ 
        title: '标记已读', wantAgent: markReadWantAgent 
      }); 
    } 
 
    return actionButtons; 
  } 
}

//在进程关闭时清除对应通知

import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
import Notification from '@ohos.notificationManager'; 
import PublicEventHandle from '../manager/PublicEventHandle'; 
import hilog from '@ohos.hilog'; 
 
const TAG = 'EntryAbility'; 
 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 
 
    hilog.info(0x0000, 'testTag33', '%{public}s', 'oncreate1111'); 
    PublicEventHandle.getInstance().initEventSubscribe(); 
  } 
 
  onDestroy() { 
    console.info(TAG, 'onDestroy');//通过通知ID取消已发布的通知 
    Notification.cancel(1) 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage) { 
    console.info(TAG, 'onWindowStageCreate'); 
 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        console.info(TAG, 'onWindowStageCreate'); 
        return; 
      } 
      console.info(TAG, 'onWindowStageCreate'); 
    }); 
  } 
 
  onWindowStageDestroy() { 
    console.info(TAG, 'onWindowStageDestroy'); 
  } 
 
  onForeground() { 
    console.info(TAG, 'onForeground'); 
  } 
 
  onBackground() { 
    console.info(TAG, 'onBackground'); 
  } 
}

4.实现效果

关闭应用,对应通知栏清除。

5.适配的版本信息

  • IDE:DevEco Studio 4.0.3.600
  • SDK:4.0(API10)(HMS Core 4.0.0.40)(OpenHarmony 4.0.10.11)
分享
微博
QQ
微信
回复
2024-05-29 22:07:05
相关问题
通知点击事件监听方法
294浏览 • 1回复 待解决
怎么获取是否开启推送通知权限
2206浏览 • 1回复 待解决
通知里边快捷键重叠了
5204浏览 • 1回复 待解决
如何做到点击通知消息打开应用
390浏览 • 1回复 待解决
华为事件通知接口返回10008错误
9104浏览 • 2回复 待解决
鸿蒙应用如何实现本地通知?
40浏览 • 0回复 待解决
通知图标支持格式
1767浏览 • 1回复 待解决