华为推送是华为公司推出的,优秀的推送服务,在项目中,我使用了华为的推送服务,记录下来,供其他同学参考。
1.配置Maven库
在allprojects->repositories 里面配置HMSSDK的maven仓。
allprojects {
repositories {
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
}
2.添加依赖库
dependencies {
compile 'com.huawei.android.hms:push:{version}'
}
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。
android:name="com.huawei.hms.client.appid"
android:value="appid=xxx">
5.配置manifest文件
步骤 1 在application节点下增加APPID。(同2.3.4)
android:name="com.huawei.hms.client.appid"
android:value="appid=xxx">
步骤 2 在application节点下增加activity。
BridgeActivity定义了HMS-SDK中一些跳转所需要的透明页面。
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" >
android:value="androidhwext:style/Theme.Emui.Translucent" />
● AppUpdateActivity和PackageInstallActivity是应用自升级接口所需要使用的页面。
android:name="com.huawei.updatesdk.service.otaupdate.AppUpdateActivity"
android:configChanges="orientation|screenSize"
android:exported="false"
android:theme="@style/upsdkDlDialog" >
android:value="androidhwext:style/Theme.Emui.Translucent.NoTitleBar" />
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="false"
android:theme="@style/upsdkDlDialog" >
android:value="androidhwext:style/Theme.Emui.Translucent" />
步骤 3 在application节点下增加UpdateProvider,用于HMS-SDK引导升级HMS,提供给系统安装器读取升级文件。
android:name="com.huawei.hms.update.provider.UpdateProvider"
android:authorities="xxx.xxx.xxx.hms.update.provider"
android:exported="false"
android:grantUriPermissions="true" >
步骤 4 在application节点下增加UpdateSdkFileProvider,用于应用自升级。
如果应用或者游戏使用了如下接口,HMSAgent.checkUpdate(Activity activity,CheckUpdateHandler callback),则必须配置UpdateSdkFileProvider。
android:name="com.huawei.updatesdk.fileprovider.UpdateSdkFileProvider"
android:authorities="xxx.xxx.xxx.updateSdk.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
步骤 5 在application节点下增加Service,用于应用自升级。
步骤 6 在manifest节点下增加所需权限。
步骤 7 在application节点下增加2个receiver,用来接收广播信息。
用来接收PUSH消息的receiver:
接收通道发来的通知栏消息:
步骤 8 在application节点下注册android组件 。
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" >
android:value="androidhwext:style/Theme.Emui.Translucent" />
处理拉起支付界面,并处理支付结果的透明界面(集成支付时需要声明)
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" >
android:value="androidhwext:style/Theme.Emui.Translucent" />
处理拉起帐号登录界面,并处理登录结果的透明界面(集成帐号服务时需要声明)
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" >
android:value="androidhwext:style/Theme.Emui.Translucent" />
步骤 9 配置华为移动服务版本。
如果您的应用使用以下能力:
● 获取unionId。
● 使用getPurchaseInfo接口。
请在application节点下增加"com.huawei.hms.version"信息。
android:name="com.huawei.hms.version"
android:value="2.6.1">
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())
}
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(this@ChooseActivity, HuaWeiActivity::class.java)
startActivity(intent)
}
private fun geTui() {
val intent = Intent(this@ChooseActivity, MainActivity::class.java)
startActivity(intent)
}
private fun xiaoMi() {
val intent = Intent(this@ChooseActivity, 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'