HarmonyOS 鸿蒙Next Harmony启动backgroundtaskmanager长时任务之后,发布Notification时概率性失败

HarmonyOS 鸿蒙Next Harmony启动backgroundtaskmanager长时任务之后,发布Notification时概率性失败

前提条件:按照backgroundtaskmanager之中的描述,启动一个后台长时任务,然后执行下载逻辑。在下载的过程之中,定时发布本地Notification。

问题描述:在发布Notification时,会概率性出现以下错误信息:

本人代码如下:

// 启动长时任务逻辑 
private startContinuousTask() {
  if (this.sdkInfo == null) {
    return;
  }
  let context = getContext();
  let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
  let bundleName = bundleInfo.name;

  let wantAgentInfo: wantAgent.WantAgentInfo = {
    // 点击通知后,将要执行的动作列表
    // 添加需要被拉起应用的bundleName和abilityName
    wants: [
      {
        bundleName: bundleName,
        abilityName: "EntryAbility"
      }
    ],
    // 指定点击通知栏消息后的动作是拉起ability
    actionType: wantAgent.OperationType.START_ABILITY,
    // 使用者自定义的一个私有值
    requestCode: 0,
    // 点击通知后,动作执行属性
    actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  };

  DownloadLogger.debug(DownloadUIHandler.TAG, `startContinuousTask start, bundleName = ${bundleName}`);

  wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
    let list: Array<string> = ["dataTransfer"];
    backgroundTaskManager.startBackgroundRunning(context, list, wantAgentObj).then((notification) => {
      DownloadLogger.debug(DownloadUIHandler.TAG, `startContinuousTask success. notificationId=${notification.notificationId}`);
      if (this.sdkInfo != null) {
        this.sdkInfo.notificationId = notification.notificationId;
      }
    })
  });
}

// notification发布逻辑
public publishNotification() {
  if (this.sdkInfo == null) {
    return;
  }

  let enabled = notificationManager.isNotificationEnabledSync();
  if (!enabled) {
    DownloadLogger.debug(DownloadUIHandler.TAG, `publishNotification enabled=${enabled}`);
    return;
  }

  let notificationId = this.sdkInfo.notificationId;
  let runningTask = this.sdkInfo.getRunningTask();
  if (runningTask == null) {
    return;
  }

  let progress = runningTask.downloadedSize * 100 / runningTask.totalDownloadSize;
  let title = this.sdkInfo.notificationTitle.downloading;
  DownloadLogger.debug(DownloadUIHandler.TAG, `publishNotification progress=${progress}, title=${title}`)

  let downLoadTemplate: notificationManager.NotificationTemplate = {
    name: 'downloadTemplate', // 当前只支持downloadTemplate,保持不变
    data: {
      title: title, // 必填。
      fileName: '文件下载', // 必填。
      progressValue: progress, // 应用更新进度值,自定义。
    }
  };

  let request: notificationManager.NotificationRequest = {
    content: {
      // 系统实况类型,保持不变
      notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW,
      systemLiveView: {
        typeCode: 8, // 上传下载类型需要填写 8,当前仅支持此类型。保持不变
        title: title, // 应用自定义
        text: "文件下载", // 应用自定义,
        additionalText: "..."
      }
    },
    id: notificationId, // 必须是申请长时任务返回的id,否则应用更新通知失败。
    notificationSlotType: notificationManager.SlotType.LIVE_VIEW, // 实况窗类型,保持不变
    template: downLoadTemplate // 应用需要设置的模版名称
  };
  try {
    notificationManager.publish(request).then(() => {
      DownloadLogger.debug(DownloadUIHandler.TAG, `publish success, taskId=${runningTask?.taskId} , notificationId=${notificationId}`)
    }).catch((err: BusinessError) => {
      DownloadLogger.debug(DownloadUIHandler.TAG, `publish fail, taskId=${runningTask?.taskId} , err=${err.message}`)
    });
  } catch (err) {
    DownloadLogger.debug(DownloadUIHandler.TAG, `publish fail, taskId=${runningTask?.taskId} , err=${err.toString()}`)
  }
}

问题复现情况:偶现问题。在首次安装App时,出现的概率稍微高一些,但也是偶现的。

系统信息: ​​​​​​​


更多关于HarmonyOS 鸿蒙Next Harmony启动backgroundtaskmanager长时任务之后,发布Notification时概率性失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS 鸿蒙Next Harmony启动backgroundtaskmanager长时任务之后,发布Notification时概率性失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,BackgroundTaskManager用于管理长时任务,而Notification用于向用户发送通知。当系统资源紧张或任务调度优先级发生变化时,Notification的发布可能会概率性失败。此外,BackgroundTaskManager中长时任务的执行可能占用较多系统资源,影响Notification的正常发布。建议检查系统日志以确认资源占用情况,并确保在发布Notification时,系统资源处于可用状态。

回到顶部