自己解决基于MQTT的android推送
2016-08-19 12:23
169 查看
突然想探索一下MQTT的推送~
都是现成的架子,搭起来很容易!
http://activemq.apache.org/activemq-5140-release.html
这里我用的系统是ubuntu14.0.4选择的是Linux版本,下载压缩包.
解压到自己的软件目录.
启动服务器
详细的内容可以看官网
http://activemq.apache.org/getting-started.html
也要读一下docs/下的内容!
这个时候,可以登录后台了,浏览器中输入
登录时,会提示账户名和密码
默认账户admin,密码admin
当然这个可以改的, 配置 conf/jetty-real.properties这个文件.
此时服务端算是配好了,对了,还要看下MQTT的监听端口,这个ActiveMQ是很强大的,它不仅仅是支持MQTT协议,还有其它协议!
打开conf/activemq.xml这个配置文件
这里可以看MQTT的监听端口是1883,如果自己的防火墙打开了,那么用速sudo ufw allow 1883,这个端口允许访问!
用命令netstat -lt可以查看所有监听的TCP协议端口!
https://github.com/tokudu/AndroidPushNotificationsDemo
这是很老的一个Eclipse项目,但是不妨碍测试,下下来,我直接把相关文件拷贝到了androidstudio下面,习惯了as
1.修改清单文件中的报错,增加访问外部sd卡的权限
2.修改推送服务PushService的
上面是我的服务端ip,和端口~
3.通知,原项目用的过时的包,有一个方法,现在没有了,那我就改成v7下的兼容api
在这里启动,停止推送,都是交给了服务,在onStart()中判断action内容~
连接tcp://10.42.0.1@1883
订阅自己的专题
可以订阅多个专题!
好啦,如果没有异常,打印了”Connection established to 10.42.0.1 on topic tokudu/yzq124 “, 就连接成功了…
好,上后台看下真正连上来没有!
最后面的 tokudu.yzq124 即是 客户端订阅的专题,那么来一发吧!
自定义自己的内容,Send!
此时,客户端会收到从broker推送过来的消息
都是现成的架子,搭起来很容易!
1.先配置服务端,测试的时候可以在本地先测试
这里选择apache的开源项目http://activemq.apache.org/activemq-5140-release.html
这里我用的系统是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
启动服务器
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
详细的内容可以看官网
http://activemq.apache.org/getting-started.html
也要读一下docs/下的内容!
这个时候,可以登录后台了,浏览器中输入
http://localhost:8161/admin
登录时,会提示账户名和密码
默认账户admin,密码admin
当然这个可以改的, 配置 conf/jetty-real.properties这个文件.
此时服务端算是配好了,对了,还要看下MQTT的监听端口,这个ActiveMQ是很强大的,它不仅仅是支持MQTT协议,还有其它协议!
打开conf/activemq.xml这个配置文件
这里可以看MQTT的监听端口是1883,如果自己的防火墙打开了,那么用速sudo ufw allow 1883,这个端口允许访问!
用命令netstat -lt可以查看所有监听的TCP协议端口!
... tcp6 0 0 [::]:1883 [::]:* LISTEN ...
2.配置客户端
先下载一个现成的demohttps://github.com/tokudu/AndroidPushNotificationsDemo
这是很老的一个Eclipse项目,但是不妨碍测试,下下来,我直接把相关文件拷贝到了androidstudio下面,习惯了as
1.修改清单文件中的报错,增加访问外部sd卡的权限
2.修改推送服务PushService的
private static final String MQTT_HOST = "10.42.0.1"; private static int MQTT_BROKER_PORT_NUM = 1883;
上面是我的服务端ip,和端口~
3.通知,原项目用的过时的包,有一个方法,现在没有了,那我就改成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); }
在这里启动,停止推送,都是交给了服务,在onStart()中判断action内容~
连接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);
订阅自己的专题
String[] topics = { topicName }; mqttClient.subscribe(topics, MQTT_QUALITIES_OF_SERVICE);
可以订阅多个专题!
好啦,如果没有异常,打印了”Connection established to 10.42.0.1 on topic tokudu/yzq124 “, 就连接成功了…
好,上后台看下真正连上来没有!
最后面的 tokudu.yzq124 即是 客户端订阅的专题,那么来一发吧!
自定义自己的内容,Send!
此时,客户端会收到从broker推送过来的消息
/* * 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); }
相关文章推荐
- 手机服务器Android消息推送(二)--基于MQTT协议实现的推送功能
- Android推送通知的实现--PHP+ANDROID做消息推送:基于IBM的MQTT协议的Tokudu项目测试
- Android MQTT推送之SSL相关问题解决
- 基于MQTT的android推送服务
- Android消息推送(二)--基于MQTT协议实现的推送功能
- 基于Mqtt协议Paho项目实现Android消息推送
- Android消息推送(二)--基于MQTT协议实现的推送功能
- 基于MQTT实现Android消息推送(Push…
- 基于MQTT协议推送Android消息
- Android消息推送(二)--基于MQTT协议实现的推送功能
- 基于MQTT协议推送Android消息
- 基于MQTT协议推送Android消息
- Android消息推送(二)--基于MQTT协议实现的推送功能
- Android基于MQTT协议实现的推送功能
- Android消息推送(二)--基于MQTT协议实现的推送功能
- 基于MQTT实现Android消息推送(Push)(2)
- 【转载】Android消息推送(二)--基于MQTT协议实现的推送功能
- Android消息推送(二)--基于MQTT协议实现的推送功能
- PHP+ANDROID做消息推送:基于IBM的MQTT协议的Tokudu项目测试
- 基于MQTT实现Android消息推送(Push)