使用华为推送向服务器注册,并接收消息,处理消息点击事件。

华为推送是华为公司推出的,优秀的推送服务,在项目中,我使用了华为的推送服务,记录下来,供其他同学参考。

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。

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

}

 

    1. 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(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'  

猜你喜欢

转载自blog.csdn.net/mlsnatalie/article/details/81169018