在HarmonyOS鸿蒙Next的Stage模型中启动长时任务,任务启动成功,但没有文档中说的通知栏显示“正在xxx任务”通知
在HarmonyOS鸿蒙Next的Stage模型中启动长时任务,任务启动成功,但没有文档中说的通知栏显示“正在xxx任务”通知 在Stage模型中启动长时任务,任务启动成功,但没有文档中说的通知栏显示“正在xxx任务”通知,在任务启动成功后,紧接着执行的订阅定位更改,应用在前端时,每隔10秒有定位更新打印,但应用进入后台后,定位打印就没有了;
module.json5
中权限申请如下:
{
"requestPermissions": [
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
},
{
"name": "ohos.permission.LOCATION"
},
{
"name": "ohos.permission.APPROXIMATELY_LOCATION"
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND"
}
]
}
abilities
:
{
"name": "EntryAbility",
"backgroundModes": ["location"]
}
EntryAbility.ts
中
onWindowStageCreate(windowStage: window.WindowStage): void {
windowStage.loadContent('pages/Index', (err, data) => {})
}
'pages/Index.ets
中
onPageShow() {
// 向用户请求定位权限
// 获取权限后,启动服务(依照稳定代码 https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/continuous-task-0000001711672320-V2)
// startContinuousTask()主要代码
let wantAgentInfo: wantAgent.WantAgentInfo = {
// 点击通知后,将要执行的动作列表
// 添加需要被拉起应用的bundleName和abilityName
wants: [
{
bundleName: "com.sabri.myapp",
abilityName: "com.sabri.myapp.EntryAbility"
}
],
// 指定点击通知栏消息后的动作是拉起ability
operationType: wantAgent.OperationType.START_ABILITY,
// 使用者自定义的一个私有值
requestCode: 0,
// 点击通知后,动作执行属性
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
}
// 通过wantAgent模块下getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
backgroundTaskManager.startBackgroundRunning(
this.context,
backgroundTaskManager.BackgroundMode.LOCATION,
wantAgentObj
).then(() => {
console.info(`--MyApp--Succeeded in operationing startBackgroundRunning.`);
this.startLocation(); // 不知道在这里开始订阅定位对不对,需要后台执行的代码到底要放在哪里?
}).catch((err) => {
console.error(`--MyApp--Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
});
}).catch((err) => {
console.info(`--MyApp------wantAgent.getWantAgent.err==-----`, err);
})
// startLocation()主要代码
let requestInfo = {
'scenario': geoLocationManager.LocationRequestScenario.NAVIGATION,
'timeInterval': 1,
'distanceInterval': 0,
'maxAccuracy': 0
};
let locationChange = (location) => {
console.log('--MyApp--locationChanger: data: ' + JSON.stringify(location));
};
try {
geoLocationManager.on('locationChange', requestInfo, locationChange);
} catch (err) {
console.log('--MyApp--location.on: error=' + err);
}
更多关于在HarmonyOS鸿蒙Next的Stage模型中启动长时任务,任务启动成功,但没有文档中说的通知栏显示“正在xxx任务”通知的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你好,这个问题建议附上代码通过在线提单进一步解决:https://developer.huawei.com/consumer/cn/support/feedback/#/,感谢您的反馈和支持
更多关于在HarmonyOS鸿蒙Next的Stage模型中启动长时任务,任务启动成功,但没有文档中说的通知栏显示“正在xxx任务”通知的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
好的,我提工单试试。
sets() {
let wantAgentInfo: wantAgent.WantAgentInfo = {
// 点击通知后,将要执行的动作列表
// 添加需要被拉起应用的bundleName和abilityName
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.EntryAbility"
}
],
// 指定点击通知栏消息后的动作是拉起ability
operationType: wantAgent.OperationType.START_ABILITY,
// 使用者自定义的一个私有值
requestCode: 0,
// 点击通知后,动作执行属性
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
// 通过wantAgent模块下getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
backgroundTaskManager.startBackgroundRunning(
this.context,
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
console.info(`--MyApp--Succeeded in operationing startBackgroundRunning.`);
this.startLocation();//不知道在这里开始订阅定位对不对,需要后台执行的代码到底要放在哪里?
}).catch((err) => {
console.error(`--MyApp--Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
});
}).catch((err) => {
console.info(`--MyApp------wantAgent.getWantAgent.err==-----`, err);
})
}
startLocation(){
let requestInfo = { 'scenario': geoLocationManager.LocationRequestScenario.NAVIGATION
, 'timeInterval': 1
, 'distanceInterval': 0
, 'maxAccuracy': 0 };
let locationChange = (location) => {
console.log('--MyApp--locationChanger: data: ' + JSON.stringify(location));
};
try {
geoLocationManager.on('locationChange', requestInfo, locationChange);
} catch (err) {
console.log('--MyApp--location.on: error=' + err);
}
}
ohos.permission.APPROXIMATELY_LOCATION
// api9及以上,申请LOCATION必须同时申请该权限ohos.permission.LOCATION_IN_BACKGROUND
// 后台定位ohos.permission.KEEP_BACKGROUND_RUNNING
ohos.permission.LOCATION
我点击时调用、进入后台时自动调用都试过了,都是转入后台一会(大概5s)定位图标就不显示了,也不打印。
仔细对照了你的代码,除了bundleName,abilityName不一样,其他都一样的,包名与ability应该是没有写错的,如果写错了wantAgent.getWantAgent都启动不了的,服务也启动不了的,我的服务是启动成功了的。
我的设备是Mate40Pro,HarmonyOs 4.0.0.132,你的是系统版本是多少?
帮我看看我的问题在哪里呢
import common from '@ohos.app.ability.common';
import { Want } from '@ohos.app.ability.Want';
import { WantAgent, OperationType, WantAgentFlags } from '@ohos.app.ability.wantAgent';
import { backgroundTaskManager, BackgroundMode } from '@ohos.resourceschedule.backgroundTaskManager';
import { AbilityStage } from '@ohos.app.ability.AbilityStage';
import { geoLocationManager, LocationRequestScenario } from '@ohos.geoLocationManager';
export default class VoipContinueTask {
message: string = 'VoipContinuousTask';
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
constructor(context: common.UIAbilityContext) {
this.context = context
this.startContinuousTask();
}
startContinuousTask(): void {
let wantAgentObj: WantAgent = null;
let wantAgentInfo = {
deviceId: '',
wants: [
{
bundleName: "com.voip.test",
abilityName: "com.voip.test.EntryAbility"
}
],
operationType: WantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
try {
backgroundTaskManager.startBackgroundRunning(
this.context,
backgroundTaskManager.BackgroundMode.LOCATION,
wantAgentObj).then(() => {
console.info(`Succeeded in voip startBackgroundRunning.`);
this.startLocation();
}).catch((err) => {
console.error(`Failed to operation voip startBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
console.error(`Failed to start background running. Code is ${error.code} message is ${error.message}`);
}
});
}
stopContinuousTask() :void{
try {
backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
console.info(`Succeeded in operationing stopBackgroundRunning.`);
}).catch((err) => {
console.error(`Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
console.error(`Failed to stop background running. Code is ${error.code} message is ${error.message}`);
}
}
startLocation(){
let requestInfo = { 'scenario': geoLocationManager.LocationRequestScenario.NAVIGATION
, 'timeInterval': 1
, 'distanceInterval': 0
, 'maxAccuracy': 0 };
let locationChange = (location) => {
console.log('--MyApp--locationChanger: data: ' + JSON.stringify(location));
};
try {
geoLocationManager.on('locationChange', requestInfo, locationChange);
} catch (err) {
console.log('--MyApp--location.on: error=' + err);
}
}
}
这个backgrounmode没有voip。。。。。先用location试试能不能后台长跑吧
调用的地方,从示例的chat项目改动过来
struct Index {
@StorageLink('page_layout') page_layout: boolean | undefined = AppStorage.Get('page_layout'); // AppStorage中key对应的属性建立双向数据同步
@State currentIndex: number = TAB_INDEX_0; // tabBar初始选中的位置
@State message: Resource = $r('app.string.chat');
private controller: TabsController = new TabsController();
myVoip:VoipContinueTask=new VoipContinueTask(getContext() as common.UIAbilityContext);
// 设置tabBar样式的位置、未选中图片、选中图片及文字显示的参数
@Builder
TabBuilder(index: number, imageNormal: Resource, imageSelected: Resource, text: Resource) {
Column() {
Divider()
.vertical(false)
.strokeWidth(DIVIDER_LINE_STROKWIDTH)
.color($r('app.color.tab_bar_separator_line_color'))
.opacity(TABBAR_SEPARATE_LINE)
Image(this.currentIndex === index ? imageSelected : imageNormal)
.syncLoad(true)
.width($r('app.integer.tabbar_image_width'))
.height($r('app.integer.tabbar_image_height'))
.margin($r('app.integer.tabbar_image_margin'))
.objectFit(ImageFit.Contain)
Text(text)
.fontSize($r('app.integer.tabbar_item_font_size'))
.fontColor(this.currentIndex === index ?
$r('app.color.tabbar_font_color_selected') : $r('app.color.tabbar_font_color_normal'));
}
.height($r('app.string.layout_100'))
.width($r('app.string.layout_100'))
}
}
类型
"backgroundModes": ["location"]
权限
"requestPermissions": [
{
"name": "ohos.permission.INTERNET",
"reason": "$string:permission_internet",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "inuse"
}
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason" : "$string:PjsipService_desc",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
},
{"name": "ohos.permission.APPROXIMATELY_LOCATION"},//api9及以上,申请LOCATION必须同时申请该权限
{"name": "ohos.permission.LOCATION_IN_BACKGROUND"},//后台定位
{ "name": "ohos.permission.LOCATION"}
]
我需要的是程序一启动,voip的线程就要在后台长时间运行,目前这样它不报错,就是每次运行长时间任务都失败,然后报错如下,都还没到执行定位代码那段
BussinessError 9800006: Notification verification failed. The title or text of the notification cannot be empty.
我们现在遇到的问题是,通知栏的确显示了通知,但很快就自己消失了。
到现在也没搞清楚到底是什么情况,不知道是不是系统Bug,
总的来说,HarmonyOS是一款非常优秀的操作系统,期待它能在未来带给我们更多惊喜!
我的也有这个问题,不知道你的解决了吗
我申请的后台音乐播放长时任务,日志打印都是成功的,
然后插上usb链接线有后台播放,不插就没有,
并且通知栏也没有图标显示。
配置一切都正确,按照demo上的来看,日志也是打印成功的
期待HarmonyOS能在未来推出更多针对特定场景的优化功能。
音乐播放我也简单试过,好像不用服务,转入后台后也能继续播放,
那个没用,播放5分钟就停了,不能一直播放!然后现在申请了长时任务后,切到后台直接没声音!搞不懂,
我可以定位了,但是不显示通知栏消息。但是有定位常驻的一个图标。
这个权限有加的,后台长时任务也能跑起来,但应用转入后台后,定位就不工作了(locationChange)没有打印了;
定位常驻图标,只要开启定位就有(不开长时任务也有),但应用转入后台,过一会儿图标会消失,也没有定位打印信息了,
你是按home键吗?在后台。
首先感谢!!
audioRecording应该是对应后台录音的,我是需要后台定位,对应location
包名是我项目中的包名,
在HarmonyOS鸿蒙Next的Stage模型中,长时任务启动后未显示通知栏提示,可能是由于以下原因:
-
权限问题:确保应用已获取
ohos.permission.KEEP_BACKGROUND_RUNNING
权限,该权限允许应用在后台执行长时任务。 -
通知配置:检查应用的通知配置,确保在
config.json
文件中正确配置了通知权限,并且应用在运行时请求了通知权限。 -
任务类型:确认启动的长时任务类型是否支持通知栏提示。某些任务类型可能不会自动生成通知。
-
系统版本:确认设备运行的HarmonyOS版本是否支持该功能。不同版本可能存在功能差异。
-
代码实现:检查代码中是否正确实现了长时任务的启动逻辑,确保任务启动后触发了通知栏提示的相关代码。
-
系统限制:某些设备或系统设置可能会限制后台任务的通知显示,检查设备设置中是否允许应用显示通知。
-
日志排查:通过查看系统日志或应用日志,确认任务启动过程中是否有异常或错误信息,帮助定位问题。
如果以上检查均无问题,建议进一步排查应用代码和系统环境,确保长时任务的通知栏提示功能正常实现。
在HarmonyOS鸿蒙Next的Stage模型中启动长时任务时,如果任务启动成功但没有显示通知栏提示,可能是以下原因:
-
权限问题:确保应用已获取
ohos.permission.KEEP_BACKGROUND_RUNNING
权限,并在config.json
中声明。 -
通知配置:检查是否在任务启动时正确配置了通知栏提示,使用
NotificationRequest
设置标题和内容。 -
任务类型:确认任务类型是否支持通知栏显示,某些任务类型可能默认不显示通知。
-
系统版本:确保设备系统版本支持该功能,部分功能可能仅在特定版本中生效。
-
日志排查:通过
HiLog
查看任务启动日志,确认任务是否真正启动成功。
建议逐一排查以上问题,确保配置正确。