• 欢迎访问风的记忆博客网站,如有疑问请加作者QQ或者微信联系。作者QQ:524100248,微信号:sendtion。

Android集成“小米消息推送”详细教程

安卓教程 sendtion 8年前 (2016-11-30) 8580次浏览 已收录 7个评论 扫描二维码
文章目录[隐藏]

消息推送这个玩意,大部分 APP 都有的,你像 QQ、淘宝、微博、支付宝、网易新闻等,他们的消息推送简直伤心病狂。

常见的第三方消息推送方案有这些:

  • 极光推送
  • 友盟推送
  • 个推
  • 小米推送
  • 腾讯信鸽
  • 百度云推送
  • 阿里云推送

今天不讲别的,就讲讲小米推送的集成教程,说实话挺简单的,步骤也不多,推送效果尚可。

1、下载小米消息推送 SDK

下载地址:http://dev.xiaomi.com/mipush/downpage/

里面包括 Eclipse 和 Android Studio 开发 Demo,包括 SDK jar 包。

小米推送服务 Android 版客户端 SDK 使用指南:http://dev.xiaomi.com/doc/?p=544

2、注册小米账号,并在小米开放平台创建应用

创建应用地址:http://dev.xiaomi.com/myitems

我的应用包名:com.sdc.cfm

创建应用后会得到一些 APPID,APPKey,APPSecret 等信息。

3、参照 Android Studio 的开发 demo 进行集成

先把 jar 包拷贝到项目的 libs 文件夹,然后选中 jar 包右键选择“add library”引入。

4、打开项目的 AndroidManifest.xml 文件,把 Demo 中使用的权限拷贝进去。

把权限中“com.xiaomi.mipushdemo”包名替换成自己项目的包名,比如我的包名:com.sdc.cfm


1
2
3
4
5
6
7
8
9
10
11
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.INTERNET"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_NETWORK_STATE"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_WIFI_STATE"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.READ_PHONE_STATE"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.GET_TASKS"</span> /&gt;</span>
<span class="hljs-comment">&lt;!-- the following 2 com.xiaomi.mipushdemo should be changed to your package name --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.sdc.cfm.permission.MIPUSH_RECEIVE"</span>
            <span class="hljs-attribute">android:protectionLevel</span>=<span class="hljs-value">"signature"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.sdc.cfm.permission.MIPUSH_RECEIVE"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.VIBRATE"</span> /&gt;</span>

5、把 AndroidManifest.xml 文件中注册的 service 和 receiver 拷贝到自己项目中。

把 name 中“com.xiaomi.mipushdemo”包名替换成自己项目的包名。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<span class="hljs-tag">&lt;<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.push.service.XMJobService"</span>
    <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"false"</span>
    <span class="hljs-attribute">android:permission</span>=<span class="hljs-value">"android.permission.BIND_JOB_SERVICE"</span>
    <span class="hljs-attribute">android:process</span>=<span class="hljs-value">":pushservice"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.push.service.XMPushService"</span>
    <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"true"</span>
    <span class="hljs-attribute">android:process</span>=<span class="hljs-value">":pushservice"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.mipush.sdk.PushMessageHandler"</span>
    <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"true"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.mipush.sdk.MessageHandleService"</span>
    <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"true"</span> /&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">receiver</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.sdc.cfm.DemoMessageReceiver"</span>
    <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"true"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">intent-filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.mipush.RECEIVE_MESSAGE"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">intent-filter</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">intent-filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.mipush.MESSAGE_ARRIVED"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">intent-filter</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">intent-filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.mipush.ERROR"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">intent-filter</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">receiver</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">receiver</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.push.service.receivers.NetworkStatusReceiver"</span>
    <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"true"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">intent-filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.net.conn.CONNECTIVITY_CHANGE"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">category</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.category.DEFAULT"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">intent-filter</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">receiver</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">receiver</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.push.service.receivers.PingReceiver"</span>
    <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"false"</span>
    <span class="hljs-attribute">android:process</span>=<span class="hljs-value">":pushservice"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">intent-filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.xiaomi.push.PING_TIMER"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">intent-filter</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">receiver</span>&gt;</span>

6、参照 DemoApplication.java 文件在自己的 Application 文件中初始化小米推送。

比如我的 Application 文件为 MyApplication,记得要在 AndroidManifest.xml 中命名。

在 oncreate()方法中调用 initXiaomiPush()方法。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<span class="hljs-comment">//小米消息推送APP_ID ,APP_KEY </span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String APP_ID = <span class="hljs-string">" "</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String APP_KEY = <span class="hljs-string">" "</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> DemoHandler sHandler = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> MainActivity sMainActivity = <span class="hljs-keyword">null</span>;

<span class="hljs-annotation">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">super</span>.onCreate();
    initXiaomiPush();
}

<span class="hljs-comment">/** 初始化小米消息推送 **/</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initXiaomiPush</span><span class="hljs-params">()</span></span>{
    <span class="hljs-comment">// 注册push服务,注册成功后会向DemoMessageReceiver发送广播 </span>
    <span class="hljs-comment">// 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息 </span>
    <span class="hljs-keyword">if</span> (shouldInit()) {
        MiPushClient.registerPush(<span class="hljs-keyword">this</span>, APP_ID, APP_KEY);
    }

    LoggerInterface newLogger = <span class="hljs-keyword">new</span> LoggerInterface() {
        <span class="hljs-annotation">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setTag</span><span class="hljs-params">(String tag)</span> </span>{
            <span class="hljs-comment">// ignore </span>
        }
        <span class="hljs-annotation">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">log</span><span class="hljs-params">(String content, Throwable t)</span> </span>{
            Log.d(TAG, content, t);
        }
        <span class="hljs-annotation">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">log</span><span class="hljs-params">(String content)</span> </span>{
            Log.d(TAG, content);
        }
    };

    Logger.setLogger(<span class="hljs-keyword">this</span>, newLogger);
    <span class="hljs-keyword">if</span> (sHandler == <span class="hljs-keyword">null</span>) {
        sHandler = <span class="hljs-keyword">new</span> DemoHandler(getApplicationContext());
    }
}

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">shouldInit</span><span class="hljs-params">()</span> </span>{
    ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
    List&lt;ActivityManager.RunningAppProcessInfo&gt; processInfos = am.getRunningAppProcesses();
    String mainProcessName = getPackageName();
    <span class="hljs-keyword">int</span> myPid = Process.myPid();
    <span class="hljs-keyword">for</span> (ActivityManager.RunningAppProcessInfo info : processInfos) {
        <span class="hljs-keyword">if</span> (info.pid == myPid &amp;&amp; mainProcessName.equals(info.processName)) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> DemoHandler <span class="hljs-title">getHandler</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">return</span> sHandler;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMainActivity</span><span class="hljs-params">(MainActivity activity)</span> </span>{
sMainActivity = activity;
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoHandler</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Handler</span> </span>{
    <span class="hljs-keyword">private</span> Context context;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">DemoHandler</span><span class="hljs-params">(Context context)</span> </span>{
        <span class="hljs-keyword">this</span>.context = context;
    }
    <span class="hljs-annotation">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleMessage</span><span class="hljs-params">(Message msg)</span> </span>{
        String s = (String) msg.obj;
        <span class="hljs-keyword">if</span> (sMainActivity != <span class="hljs-keyword">null</span>) {
        sMainActivity.refreshLogInfo();
    }
    <span class="hljs-keyword">if</span> (!TextUtils.isEmpty(s)) {
        Toast.makeText(context, s, Toast.LENGTH_LONG).show();
     }
}

7、把 DemoMessageReceiver.java 文件拷贝到项目中。

然后,修改 AndroidManifest.xml 文件中 DemoMessageReceiver 注册的名字,包名修改为自己项目的包名。

比如,我的包名:com.sdc.cfm


1
<span class="hljs-rule"><span class="hljs-attribute">android</span>:<span class="hljs-value">name=<span class="hljs-string">"com.sdc.cfm.receiver.DemoMessageReceiver"</span></span></span>

8、在 MainActivity 中设置别名等识别标识。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<span class="hljs-annotation">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{    
    <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyApplication.setMainActivity(<span class="hljs-keyword">this</span>);
    <span class="hljs-comment">//设置别名,撤销别名(alias) </span>
    MiPushClient.setAlias(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"demo1"</span>, <span class="hljs-keyword">null</span>);
    <span class="hljs-comment">//MiPushClient.unsetAlias(MainActivity.this, "demo1", null); </span>
    <span class="hljs-comment">//设置账号,撤销账号(account) </span>
    MiPushClient.setUserAccount(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"user1"</span>, <span class="hljs-keyword">null</span>);
    <span class="hljs-comment">//MiPushClient.unsetUserAccount(MainActivity.this, "user1", null); </span>
    <span class="hljs-comment">//设置标签,撤销标签(topic:话题、主题) </span>
    MiPushClient.subscribe(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"IT"</span>, <span class="hljs-keyword">null</span>);
    <span class="hljs-comment">//MiPushClient.unsubscribe(MainActivity.this, "IT", null); </span>
    <span class="hljs-comment">//设置接收时间(startHour, startMin, endHour, endMin) </span>
    MiPushClient.setAcceptTime(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">23</span>, <span class="hljs-number">0</span>, <span class="hljs-keyword">null</span>);
    <span class="hljs-comment">//暂停和恢复推送 //MiPushClient.pausePush(MainActivity.this, null); </span>
    <span class="hljs-comment">//MiPushClient.resumePush(MainActivity.this, null);</span>
}

<span class="hljs-annotation">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDestroy</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">super</span>.onDestroy();
    MyApplication.setMainActivity(<span class="hljs-keyword">null</span>);
}

9、运行自己的项目,并在小米推送后台设置推送消息,等待 APP 接收消息。

设置推送消息:http://admin.xmpush.xiaomi.com/v2/tool/nb

10、到此集成完毕,如果没有意外,会很快收到推送的消息。

推送统计:http://admin.xmpush.xiaomi.com/v2/statistics/push

 

如有疑问,欢迎指正!

个人简介:

GitHub:https://github.com/sendtion

个人博客:http://sendtion.cn/

简书:http://www.jianshu.com/users/630d98ed1424


风的记忆 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Android 集成“小米消息推送”详细教程
喜欢 (4)
[sendtion@126.com]
分享 (0)
sendtion
关于作者:
一个不断奋斗追逐梦想的少年~
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(7)个小伙伴在吐槽
  1. 回复测试
    sendtion2017-06-05 16:45 回复 未知操作系统 | 未知浏览器
  2. 畅言测试
    sendtion2017-06-05 16:44 回复 未知操作系统 | 未知浏览器
  3. IG7感谢分享 祝您开心快乐每一天!昊林打样PCB 欢迎回访: http://www.hldypcb.com/
    www.hldypcb.com2017-04-08 17:18 回复 未知操作系统 | 未知浏览器
  4. 学习
    一声菊响2016-12-31 11:33 回复 未知操作系统 | 未知浏览器
    • 你做出来了?
      张海洋2017-07-17 19:58 回复 未知操作系统 | 未知浏览器
  5. 最美的风景是代码
    胡杨2016-12-05 16:47 回复 未知操作系统 | 360浏览器 535720917992194
  6. 打开 uvu.cc/ir7p都是 浪美眉
    EgRlV2016-12-05 06:51 回复 未知操作系统 | 360浏览器 382294993142530