突然想探索一下MQTT的推送~
都是現(xiàn)成的架子,搭起來(lái)很容易!
1.先配置服務(wù)端,測(cè)試的時(shí)候可以在本地先測(cè)試
這里選擇apache的開(kāi)源項(xiàng)目
http://activemq.apache.org/activemq-5140-release.html
這里我用的系統(tǒng)是ubuntu14.0.4選擇的是Linux版本,下載壓縮包.
解壓到自己的軟件目錄.
ss@Dell:~/Soft/apache-activemq-5.14.0$ ls
activemq-all-5.14.0.jar bin conf data docs examples lib LICENSE NOTICE README.txt tmp webapps webapps-demo
啟動(dòng)服務(wù)器
ss@Dell:~/Soft/apache-activemq-5.14.0/bin$ ls
activemq activemq-diag activemq.jar env linux-x86-32 linux-x86-64 macosx wrapper.jar
ss@Dell:~/Soft/apache-activemq-5.14.0/bin$ ./activemq start
詳細(xì)的內(nèi)容可以看官網(wǎng)
http://activemq.apache.org/getting-started.html
也要讀一下docs/下的內(nèi)容!
這個(gè)時(shí)候,可以登錄后臺(tái)了,瀏覽器中輸入
http://localhost:8161/admin
登錄時(shí),會(huì)提示賬戶(hù)名和密碼
默認(rèn)賬戶(hù)admin,密碼admin
當(dāng)然這個(gè)可以改的, 配置 conf/jetty-real.properties這個(gè)文件.
此時(shí)服務(wù)端算是配好了,對(duì)了,還要看下MQTT的監(jiān)聽(tīng)端口,這個(gè)ActiveMQ是很強(qiáng)大的,它不僅僅是支持MQTT協(xié)議,還有其它協(xié)議!
打開(kāi)conf/activemq.xml這個(gè)配置文件
這里可以看MQTT的監(jiān)聽(tīng)端口是1883,如果自己的防火墻打開(kāi)了,那么用速sudo ufw allow 1883,這個(gè)端口允許訪問(wèn)!
用命令netstat -lt可以查看所有監(jiān)聽(tīng)的TCP協(xié)議端口!
...
tcp6 0 0 [::]:1883 [::]:* LISTEN
...
2.配置客戶(hù)端
先下載一個(gè)現(xiàn)成的demo
https://github.com/tokudu/AndroidPushNotificationsDemo
這是很老的一個(gè)Eclipse項(xiàng)目,但是不妨礙測(cè)試,下下來(lái),我直接把相關(guān)文件拷貝到了androidstudio下面,習(xí)慣了as
1.修改清單文件中的報(bào)錯(cuò),增加訪問(wèn)外部sd卡的權(quán)限
2.修改推送服務(wù)PushService的
private static final String MQTT_HOST = "10.42.0.1";
private static int MQTT_BROKER_PORT_NUM = 1883;
上面是我的服務(wù)端ip,和端口~
3.通知,原項(xiàng)目用的過(guò)時(shí)的包,有一個(gè)方法,現(xiàn)在沒(méi)有了,那我就改成v7下的兼容api
private void showNotification(String text) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(NOTIF_TITLE);
builder.setContentText(text);
builder.setAutoCancel(true);
builder.setSmallIcon(com.tokudu.demo.R.drawable.icon);
builder.setWhen(System.currentTimeMillis());
// Notification n = new Notification();
// n.flags |= Notification.FLAG_SHOW_LIGHTS;
// n.flags |= Notification.FLAG_AUTO_CANCEL;
// n.defaults = Notification.DEFAULT_ALL;
// n.icon = com.tokudu.demo.R.drawable.icon;
// n.when = System.currentTimeMillis();
// // Simply open the parent activity
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
PushActivity.class), 0);
// // Change the name of the notification here
// n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
builder.setContentIntent(pi);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification n = builder.build();
mNotifMan.notify(NOTIF_CONNECTED++, n);
}
在這里啟動(dòng),停止推送,都是交給了服務(wù),在onStart()中判斷action內(nèi)容~
連接tcp://10.42.0.1@1883
// Create connection spec
String mqttConnSpec = "tcp://" + brokerHostName + "@" + MQTT_BROKER_PORT_NUM;
// Create the client and connect
mqttClient = MqttClient.createMqttClient(mqttConnSpec, MQTT_PERSISTENCE);
String clientID = MQTT_CLIENT_ID + "/"+ mPrefs.getString(PREF_DEVICE_ID, "");
mqttClient.connect(clientID, MQTT_CLEAN_START, MQTT_KEEP_ALIVE);
訂閱自己的專(zhuān)題
String[] topics = { topicName };
mqttClient.subscribe(topics, MQTT_QUALITIES_OF_SERVICE);
可以訂閱多個(gè)專(zhuān)題!
好啦,如果沒(méi)有異常,打印了"Connection established to 10.42.0.1 on topic tokudu/yzq124 ", 就連接成功了...
好,上后臺(tái)看下真正連上來(lái)沒(méi)有!
最后面的 tokudu.yzq124 即是 客戶(hù)端訂閱的專(zhuān)題,那么來(lái)一發(fā)吧!
自定義自己的內(nèi)容,Send!
此時(shí),客戶(hù)端會(huì)收到從broker推送過(guò)來(lái)的消息
/*
* Called when we receive a message from the message broker.
*/
public void publishArrived(String topicName, byte[] payload, int qos,
boolean retained) {
// Show a notification
String s = new String(payload);
showNotification(s);
log("Got message: " + s);
}
MQTT強(qiáng)大的是群推模式
客戶(hù)端可以注冊(cè)多個(gè)token(客戶(hù)端的唯一標(biāo)識(shí))可以讓所有客戶(hù)端都注冊(cè)該token即可啡彬,然后發(fā)送的時(shí)候糟港,只需要針對(duì)一個(gè)token發(fā)消息,那么所有的手機(jī)都收到了脚祟。不用像其他的推送一樣饲鄙,對(duì)每個(gè)token都去發(fā)一遍凄诞。所以MQTT群發(fā)的效率的極高的。這樣的話對(duì)手機(jī)端來(lái)說(shuō)就很好了忍级,也許我們需要對(duì)某個(gè)程序的所有手機(jī)發(fā)帆谍、向某個(gè)程序某個(gè)版本手機(jī)發(fā),向某臺(tái)手機(jī)發(fā)推送 等等用MQTT都可以很輕松的實(shí)現(xiàn)轴咱。