XiaomiPushDemo【小米推送集成,基于V3.6.12版本】
版权声明:本文为haiyuking原创文章,转载请注明出处!
前言
这个demo只是记录小米推送的集成,不能运行。
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import r文件路径
2、 values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
2.1、接入准备
参考官网《小米推送服务启用指南》
注册小米开发者账号——》启用推送
2.2、下载sdk
下载地址:
下载后的压缩包解压后的目录:
2.3、集成sdk
为了便于管理,我在demo中新建了一个thirdlib的module,用于集成sdk。
(1)新建thirdlib的module,并在app的build.gradle中引用
//引用thirdlib implementation project(':thirdlib')
(2)在thirdlib这个module中集成sdk
复制mipush_sdk_client_x_x_x.jar到工程 libs/ 目录下;
因为是在thirdlib这个module中集成jar包,所以还需要在thirdlib这个module的build.gradle文件中引用libs目录下的jar包。
//小米推送sdk api files('libs/mipush_sdk_client_3_6_12.jar')
(3)在thirdlib这个module的res/strings.xml文件中添加以下代码(用于自定义的xiaomimessagereceiver中调用)
<resources> <string name="app_name">thirdlib</string> <!--=====================================小米推送sdk=====================================--> <string name="recv_passthrough_message"> receive a passthrough message. content is \"%1$s\"</string> <string name="click_notification_message"> clicked a notification message. content is \"%1$s\"</string> <string name="arrive_notification_message"> arrived a notification message. content is \"%1$s\"</string> <string name="register_success">register push success.</string> <string name="register_fail">register push fail.</string> <string name="set_alias_success"> set alias \"%1$s\" success.</string> <string name="set_alias_fail"> set alias fail for %1$s.</string> <string name="unset_alias_success"> unset alias \"%1$s\" success.</string> <string name="unset_alias_fail"> unset alias fail for %1$s.</string> <string name="set_account_success"> set account \"%1$s\" success.</string> <string name="set_account_fail"> set account fail for %1$s.</string> <string name="unset_account_success"> unset account \"%1$s\" success.</string> <string name="unset_account_fail"> unset account fail for %1$s.</string> <string name="subscribe_topic_success"> subscribe topic \"%1$s\" success.</string> <string name="subscribe_topic_fail"> subscribe topic fail for %1$s.</string> <string name="unsubscribe_topic_success"> unsubscribe topic \"%1$s\" success.</string> <string name="unsubscribe_topic_fail"> unsubscribe topic fail for %1$s.</string> <string name="set_accept_time_success"> set accept time %1$s - %2$s success.</string> <string name="set_accept_time_fail"> set accept time fail for %1$s.</string> </resources>
(4)配置 androidmanifest.xml【注意是app这个module中的,不是thirdlib这个module中的】
注意下面标记橙色代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.xiaomipushdemo"> <!-- ======================小米推送sdk====================== --> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.get_tasks" /> <!-- the following 2 com.xiaomi.mipushdemo should be changed to your package name --> <permission android:name="${applicationid}.permission.mipush_receive" android:protectionlevel="signature" /> <uses-permission android:name="${applicationid}.permission.mipush_receive" /> <uses-permission android:name="android.permission.vibrate" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <!-- ======================小米推送sdk========================== --> <service android:name="com.xiaomi.push.service.xmjobservice" android:enabled="true" android:exported="false" android:permission="android.permission.bind_job_service" android:process=":pushservice" /> <service android:name="com.xiaomi.push.service.xmpushservice" android:enabled="true" android:process=":pushservice" /> <service android:name="com.xiaomi.mipush.sdk.pushmessagehandler" android:enabled="true" android:exported="true" /> <service android:name="com.xiaomi.mipush.sdk.messagehandleservice" android:enabled="true" /> <!--自定义一个broadcastreceiver类:为了接收消息--> <receiver android:name="com.why.project.xiaomipushdemo.xiaomipush.xiaomimessagereceiver" android:exported="true"> <intent-filter> <action android:name="com.xiaomi.mipush.receive_message" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.message_arrived" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.error" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.networkstatusreceiver" android:exported="true"> <intent-filter> <action android:name="android.net.conn.connectivity_change" /> <category android:name="android.intent.category.default" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.pingreceiver" android:exported="false" android:process=":pushservice"> <intent-filter> <action android:name="com.xiaomi.push.ping_timer" /> </intent-filter> </receiver> </application> </manifest>
(5)在项目中添加xiaomipush包中的文件
1、permissionactivity中需要用到自定义myapplication中的代码,下一步中会增加,这里报错不用管;
2、xiaomimessagereceiver中使用的到字符串资源在thirdlib中的res/strings.xml文件中定义了;
3、xiaomimessagereceiver主要通知、消息的回调
(6)初始化sdk
在myapplication中执行
package com.why.project.xiaomipushdemo; import android.app.activitymanager; import android.app.application; import android.content.context; import android.os.process; import com.xiaomi.mipush.sdk.mipushclient; import java.util.list; /** * created by haiyuking * used */ public class myapplication extends application { /*=================小米推送sdk=====================*/ // user your appid the key. private static final string app_id = "28823037343464645735"; // user your appid the key. private static final string app_key = "56545654754865"; @override public void oncreate() { super.oncreate(); initxiaomipush(); } //小米推送sdk private void initxiaomipush(){ // 注册push服务,注册成功后会向demomessagereceiver发送广播 // 可以从demomessagereceiver的oncommandresult方法中mipushcommandmessage对象参数中获取注册信息 if (shouldinit()) { mipushclient.registerpush(this, app_id, app_key); } } //小米推送sdk【用于permissionactivity中调用】 public static void reinitpush(context ctx) { mipushclient.registerpush(ctx.getapplicationcontext(), app_id, app_key); } //小米推送sdk相关 private boolean shouldinit() { activitymanager am = ((activitymanager) getsystemservice(context.activity_service)); list<activitymanager.runningappprocessinfo> processinfos = am.getrunningappprocesses(); string mainprocessname = getpackagename(); int mypid = process.mypid(); for (activitymanager.runningappprocessinfo info : processinfos) { if (info.pid == mypid && mainprocessname.equals(info.processname)) { return true; } } return false; } }
三、使用方法(仅供参考)
package com.why.project.xiaomipushdemo; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.text.textutils; import com.xiaomi.mipush.sdk.mipushclient; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); /*=======================================小米推送sdk相关=============================================*/ updatejpushdeviceid(); } /*=======================================小米推送sdk相关=============================================*/ /**更新设备id接口*/ private void updatejpushdeviceid(){ //====小米推送sdk相关==== string regid = mipushclient.getregid(myapplication.getappcontext()); requestdeviceid(regid);//判断是请求接口还是弹出对话框 } //请求接口存储设备id或者token的方法 private void requestdeviceid(string regid) { //首要条件是设备id值或者token值不为空,否则下面的判断没有意义了 //如果没有设置过别名,或者则需要设置别名 //如果服务器上的deviceid值是空值,表明当前用户还没有绑定任何设备,则直接请求接口,不需要弹出对话框; //如果服务器上的deviceid值不为空,并且客户端获取的设备id值和服务器上的deviceid值相同,则不需要弹出对话框,直接请求接口(这个是卸载重新安装的情况) //如果服务器上的deviceid值不为空,并且客户端获取的设备id值和服务器上的deviceid值不同,则需要弹出对话框(这个是换设备的情况) if (!textutils.isempty(regid)) { //如果已经设置过别名(存储过了设备id值)了,但是当前的别名(设备id值)和服务器上的不一致,则需要重新设置别名(存储设备id值)(这个是其他设备上登录的情况) } //====小米推送sdk相关==== //貌似需要每一次都要设置别名 setalias(preferencesutils.getstring(mcontext,globals.username_key)); } // 这是来自 jpush example 的设置别名的 activity 里的代码。一般 app 的设置的调用入口,在任何方便的地方调用都可以。 private void setalias(string alias) { if (textutils.isempty(alias)) { toastutil.showshorttoast(getresources().getstring(r.string.error_alias_empty));//alias别名不能为空 return; } if (!exampleutil.isvalidtagandalias(alias)) { toastutil.showshorttoast(getresources().getstring(r.string.error_tag_gs_empty));//格式不对 return; } //====小米推送sdk相关==== mipushclient.setalias(myapplication.getappcontext(), alias, null); } }
四、发送消息
五、实现多个通知在通知栏中并存
六、实现打开应用内指定页面的效果
一般由应用客户端自定义即可,但是有可能会想要实现打开应用内指定页面。对应的网页上的设置:
6.1、添加xmpushactivity【一个透明界面,主要用于获取数据,封装数据,传输数据】
package com.why.project.xiaomipushdemo.xiaomipush; import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import com.why.project.xiaomipushdemo.r; import com.xiaomi.mipush.sdk.mipushmessage; import com.xiaomi.mipush.sdk.pushmessagehelper; import org.json.jsonobject; import cn.jpush.android.api.jpushinterface; /** * created by haiyuking * used 小米推送【打开应用内指定页面】【暂时用不到】 */ public class xmpushactivity extends activity { private static final string tag = xmpushactivity.class.getsimplename(); private context mcontext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_xmpush); mcontext = this; //获取自定义动作的值 intent intent = getintent(); string intenturi = intent.touri(intent.uri_intent_scheme); logutil.e(tag,"action是:" + intenturi); //intent:#intent;launchflags=0x10000000;package=com.why.project.xiaomipushdemo;component=com.why.project.xiaomipushdemo/.xiaomipush.xmpushactivity;s.messageid=sdm04994545992668152zh;i.eventmessagetype=1000;end /*获取自定义键值对的值*/ mipushmessage msgcontent = (mipushmessage) intent.getserializableextra(pushmessagehelper.key_message); //关闭当前界面,跳转到指定的界面 try { string title = msgcontent.gettitle(); string content = msgcontent.getcontent(); jsonobject extrajson = new jsonobject(msgcontent.getextra());//将map转成json对象 bundle bundle = new bundle(); bundle.putstring(jpushinterface.extra_notification_title,title);//通知的标题 bundle.putstring(jpushinterface.extra_alert,content);//通知内容 bundle.putstring(jpushinterface.extra_extra,extrajson.tostring());//通知附加字段 bundle.putint(jpushinterface.extra_notification_id,msgcontent.getnotifyid());//通知id值【没什么用】 intent i = new intent(mcontext, jpushactivity.class); i.putextras(bundle); i.setflags(intent.flag_activity_new_task);//必须使用这个,这个保证了多个通知,点击返回返回到的是上一个通知界面 mcontext.startactivity(i); finish(); } catch (exception e){ e.printstacktrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <!-- ======================小米推送sdk====================== --> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.constraint.constraintlayout>
6.2、在androidmanifest.xml中添加以下代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.xiaomipushdemo"> <!-- ======================小米推送sdk====================== --> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.get_tasks" /> <!-- the following 2 com.xiaomi.mipushdemo should be changed to your package name --> <permission android:name="${applicationid}.permission.mipush_receive" android:protectionlevel="signature" /> <uses-permission android:name="${applicationid}.permission.mipush_receive" /> <uses-permission android:name="android.permission.vibrate" /> <application android:name=".myapplication" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <!-- ======================小米推送sdk========================== --> <service android:name="com.xiaomi.push.service.xmjobservice" android:enabled="true" android:exported="false" android:permission="android.permission.bind_job_service" android:process=":pushservice" /> <service android:name="com.xiaomi.push.service.xmpushservice" android:enabled="true" android:process=":pushservice" /> <service android:name="com.xiaomi.mipush.sdk.pushmessagehandler" android:enabled="true" android:exported="true" /> <service android:name="com.xiaomi.mipush.sdk.messagehandleservice" android:enabled="true" /> <!--自定义一个broadcastreceiver类:为了接收消息--> <receiver android:name="com.why.project.xiaomipushdemo.xiaomipush.xiaomimessagereceiver" android:exported="true"> <intent-filter> <action android:name="com.xiaomi.mipush.receive_message" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.message_arrived" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.error" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.networkstatusreceiver" android:exported="true"> <intent-filter> <action android:name="android.net.conn.connectivity_change" /> <category android:name="android.intent.category.default" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.pingreceiver" android:exported="false" android:process=":pushservice"> <intent-filter> <action android:name="com.xiaomi.push.ping_timer" /> </intent-filter> </receiver> <!--小米推送【打开应用内指定页面】【暂时用不到】--> <!--intent:#intent;component=com.why.project.xiaomipushdemo/.xiaomipush.xmpushactivity;end--> <activity android:name="com.why.project.xiaomipushdemo.xiaomipush.xmpushactivity" android:theme="@android:style/theme.translucent"> </activity> </application> </manifest>
6.3、网页发送消息(关键部分)
intent:#intent;component=com.why.project.xiaomipushdemo/.xiaomipush.xmpushactivity;end
混淆配置
橙色标记的需要换成实际的路径:
#=====================小米推送sdk===================== #这里com.xiaomi.mipushdemo.demomessagerreceiver改成app中定义的完整类名 -keep class com.why.project.xiaomipushdemo.xiaomipush.xiaomimessagereceiver {*;} #可以防止一个误报的 warning 导致无法成功编译,如果编译使用的 android 版本是 23。 -dontwarn com.xiaomi.push.**
参考资料
暂时空缺
项目demo下载地址
链接:https://pan.baidu.com/s/1clztxbthigvgy0vzwwnnfq 提取码:gne6
上一篇: 定时实施php脚本
下一篇: Linux 命令 Knowhow