Android集成Huawei PUSH(三)——集成工程
一、集成SDK
这里使用的开发环境是Android Studio,集成方式为Gradle + maven。
具体步骤如下:
1.打开项目的build.gradle文件
2.在allprojects->repositories文件中配置HMS SDK的maven仓。
allprojects {
repositories {
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
}
3.打开子工程app文件夹下的build.gradle文件
4.在dependencies中配置编译依赖
dependencies {
compile 'com.huawei.android.hms:push:2.6.3.301'
}
说明:Huawei PUSH 2.6.3.301版本的依赖所需的Android SDK最小版本为16,如果小于16,则需要将defaultConfig中的minSdkVersion修改为16。
5.点击Sync Now链接重新build安卓项目,等待同步完成。
说明:如果Sync出现错误,则需要检查网络连接是否正常,并且检查gradle文件是否正确。
注意:这里是Gradle + maven集成方法。另外两种方法(Eclipse+ADT开发环境、通用包)的集成方法请参考:https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_devprepare_agent
-----------------------------------------------------------------------------------------
二、集成HMS SDK Agent
具体步骤如下:
1.解压下载的HMSAgent_2.6.3.301.zip压缩包
2.双击GetHMSAgent_cn.bat文件运行脚本
3.根据脚本中的提示生成HMSAgent代码和manifest文件(这里进集成了Push):
4.在HMSAgent_2.6.301/copysrc文件夹中可以看到生成的代码:
5.拷贝copysrc/java里面的代码到现有的工程
-----------------------------------------------------------------------------------------
三、配置Manifest.xml文件
找到并配置Manifest.xml文件,具体步骤如下:
1.在application节点下增加APPID
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="appid=100954437">
</meta-data>
2.配置application的name属性
<application
<!-- “xxx”用实际的应用包名替换-->
android:name="xxx.xxx.xxx.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
3.在application节点下增加BridgeActivity,用来定义HMS SDK中一些跳转所需要的透明页面
<activity
android:name="com.huawei.hms.activity.BridgeActivity"
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
android:excludeFromRecents="true"
android:exported="false"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
4.在application节点下增加AppUpdateActivity和PackageInstallActivity,用来是应用自升级接口所需要使用的页面
<activity
android:name="com.huawei.updatesdk.service.otaupdate.AppUpdateActivity"
android:configChanges="orientation|screenSize"
android:exported="false"
android:theme="@style/upsdkDlDialog" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent.NoTitleBar" />
</activity>
<activity
android:name="com.huawei.updatesdk.support.pm.PackageInstallerActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="false"
android:theme="@style/upsdkDlDialog" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
5.在application节点下增加UpdateProvider,用于HMS SDK引导升级HMS APK,提供给系统安装器读取升级文件
<provider
android:name="com.huawei.hms.update.provider.UpdateProvider"
<!--“xxx.xxx.xxx”用实际的应用包名替换-->
android:authorities="xxx.xxx.xxx.hms.update.provider"
android:exported="false"
android:grantUriPermissions="true" >
</provider>
6.在application节点下增加UpdateSdkFileProvider,用于应用自升级
<provider
android:name="com.huawei.updatesdk.fileprovider.UpdateSdkFileProvider"
<!--“xxx.xxx.xxx”用实际的应用包名替换-->
android:authorities="xxx.xxx.xxx.updateSdk.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
</provider>
7.在application节点下增加Service,用于应用自升级
<!-- 应用下载服务 -->
<service android:name="com.huawei.updatesdk.service.deamon.download.DownloadService"
android:exported="false"/>
8.在manifest节点下增加集成Huawei PUSH 所需的权限
<!--HMS-SDK引导升级HMS功能,访问OTA服务器需要网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--HMS-SDK引导升级HMS功能,保存下载的升级包需要SD卡写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--检测网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--检测wifi状态-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!--为了获取用户手机的IMEI,用来唯一的标识用户。-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!--如果是安卓8.0,应用编译配置的targetSdkVersion>=26,请务必添加以下权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- 接收PUSH TOKEN的广播以及PUSH消息需要定义该权限 ${PACKAGE_NAME} 要替换上您应用的包名 -->
<permission
android:name="${PACKAGE_NAME}.permission.PROCESS_PUSH_MSG"
android:protectionLevel="signatureOrSystem"/>
<!--接收PUSH TOKEN的广播以及PUSH消息需要定义该权限 ${PACKAGE_NAME} 要替换上您应用的包名 -->
<uses-permission android:name="${PACKAGE_NAME}.permission.PROCESS_PUSH_MSG" />
9.在application节点下声明receiver,用于接收PUSH Token、透传消息和通知栏点击消息
注意:接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(注册、透传消息、通知栏点击事件)广播,此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类
<!--“xxx”用实际的类名替换, ${PACKAGE_NAME} 要替换上您应用的包名-->
<receiver android:name="xxx"
android:permission="${PACKAGE_NAME}.permission.PROCESS_PUSH_MSG">
<intent-filter>
<!-- 必须,用于接收token -->
<action android:name="com.huawei.android.push.intent.REGISTRATION" />
<!-- 必须, 用于接收透传消息 -->
<action android:name="com.huawei.android.push.intent.RECEIVE" />
<!-- 必须, 用于接收通知栏消息点击事件 此事件不需要开发者处理,只需注册就可以-->
<action android:name="com.huawei.intent.action.PUSH_DELAY_NOTIFY"/>
</intent-filter>
</receiver>
10.在application节点下声明receiver,用于点击通知栏或通知栏上按钮后触发onEvent回调(此步骤非必选)
注意:这个通知会在后续版本中逐渐废弃,请开发者谨慎使用
接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(点击通知栏或通知栏上的按钮后触发onEvent回调、查看push通道是否连接)广播, 此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类
<!--“xxx”用实际的类名替换, ${PACKAGE_NAME} 要替换上您应用的包名-->
<receiver android:name="xxxx">
<intent-filter>
<!-- 用于点击通知栏或通知栏上的按钮后触发onEvent回调 -->
<action android:name="com.huawei.android.push.intent.CLICK" />
<!-- 查看push通道是否连接, 不查看则不需要 -->
<action android:name="com.huawei.intent.action.PUSH_STATE"/>
</intent-filter>
</receiver>
11.如果应用需要在非华为设备上使用PUSH,需要声明HmsMsgService,在application节点上声明HMSMsgService
<!--接入HMSSDK PUSH模块需要注册该service,不需要开发者处理-->
<service
android:name="com.huawei.hms.support.api.push.service.HmsMsgService"
android:enabled="true"
android:exported="true"
android:process=":pushservice">
<intent-filter>
<action android:name="com.huawei.push.msg.NOTIFY_MSG" />
<action android:name="com.huawei.push.msg.PASSBY_MSG" />
</intent-filter>
</service>
12.在application节点下注册HMSAgentActivity组件,用于解决华为移动服务升级问题的透明界面
<activity
android:name="com.huawei.android.hms.agent.common.HMSAgentActivity"
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
android:excludeFromRecents="true"
android:exported="false"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
13.如果应用继承了华为支付服务时,在application节点下注册HMSPayAgentActivity组件,用于处理拉起支付界面,并处理支付结果的透明界面
<activity
android:name="com.huawei.android.hms.agent.pay.HMSPayAgentActivity"
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
android:excludeFromRecents="true"
android:exported="false"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
14.如果应用集成了华为账号服务时,在application节点下注册HMSSignInAgentActivity组件,用于处理拉起帐号登录界面,并处理登录结果的透明界面
<activity
android:name="com.huawei.android.hms.agent.hwid.HMSSignInAgentActivity"
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
android:excludeFromRecents="true"
android:exported="false"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" >
<meta-data
android:name="hwc-theme"
android:value="androidhwext:style/Theme.Emui.Translucent" />
</activity>
15.在application节点下增加配置华为移动服务版本com.huawei.hms.version信息
<meta-data
android:name="com.huawei.hms.version"
android:value="2.6.1">
</meta-data>
16.在混淆配置文件(*.pro)中添加排除HMS的混淆配置
-ignorewarning
-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.android.hms.agent.**{*;}