HarmonyOS鸿蒙Next中APP接收不到离线消息,后台已配置
HarmonyOS鸿蒙Next中APP接收不到离线消息,后台已配置
UniPush 2.0的推送服务接入了华为推送厂商,目前离线消息接受不到
更多关于HarmonyOS鸿蒙Next中APP接收不到离线消息,后台已配置的实战教程也可以访问 https://www.itying.com/category-93-b0.html
您好,可以采取以下方式解决:
按以下步骤进行排查:
-
通知授权: 确保应用请求用户授权接收通知。如果没有明确的用户授权,应用可能被禁止接收通知。在通知发布前调用
requestEnableNotification()
方法,弹窗让用户选择是否允许发送通知,后续再次调用时,则不再弹窗。可通过requestEnableNotification
的错误码判断用户是否授权。若返回的错误码为1600004,即为拒绝授权。以下是开发步骤的示例代码,请参考:import { notificationManager } from '[@kit](/user/kit).NotificationKit'; import { BusinessError } from '[@kit](/user/kit).BasicServicesKit'; import { UIAbility } from '[@kit](/user/kit).AbilityKit'; import { window } from '[@kit](/user/kit).ArkUI'; import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit'; class MyAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', err); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', data); notificationManager.requestEnableNotification(this.context).then(() => { hilog.info(0x0000, 'testTag', `[ANS] requestEnableNotification success`); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); }); }); } }
-
消息类型和配置: 确认发送的消息类型和配置是否正确。例如,需要设置合适的category和push-type来确保消息被正确处理。同时,检查
testMessage
是否被设置为true,这是因为测试消息有特定的发送限制:- 当
testMessage
为true时,单个项目最多可推送1000条测试消息,每次推送携带Token数不超过10个。推送授权订阅消息与卡片刷新消息时,每次仅能携带一个Token。 - 若未申请通知消息自分类权益,则推送的通知消息默认为资讯营销类(category取值为MARKETING)消息;若仅需发送资讯营销类消息,则无需申请通知消息自分类权益。
- 当
-
PushToken: 确认应用已经成功获取并上传了PushToken。 PushToken标识了每台设备上每个应用,开发者调用
getToken()
接口向PushKit服务端请求PushToken,获取到之后使用PushToken来推送消息。 PushToken一般情况不会变化,仅下列场景Push Token会发生变化:- 卸载应用后重新安装。
- 设备恢复出厂设置。
- 应用显式调用
deleteToken()
接口后重新调用getToken()
接口。 - 应用显式调用
deleteAAID()
接口后重新调用getToken()
接口。
因此,建议在应用启动时调用
getToken()
接口,若设备的PushToken发生变化,及时上报到应用服务器更新PushToken。 以下是参考示例代码:// 导入pushService模块及相关公共模块 import { pushService } from '[@kit](/user/kit).PushKit'; import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit'; import { BusinessError } from '[@kit](/user/kit).BasicServicesKit'; // 建议在UIAbility(例如EntryAbility)的onCreate()方法中调用getToken()接口获取PushToken并上报到服务端,方便服务端向终端推送消息。 try { pushService.getToken().then((data: string) => { hilog.info(0x0000, 'testTag', 'Succeeded in getting push token.'); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', err.code, err.message); }); } catch (err) { let e: BusinessError = err as BusinessError; hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message); }
-
module.json5文件的配置问题: 检查项目模块级别下的src/main/module.json5中的
skills标签
配置,其中用于标识应用首页的skill(即配置了"entity.system.home"和"action.system.home"的skill)中不要配置uris。 module.json5文件中的skills标签下可以同时存在多个skill对象,每个对象对应一种能力。如果需要同时设置推送消息跳转能力和其他跳转能力(如NFC跳转、浏览器跳转等),需要在skills数组中创建不同的skill对象,分别映射对应的能力。一个skill中不建议配置多个action,否则可能导致无法匹配预期场景。
更多关于HarmonyOS鸿蒙Next中APP接收不到离线消息,后台已配置的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,若APP无法接收离线消息,首先检查后台配置是否正确,包括消息推送服务的权限和网络连接。其次,确保APP在后台运行时未被系统强制关闭或限制。此外,检查设备的省电模式是否影响了消息接收。如果问题依旧,建议更新APP至最新版本,或联系开发者获取技术支持。