Android详细总结华为推送功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/f552126367/article/details/84180870

1、前言

Android集成推送功能也算是有好几个SDK了,包括极光、个推、小米。但是真心感觉华为的文档写的太差,封装的也不好,别的开发文档看一两遍就能按照文档集成成功,而华为仔细看了几遍都没完全搞懂要干啥。还去查了一下别人的封装过程才看懂。本来还对中国第一大品牌抱有很大期望,看了以后感觉有点差啊,开发水平明显赶不上其它几大推送啊。经历千辛万苦终于集成完成。

小米推送集成的文章:https://blog.csdn.net/f552126367/article/details/84065630

完美推送功能总结+极光推送的文章:https://blog.csdn.net/f552126367/article/details/79961260

2、集成华为推送功能

1)去华为开发者联盟注册一个账号,开通Push服务

方式:管理中心--开发服务下的PUSH---申请PUSH服务,如下图:

3、SDK集成

(1)去官网https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_sdkdownload_agent

下载HMSAgent_2.6.1.302.zip,和HMS_SDK_2.6.1.301.zip

官网说可以通过maven的形式进行下载,但是真的不好使,因为打开他给的网站都是空的。

(2)直接解压 HMS_SDK_2.6.1.301.zip,将里面的HMS_SDK_2.6.1.301.jar包复制到自己的工程libs下,将assest复制到自己的工程中。res文件夹下可以不复制(调试语言用的)。

(3)解压HMSAgent_2.6.1.302.zip,双击GetHMSAgent_cn.bat文件,会提示你需要什么功能,按照提示选择是或者否就行,完成以后会出现一个copysrc文件夹,将java中com下的所有class复制到自己工程的com下。到这里

4、代码集成

(1)需要自己写接收方式

package com.huawei.android.hms.agent.push;

import android.content.Context;
import android.os.Bundle;

import com.huawei.hms.support.api.push.PushReceiver;

/**
 * Created by fei on 2018/11/17.
 */

public class HUAWEIPushRevicer extends PushReceiver {

    @Override
    public void onToken(Context context, String token, Bundle extras) {
        String value=extras.getString("");

    }
}

(2)androidMainfest.xml下需要填写的内容

 <!--华为推送服务的配置 -->
        <!-- value的值“xxx”用实际申请的应用ID替换,来源于开发者联盟网站应用的服务详情。-->
        <meta-data
            android:name="com.huawei.hms.client.appid"
            android:value="appid=100494377">
    </meta-data>
        <provider
            android:name="com.huawei.hms.update.provider.UpdateProvider"
            android:authorities="com.fei.main.hms.update.provider"
            android:exported="false"
            android:grantUriPermissions="true"/>

        <!-- 接入HMSSDK 需要注册的provider,authorities 一定不能与其他应用一样,所以这边 com.fei.main 要替换上您应用的包名
            Access HMSSDK need to register provider,authorities must not be the same as other applications, so this side ${package_name} to replace the package name you applied-->
        <provider
            android:name="com.huawei.updatesdk.fileprovider.UpdateSdkFileProvider"
            android:authorities="com.fei.main.updateSdk.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
        </provider>

        <!--<activity android:name=".OpendeviceActivity"/>-->

        <!-- 使用 HMSAgent 代码接入HMSSDK 需要注册的activity | Use hmsagent code to access HMSSDK activity that requires registration-->
        <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>




        <!-- 接入HMSSDK 需要注册的activity | Access HMSSDK activity to be registered-->
        <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>

        <!-- 接入HMSSDK 需要注册的activity | Access HMSSDK activity to be registered-->
        <activity
            android:name="com.huawei.updatesdk.service.otaupdate.AppUpdateActivity"
            android:configChanges="orientation|screenSize"
            android:exported="false" >
            <meta-data
                android:name="hwc-theme"
                android:value="androidhwext:style/Theme.Emui.Translucent.NoTitleBar" />
        </activity>

        <!-- 接入HMSSDK 需要注册的activity | Access HMSSDK activity to be registered-->
        <activity
            android:name="com.huawei.updatesdk.support.pm.PackageInstallerActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:exported="false" >
            <meta-data
                android:name="hwc-theme"
                android:value="androidhwext:style/Theme.Emui.Translucent" />
        </activity>

        <!-- 接入HMSSDK PUSH模块需要注册,第三方相关 :接收Push消息(注册、Push消息、Push连接状态)广播,
                此receiver类需要开发者自己创建并继承com.huawei.hms.support.api.push.PushReceiver类,
                参考示例代码中的类:com.huawei.hmsagent.HuaweiPushRevicer
            Access to the HMSSDK push module requires registration:
	            Receive push message (registration, push message, push connection state) broadcast.
	            This receiver class requires the developer to create and inherit the com.huawei.hms.support.api.push.PushReceiver class.
	            Reference to class in sample code: Com.huawei.hmsagent.HuaweiPushRevicer-->
        <receiver android:name="com.huawei.android.hms.agent.push.HUAWEIPushRevicer" >
            <intent-filter>
                <!-- 必须,用于接收token | Must, for receiving token -->
                <action android:name="com.huawei.android.push.intent.REGISTRATION" />
                <!-- 必须,用于接收消息 | Must, used to receive messages-->
                <action android:name="com.huawei.android.push.intent.RECEIVE" />
                <!-- 可选,用于点击通知栏或通知栏上的按钮后触发onEvent回调 | Optional, click the button on the notification bar or the notification bar to trigger the onevent callback -->
                <action android:name="com.huawei.android.push.intent.CLICK" />
                <!-- 可选,查看push通道是否连接,不查看则不需要 | Optional, query whether the push channel is connected or not -->
                <action android:name="com.huawei.intent.action.PUSH_STATE" />
            </intent-filter>
        </receiver>

        <!-- 接入HMSSDK PUSH模块需要注册 :接收通道发来的通知栏消息 | The access HMSSDK push module needs to be registered: the notification bar message sent from the receiving channel -->
        <receiver android:name="com.huawei.hms.support.api.push.PushEventReceiver" >
            <intent-filter>
                <action android:name="com.huawei.intent.action.PUSH" />
            </intent-filter>
        </receiver>

        <!-- 接入HMSSDK 需要注册的应用下载服务 | Access HMSSDK need to register app download service-->
        <service android:name="com.huawei.updatesdk.service.deamon.download.DownloadService"
            android:exported="false"/>

(3)在Application中初始化SDK

 HMSAgent.init(this);

(4)在Activity中连接华为服务器并且获得token(根据这个token后台可以指定某台设备进行发送)

    /*
    * 连接华为服务器
    * */
    fun connectHuaWei(){
        HMSAgent.connect(this) { rst -> showToastShort("HMS connect end:" + rst) }
    }


    private fun getToken() {
        HMSAgent.Push.getToken(object : GetTokenHandler {
            override fun onResult(rst: Int) {
                showToastShort("get token: end" + rst)
            }
        })
    }

(5)大功告成,其实回顾起来并不是特别难,但是华为文档将好多功能放到一起,看看看着就容易蒙,还需要自己在工程中拷贝大量class类,而且正常好的方式应该是在网页选择需要的功能然后生成一个demo进行下载,而华为还需要自己生成,让一开始集成的人不知道干啥。

5、总结

虽然遇到了点问题,但还好解决了,华为存在着不能自己上传token的问题,只能获得token后再上传到服务器,华为官网说以后会改,期待下一个版本能有好的封装吧。以下是华为官网给出的说明。哎!技术又差一截。

猜你喜欢

转载自blog.csdn.net/f552126367/article/details/84180870
今日推荐