Android詳細總結華為推送功能
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整合
下載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後再上傳到伺服器,華為官網說以後會改,期待下一個版本能有好的封裝吧。以下是華為官網給出的說明。哎!技術又差一截。