搭建Android MQTT推送平臺

最近研究了一下Android推送,在網上搜到了很多種方式,覺得IBM的MQTT協議這種方式還是挺不錯的。

下面介紹一下搭建過程:

需要準備的工具如下:

(1)、下載Mosquitto(MQTT的代理,也可以用IBM的ISMB)
http://mosquitto.org/download/

我下載的是mosquitto-1.1.2-install-win32.exe,根據自己的需要下載,不安裝這個推送不了消息。
(2)、下載AndroidPushNotificationsDemo
https://github.com/tokudu/Androi ... /archive/master.zip

(3)、下載PhpMQTTClient
https://github.com/tokudu/PhpMQTTClient/archive/master.zip

將PHPMQTTClient部署到PHP服務器上,運行,打開mosquitto安裝目錄的mosquitto.exe

 

將以下AndroidPushNotificationsDemo的代碼根據自己的情況修改。

public class PushService extends Service
{
	// this is the log tag
	public static final String		TAG = "DemoPushService";

	// the IP address, where your MQTT broker is running.
//修改成自己的服務器地址
	private static final String		MQTT_HOST = 
//			"209.124.50.174"
//			"192.168.12.19" 
			"10.0.2.2" 
			;
	// the port at which the broker is running. 
	private static int				MQTT_BROKER_PORT_NUM      = 1883;
	// Let's not use the MQTT persistence.
	private static MqttPersistence	MQTT_PERSISTENCE          = null;

 

輸入Device target ID ,內容,點擊按鈕,OK!



PS:

原來其實是不需要那個PHP的,直接用mosquitto_pub.exe就可以測試了,運行客戶端程序,在命令窗口中使用Mosquitto_pub.exe -q [Qos級別] -t [主題] -m [發佈的內容] 進行測試。這個android客戶端例子中訂閱的topic是“tokudu/你的deviceID”。


一些別的資料:

http://www.cnblogs.com/yjl49/archive/2012/05/17/2506819.html



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章