使用華為推送向伺服器註冊,並接收訊息,處理訊息點選事件。
華為推送是華為公司推出的,優秀的推送服務,在專案中,我使用了華為的推送服務,記錄下來,供其他同學參考。
1.配置Maven庫
在allprojects->repositories 裡面配置HMSSDK的maven倉。
allprojects {
repositories {
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
}
2.新增依賴庫
dependencies {
compile 'com.huawei.android.hms:push:
}
3.整合Agent
步驟 1 解壓包,生成目錄和檔案,包括HMSAgent的示例工程和指令碼檔案。
步驟 2 執行GetHMSAgent_xx.bat生成copysrc的資料夾,裡面是根據您選擇需要整合HMS服務,抽取後的HMSAgent程式碼(java 資料夾)和manifest檔案(AndroidManifest.xml)。
步驟 3 拷貝copysrc/java裡面的程式碼到您現有的工程(例如,在Eclipse環境下,請將程式碼拷貝至src目錄下)。請保持HMSAgent程式碼的包的路徑和結構不變。AndroidManifest.xml僅供配置參考。
Agent檔案下載
windows批處理指令碼,用於從示例程式碼中提取所需要模組的HMSAgent程式碼(只包含)需要的模組。請直接執行,根據提示操作即可。如果由於PC環境問題導致指令碼無法執行,請手動拷貝hmsagents\src\main\到 copysrc\。
4.配置華為引數
在application節點下增加APPID。
<meta-data
android:name="com.huawei.hms.client.appid"
<!-- value的值“xxx”用實際申請的應用ID替換,來源於開發者聯盟網站應用的服務詳情。-->
android:value="appid=xxx">
</meta-data>
5.配置manifest檔案
步驟 1 在application節點下增加APPID。(同2.3.4)
<meta-data
android:name="com.huawei.hms.client.appid"
<!-- value的值“xxx”用實際申請的應用ID替換,來源於開發者聯盟網站應用的服務詳情。-->
android:value="appid=xxx">
</meta-data>
步驟 2 在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>
步驟 3 在application節點下增加UpdateProvider,用於HMS-SDK引導升級HMS,提供給系統安裝器讀取升級檔案。
<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>
步驟 4 在application節點下增加UpdateSdkFileProvider,用於應用自升級。
如果應用或者遊戲使用瞭如下介面,HMSAgent.checkUpdate(Activity activity,CheckUpdateHandler callback),則必須配置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>
步驟 5 在application節點下增加Service,用於應用自升級。
<!-- 應用下載服務 -->
<service android:name="com.huawei.updatesdk.service.deamon.download.DownloadService"
android:exported="false"/>
步驟 6 在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" />
步驟 7 在application節點下增加2個receiver,用來接收廣播資訊。
用來接收PUSH訊息的receiver:
<!-- xxx.xx.xx為CP自定義的廣播名稱,比如: com.huawei.hmssample. HuaweiPushRevicer -->
<receiver android:name="xxx.xx.xx" >
<intent-filter>
<!-- 必須,用於接收TOKEN -->
<action android:name="com.huawei.android.push.intent.REGISTRATION" />
<!-- 必須,用於接收訊息 -->
<action android:name="com.huawei.android.push.intent.RECEIVE" />
<!-- 可選,用於點選通知欄或通知欄上的按鈕後觸發onEvent回撥 -->
<action android:name="com.huawei.android.push.intent.CLICK" />
<!-- 可選,檢視PUSH通道是否連線,不檢視則不需要 -->
<action android:name="com.huawei.intent.action.PUSH_STATE" />
</intent-filter>
</receiver>
接收通道發來的通知欄訊息:
<receiver android:name="com.huawei.hms.support.api.push.PushEventReceiver" >
<intent-filter>
<!-- 接收通道發來的通知欄訊息 -->
<action android:name="com.huawei.intent.action.PUSH" />
</intent-filter>
</receiver>
步驟 8 在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>
處理拉起支付介面,並處理支付結果的透明介面(整合支付時需要宣告)
<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>
處理拉起帳號登入介面,並處理登入結果的透明介面(整合帳號服務時需要宣告)
<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>
步驟 9 配置華為移動服務版本。
如果您的應用使用以下能力:
● 獲取unionId。
● 使用getPurchaseInfo介面。
請在application節點下增加"com.huawei.hms.version"資訊。
<meta-data
android:name="com.huawei.hms.version"
android:value="2.6.1">
</meta-data>
6.配置簽名
1、請在開發環境配置簽名證書,該配置屬於Android通用技術,此處不詳細描述。
2、請確認在華為開發者聯盟已經配置此簽名證書的指紋
7.
向後臺註冊個人賬號
獲取賬號:
val sp = getSharedPreferences("info", Context.MODE_PRIVATE)
val myToken = sp.getString("token", token)
Log.e("sds","send中token的值" + myToken)
json = JSONObject()
json.put("customerId", phoneNmuber)
json.put("deviceId", myToken)
json.put("supplierId", "203")
Log.e("sds","json的資訊2" + json.toString())
提交賬號
OkHttpUtils
.postString()
.url(NetApi.url)
.content(json.toString())
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.build()
.execute(MyStringCallback())
8.獲取點選事件,向後臺傳送回執
override fun onEvent(context: Context?, event: PushReceiver.Event?, extras: Bundle?) {
val intent = Intent()
intent.action = ACTION_UPDATE_UI
var notifyId = 0
if (PushReceiver.Event.NOTIFICATION_OPENED == event || PushReceiver.Event.NOTIFICATION_CLICK_BTN == event) {
notifyId = extras!!.getInt(PushReceiver.BOUND_KEY.pushNotifyId, 0)
if (0 != notifyId) {
val manager = context!!
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.cancel(notifyId)
}
}
val message = extras!!.getString(PushReceiver.BOUND_KEY.pushMsgKey)
intent.putExtra("log", "Received event,notifyId:$notifyId msg:$message")
callBack(intent)
super.onEvent(context, event, extras)
sp = DemoApplication.mContext!!.getSharedPreferences("huawei", Context.MODE_PRIVATE)
val id:String? = null
val deviceId = sp.getString("token",id)
val msg = message.substring(1,message.length-1)
val jo = JSONObject(msg)
val msgId = jo.getString("messageId")
json = JSONObject()
try {
json.put("deviceId", deviceId)
json.put("msgId", msgId)
} catch (e: JSONException) {
e.printStackTrace()
}
OkHttpUtils
.postString()
.url(NetApi.url_Send)
.content(json.toString())
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.build()
.execute(MyStringCallback())
}
-
- Android版本推送識別手機機型,並根據機型推送回執
class ChooseActivity : AppCompatActivity() {
private var brand: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_choose)
brand = android.os.Build.BRAND
when (brand) {
"HUAWEI" -> huaWei()
"Xiaomi" -> xiaoMi()
else -> geTui()
}
}
private fun huaWei() {
val intent = Intent([email protected], HuaWeiActivity::class.java)
startActivity(intent)
}
private fun geTui() {
val intent = Intent([email protected], MainActivity::class.java)
startActivity(intent)
}
private fun xiaoMi() {
val intent = Intent([email protected], MiActivity::class.java)
startActivity(intent)
}
}
說明:{version} 替換為實際的版本號,如:compile 'com.huawei.android.hms:push:2.5.2.300'
完成上述步驟,即可完成使用華為推送向後臺註冊賬號,並獲取處理點選事件的功能。
說明:{version} 替換為實際的版本號,如:compile 'com.huawei.android.hms:push:2.5.2.300'
說明:{version} 替換為實際的版本號,如:compile 'com.huawei.android.hms:push:2.5.2.300'