美文网首页工作生活
华为推送集成记录

华为推送集成记录

作者: 陽光亽活力 | 来源:发表于2019-06-30 15:08 被阅读0次

一、官方文档

https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_devprepare_agent

1、注册成为开发者

2、配置应用签名

3、创建产品和应用

4、开通推送服务

5、下载SDK(下方高能注意)

image.png

1、下载解压之后是下图这样

image.png

什么鬼下载其他的压缩包都是XXX.jar的文件这个什么。
2、注意这个文件

image.png

打开之后发现这个是这个文件的说明
此目录下包含的几个特殊文件夹和文件作用说明如下:

HMSAgent 相关:
1、config 文件夹
        集成HMS SDK的应用可以参考AppManifestConfig.xml配置AndroidManifest.xml
2、hmsagents 文件夹
        agent 代码模块(包含所有HMS模块)
3、tool 文件夹
        同目录下批处理执行需要的工具包
4、GetHMSAgent_*.bat、GetHMSAgent_*.sh 脚本文件
        从agent 代码模块(hmsagents 文件夹)中抽取需要模块的agent代码。抽取后的代码放在了同目录下的copysrc目录下
        GetHMSAgent_cn.bat为中文脚本
        GetHMSAgent_oversea.bat为英文脚本
5、Buildcopysrc2jar.bat、Buildcopysrc2jar.sh 批处理文件
        用来将GetHMSAgent.bat 生成的代码(copysrc目录下)编译成jar包。 仅必须使用jar包的场景才使用此脚本,建议按步骤4直接拷贝代码。

我们运行 GetHMSAgent_cn.bat 文件按照指示输入appID等信息
发现在统计目录下生成copysrc文件里面的文件是我们需要的,拷贝文件到工程中就可以使用了。

image.png

6、集成工程

1、 AndroidStudio开发环境

Gradle+maven集成方式
步骤1 配置maven仓。
打开项目的build.gradle文件,如图3-1框中的文件。
图3-1 项目的build.gradle位置

image.png

步骤2、在allprojects->repositories 里面配置HMS SDK的maven仓。

        allprojects {
            repositories {
                jcenter()
                maven {url 'http://developer.huawei.com/repo/'}
            }
        }    

步骤3、配置项目依赖。
1. 打开子工程app下的build.gradle文件,如图3-2框中的文件。
图3-2 子工程的build.gradle位置在示例图

image.png

步骤4、 配置编译依赖。

  dependencies {
    compile 'com.huawei.android.hms:push:{版本}'         
  }      

2、 配置manifest文件

步骤 1 (必选)在application节点下增加APPID

<meta-data  
    android:name="com.huawei.hms.client.appid"  
    <!-- value的值“xxx”用实际申请的应用ID替换,来源于开发者联盟网站应用的服务详情。-->  
    android:value="appid=xxx">  
</meta-data>    

步骤 2 (必选)配置application的name属性
用于在application启动的时候调用HMSAgent.init()接口初始化HMS Agent套件,开发者需自己实现应用的MyApplication类

<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节点下增加activity。
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>

(必选) 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>    

步骤 4 (必选)在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>

步骤 5 在application节点下增加UpdateSdkFileProvider,用于应用自升级。
如果应用或者游戏使用了如下接口,HMSAgent.checkUpdate(Activity activity,CheckUpdateHandler callback),则必选。

<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>

步骤 6 (必选)在application节点下增加Service,用于应用自升级

<service android:name="com.huawei.updatesdk.service.deamon.download.DownloadService"
    android:exported="false"/>   

步骤 7 (必选)在manifest节点下增加所需权限

<!--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" />

步骤 8 (必选)在application节点下声明2个receiver和1个service,用于接收广播信息。用于接收PUSH Token,透传消息和通知栏点击消息用来接收PUSH消息的receiver:

  <!-- 接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(注册、透传消息、通知栏点击事件)广播,此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类,参考示例代码中的类:com.huawei.hmsagent.HuaweiPushRevicer-->
  <!--“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>

(可选)用于点击通知栏或通知栏上按钮后触发onEvent回调这个通知会在后续版本中逐渐废弃,请开发者谨慎使用(不用怎么能点击通知栏回调)

<!--如下2个通知会在以后的版本中逐渐废弃 
            接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(点击通知栏或通知栏上的按钮后触发onEvent回调、查看push通道是否连接)广播,          此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类,参考示例代码中的类:com.huawei.hmsagent.HuaweiPushRevicer类 -->

  <!--“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>

步骤 9(必选) 在application节点下注册android组件 。解决华为移动服务升级问题的透明界面

<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> 

相关文章

网友评论

    本文标题:华为推送集成记录

    本文链接:https://www.haomeiwen.com/subject/gvavcctx.html