mqtt协议实现安卓的推送客户端实现

本文介绍如何使用 MQTT 协议在 Android 上实现推送客户端。通过连接 MQTT 消息代理服务器,客户端可以接收和发布消息,实现低带宽、低耗电的通讯。文章提供了一个简单的 Android 服务类 `PushService` 的代码示例,展示了 MQTT 连接、心跳保持、断线重连等关键功能。

   这一周,姐姐亲自实践了一下,感觉还不错分享给大家

   MQTT是一项消息传递技术,由IBM再2001年发布。

总结一下,机制就是使用一个代理服务器message broker,
客户端client连接上这个服务器,然后告诉服务器说,我可以接收哪些类型的消息,
同时,client也可以发布自己的消息,这些消息根据协议的内容,可以被其他client获取。

只要手机客户端,连上服务器,然后就可以接收和发布消息了,不用自己写socket什么了,

低带宽,低耗电量,代码量也少,很简单吧。

先写客户端吧,啥也不说了直接上代码

package com.tokudu.demo;


import java.io.IOException;


import com.ibm.mqtt.IMqttClient;
import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttPersistence;
import com.ibm.mqtt.MqttPersistenceException;
import com.ibm.mqtt.MqttSimpleCallback;


import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;


/* 
 * PushService that does all of the work.
 * Most of the logic is borrowed from KeepAliveService.
 * http://code.google.com/p/android-random/source/browse/trunk/TestKeepAlive/src/org/devtcg/demo/keepalive/KeepAliveService.java?r=219
 */
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";
private static final String MQTT_HOST="192.168.1.110";
// mqtt协议的端口. 
private static int MQTT_BROKER_PORT_NUM      = 1883;
// Let's not use the MQTT persistence. 我们不要用MQTT持久性。
private static MqttPersistence MQTT_PERSISTENCE          = null;
// We don't need to remember any state between the connections, so we use a clean start. 
private static boolean MQTT_CLEAN_START          = true;
// Let's set the internal keep alive for MQTT to 15 mins. I haven't tested this value much. It could probably be increased.
private static short MQTT_KEEP_ALIVE           = 60 * 15;
// Set quality of services to 0 (at most once delivery), since we don't want push notifications 
// arrive more than once. However, this means that some messages might get lost (delivery is not guaranteed)
private static int[] MQTT_QUALITIES_OF_SERVICE = { 0 } ;
private static int MQTT_QUALITY_OF_SERVICE   = 0;
// The broker should not retain any messages.
private static boolean MQTT_RETAINED_PUBLISH     = false;

// MQTT client ID, which is given the broker. In this example, I also use this for the topic header. 
// You can use this to run push notifications for multiple apps with one MQTT broker. 
public static String MQTT_CLIENT_ID = "tokudu";


// These are the actions for the service (name are descriptive enough)
private static final String ACTION_START = MQTT_CLIENT_ID + ".START";
private static final String ACTION_STOP = MQTT_CLIENT_ID + ".STOP";
private static final String ACTION_KEEPALIVE = MQTT_CLIENT_ID + ".KEEP_ALIVE";
private static final String ACTION_RECONNECT = MQTT_CLIENT_ID + ".RECONNECT";

// Connection log for the push service. Good for debugging.
private ConnectionLog mLog;

// Connectivity manager to determining, when the phone loses connection
private ConnectivityManager mConnMan;
// Notification manager to displaying arrived push notifications 
private NotificationManager mNotifMan;


// Whether or not the service has been started.
private boolean mStarted;


// This the application level keep-alive interval, that is used by the AlarmManager
// to keep the connection active, even when the device goes to sleep.
private static final long KEEP_ALIVE_INTERVAL = 1000 * 60 * 28;


// Retry intervals, when the connection is lost.
private static final long INITIAL_RETRY_INTERVAL = 1000 * 10;
private static final long MAXIMUM_RETRY_INTERVAL = 1000 * 60 * 30;


// Preferences instance 
private SharedPreferences mPrefs;
// We store in the preferences, whether or not the service has been started
public static final String PREF_STARTED = "isStarted";
// We also store the deviceID (target)
public static final String PREF_DEVICE_ID = "deviceID";
// We store the last retry interval
public static final String PREF_RETRY = "retryInterval";


// Notification title
public static String NOTIF_TITLE = "Tokudu";
// Notif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值