移动推送跟进各厂商通道推送服务的更新进度,提供集成华为推送 HMS Core Push SDK 的插件依赖包供用户选择使用。
注意:
- 华为推送只有在签名发布包环境下,才可注册厂商通道成功并通过厂商通道进行推送。
- 华为通道不支持抵达回调,支持点击回调。
获取 SHA256 证书指纹,并在华为推送平台中配置证书指纹,单击 保存。证书指纹获取可参见 生成签名证书指纹。
登录华为开放平台,进入我的项目> 选择项目 > 项目设置,下载华为应用最新配置文件 agconnect-services.json。
在 Android 项目级目录 build.gradle 文件,buildscript > repositories & dependencies 下分别添加华为仓库地址和 HMS gradle 插件依赖:
buildscript {
repositories {
google()
maven {url 'https://developer.huawei.com/repo/'} // 华为 maven 仓库地址
}
dependencies {
// 其他classpath配置
classpath 'com.huawei.agconnect:agcp:1.6.0.300' // 华为推送 gradle 插件依赖
}
}
在 Android 项目级目录 build.gradle 文件,allprojects>repositories下添加华为依赖仓库地址:
allprojects {
repositories {
google()
maven {url 'https://developer.huawei.com/repo/'} // 华为 maven 仓库地址
}
}
将从华为推送平台获取的应用配置文件 agconnect-services.json 拷贝到 app 模块目录下(请勿放在子模块下)。
在 app 模块下 build.gradle 文件头部添加以下配置(请勿放在子模块 build.gradle 下):
// app 其他 gradle 插件
apply plugin: 'com.huawei.agconnect' // HMS SDK gradle 插件
android {
// app 配置内容
}
在 app 模块下 build.gradle 文件内导入华为推送相关依赖:
dependencies {
// ... 程序其他依赖
implementation 'com.tencent.tpns:huawei:[VERSION]-release' // 华为推送 [VERSION] 为当前最新 SDK 版本号,版本号可在 Android SDK 发布动态 查看
implementation 'com.huawei.hms:push:6.5.0.300' // HMS Core Push 模块依赖包
}
说明:
- 华为推送 hms:push 依赖自6.1.300起适配 Android 11预置
<queries>
标签,请注意升级 Android Studio 至3.6.1或更高版本、Android Gradle 插件至 3.5.4或更高版本,否则可能导致工程构建出错。- 华为推送 [VERSION] 为当前最新 SDK 版本号,版本号可在 Android SDK 发布动态 查看。
- 移动推送 Android SDK 自1.2.1.3版本起正式支持华为推送 V5 版本,请使用1.2.1.3及以上版本的移动推送华为依赖以避免集成冲突问题。
针对开发者内部构建环境无法访问华为 maven 仓库的情况,提供以下手动集成方法。
下载 SDK 安装包。
打开 Other-Push-jar 文件夹, 导入 huaweiv5 推送相关依赖包,将全部 jar、aar 包复制到项目工程中。
在 Android 项目级目录 build.gradle 文件,buildscript > dependencies下添加 HMS gradle 插件的依赖:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
// 其他 classpath 配置
classpath files('app/libs/agcp-1.4.1.300.jar') // 华为推送 gradle 插件依赖
}
}
将从华为推送平台获取的应用配置文件 agconnect-services.json 拷贝到 app 模块目录下。
在 app 模块下 build.gradle 文件头部添加以下配置:
// app 其他 gradle 插件
apply plugin: 'com.huawei.agconnect' // HMS SDK V4 gradle 插件
android {
// app 配置内容
}
在 app 模块下 build.gradle 文件内导入华为推送相关依赖:
dependencies {
// ... 程序其他依赖
implementation files('libs/tpns-huaweiv5-1.2.1.1.jar') // 适用于 HMS Core 版本的 移动推送 插件
implementation fileTree(include: ['*.aar'], dir: 'libs') // HMS Core Push 模块依赖包
}
在 manifest 文件 <application> </application>
标签内添加以下组件:
<application>
<service
android:name="com.huawei.android.hms.tpns.HWHmsMessageService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
在调用移动推送注册接口 XGPushManager.registerPush
前,开启第三方推送接口:
//打开第三方推送
XGPushConfig.enableOtherPush(getApplicationContext(), true);
注册成功的日志如下:
V/TPush: [XGPushConfig] isUsedOtherPush:true
E/xg.vip: get otherpush errcode: errCode : 0 , errMsg : success
V/TPush: [XGPushConfig] isUsedOtherPush:true
I/TPush: [OtherPushClient] handleUpdateToken other push token is : IQAAAACy0PsqAADxfCrWG3kupbOraeAiYoo9n2B-bAfb2d--kctc8E_UnY_mrIdg9ionukZvC******dVD8GlJi_5-0rpskunnNMcat35HA other push type: huawei
将以下混淆规则添加在 App 项目级别的 proguard-rules.pro 文件中。
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}
-keep class com.huawei.agconnect.**{*;}
如应用使用了 AndResGuard 插件,请在 AndResGuard 配置白名单中添加以下内容。如果未使用 AndResGuard 插件,则请忽略该步骤。
whiteList = [
"R.string.hms*",
"R.string.connect_server_fail_prompt_toast",
"R.string.getting_message_fail_prompt_toast",
"R.string.no_available_network_prompt_toast",
"R.string.third_app_*",
"R.string.upsdk_*",
"R.layout.hms*",
"R.layout.upsdk_*",
"R.drawable.upsdk*",
"R.color.upsdk*",
"R.dimen.upsdk*",
"R.style.upsdk*",
"R.string.agc*"
]
说明:更多混淆配置请参见 华为推送配置混淆脚本。
华为通道抵达回执需要开发者自行配置,您可参见 华为厂商通道回执配置指引 进行配置。完成后,可在推送记录中查看华为推送通道的抵达数据。
华为设备支持设置应用角标,需要开发者申请应用内角标设置和设置应用启动类权限,详情请参见 角标适配指南 文档。
华为推送服务接入过程配置要求较严格,若观察到如下类似日志则说明华为厂商通道注册失败,开发者可以通过以下方式获取华为推送注册错误码:
[OtherPushClient] handleUpdateToken other push token is : other push type: huawei
推送服务 debug 模式下,过滤关键字“OtherPush”或“HMSSDK” ,查看相关返回码日志(例如 [OtherPushHuaWeiImpl] other push huawei onConnect code:907135702
),并前往 厂商通道注册失败排查指南 查找对应原因,获取解决办法。
华为推送从 EMUI 10.0版本开始将通知消息智能分成两个级别:一般与重要。EMUI 10.0之前的版本没有对通知消息进行分类,只有一个级别,消息全部通过“默认通知”渠道展示,等价于 EMUI 10.0的重要级别消息。若通知被归类为“一般”级别,则没有震动、响铃、和状态栏图标提示,目前可通过自定义通知渠道将消息级别设为“重要”;但遵照华为推送相关规则,最终展示效果仍将与华为推送智能分类计算出的级别共同决定,两者取低,例如重要与一般取一般。详情请参见 华为消息分类使用指南。
本页内容是否解决了您的问题?