Android实现推送PushService通知Notification


请先参考:Android推送通知指南

这里使用了IBM提供的MQTT协议实现了推送。有一个wmqtt.jar包需要导入到工程,见附件。

然后编写PushService类实现一个服务,其中有个内部类:
MQTTConnection 实现了 MqttSimpleCallback接口,重写其中的publishArrived方法,我这里是当接受到推送的数据后显示一个Notification,点击该Notification后跳转到一个Activity上。
具体看PushService类,关键的地方我都用中文字说明了:

  1. package com.ata.push;   
  2.   
  3. import org.json.JSONException;   
  4. import org.json.JSONObject;   
  5.   
  6. import android.R;   
  7. import android.app.AlarmManager;   
  8. import android.app.Notification;   
  9. import android.app.NotificationManager;   
  10. import android.app.PendingIntent;   
  11. import android.app.Service;   
  12. import android.content.BroadcastReceiver;   
  13. import android.content.Context;   
  14. import android.content.Intent;   
  15. import android.content.IntentFilter;   
  16. import android.content.SharedPreferences;   
  17. import android.net.ConnectivityManager;   
  18. import android.net.NetworkInfo;   
  19. import android.os.IBinder;   
  20. import android.util.Log;   
  21.   
  22. import com.ata.view.NewMesageInfoActivity;   
  23. import com.ibm.mqtt.IMqttClient;   
  24. import com.ibm.mqtt.MqttClient;   
  25. import com.ibm.mqtt.MqttException;   
  26. import com.ibm.mqtt.MqttPersistence;   
  27. import com.ibm.mqtt.MqttPersistenceException;   
  28. import com.ibm.mqtt.MqttSimpleCallback;   
  29.   
  30. /*   
  31.  * PushService that does all of the work.  
  32.  * Most of the logic is borrowed from KeepAliveService.  
  33.  * http://code.google.com/p/android-random/source/browse/trunk/TestKeepAlive/src/org/devtcg/demo/keepalive/KeepAliveService.java?r=219  
  34.  */  
  35. public class PushService extends Service   
  36. {   
  37.     // this is the log tag   
  38.     public static final String      TAG = "PushService";   
  39.   
  40.     // the IP address, where your MQTT broker is running.   
  41.     private static final String     MQTT_HOST = "172.16.26.41";//需要改成服务器IP   
  42.     // the port at which the broker is running.    
  43.     private static int              MQTT_BROKER_PORT_NUM      = 1883;//需要改成服务器port   
  44.     // Let's not use the MQTT persistence.   
  45.     private static MqttPersistence  MQTT_PERSISTENCE          = null;   
  46.     // We don't need to remember any state between the connections, so we use a clean start.    
  47.     private static boolean          MQTT_CLEAN_START          = true;   
  48.     // Let's set the internal keep alive for MQTT to 15 mins. I haven't tested this value much. It could probably be increased.   
  49.     private static short            MQTT_KEEP_ALIVE           = 60 * 15;   
  50.     // Set quality of services to 0 (at most once delivery), since we don't want push notifications    
  51.     // arrive more than once. However, this means that some messages might get lost (delivery is not guaranteed)   
  52.     private static int[]            MQTT_QUALITIES_OF_SERVICE = { 0 } ;   
  53.     private static int              MQTT_QUALITY_OF_SERVICE   = 0;   
  54.     // The broker should not retain any messages.   
  55.     private static boolean          MQTT_RETAINED_PUBLISH     = false;   
  56.            
  57.     // MQTT client ID, which is given the broker. In this example, I also use this for the topic header.    
  58.     // You can use this to run push notifications for multiple apps with one MQTT broker.    
  59.     public static String            MQTT_CLIENT_ID = "ata";//需要改成自己需要的名称   
  60.   
  61.     // These are the actions for the service (name are descriptive enough)   
  62.     private static final String     ACTION_START = MQTT_CLIENT_ID + ".START";   
  63.     private static final String     ACTION_STOP = MQTT_CLIENT_ID + ".STOP";   
  64.     private static final String     ACTION_KEEPALIVE = MQTT_CLIENT_ID + ".KEEP_ALIVE";   
  65.     private static final String     ACTION_RECONNECT = MQTT_CLIENT_ID + ".RECONNECT";   
  66.        
  67.     // Connection log for the push service. Good for debugging.   
  68.     //private ConnectionLog             mLog;   
  69.        
  70.     // Connectivity manager to determining, when the phone loses connection   
  71.     private ConnectivityManager     mConnMan;   
  72.     // Notification manager to displaying arrived push notifications    
  73.     private NotificationManager     mNotifMan;   
  74.   
  75.     // Whether or not the service has been started.    
  76.     private boolean                 mStarted;   
  77.   
  78.     // This the application level keep-alive interval, that is used by the AlarmManager   
  79.     // to keep the connection active, even when the device goes to sleep.   
  80.     private static final long       KEEP_ALIVE_INTERVAL = 1000 * 60 * 28;   
  81.   
  82.     // Retry intervals, when the connection is lost.   
  83.     private static final long       INITIAL_RETRY_INTERVAL = 1000 * 10;   
  84.     private static final long       MAXIMUM_RETRY_INTERVAL = 1000 * 60 * 30;   
  85.   
  86.     // Preferences instance    
  87.     private SharedPreferences       mPrefs;   
  88.     // We store in the preferences, whether or not the service has been started   
  89.     //判断Service是否已经启动,不要重复启动!   
  90.     public static final String      PREF_STARTED = "isStarted";   
  91.     // We also store the deviceID (target)   
  92.     //需要提供手机设备号,该设备号应该事先保存在SharedPreferences中   
  93.     public static final String      PREF_DEVICE_ID = "deviceID";   
  94.     // We store the last retry interval   
  95.     public static final String      PREF_RETRY = "retryInterval";   
  96.   
  97.     // Notification title   
  98.     public static String            NOTIF_TITLE = "ata";       
  99.     // Notification id   
  100.     private static final int        NOTIF_CONNECTED = 0;       
  101.            
  102.     // This is the instance of an MQTT connection.   
  103.     private MQTTConnection          mConnection;   
  104.     private long                    mStartTime;   
  105.        
  106.   
  107.     // Static method to start the service   
  108.     //在需要的地方直接调用该方法就启动监听了   
  109.     public static void actionStart(Context ctx) {   
  110.         Intent i = new Intent(ctx, PushService.class);   
  111.         i.setAction(ACTION_START);   
  112.         ctx.startService(i);   
  113.     }   
  114.   
  115.     // Static method to stop the service   
  116.     public static void actionStop(Context ctx) {   
  117.         Intent i = new Intent(ctx, PushService.class);   
  118.         i.setAction(ACTION_STOP);   
  119.         ctx.startService(i);   
  120.     }   
  121.        
  122.     // Static method to send a keep alive message   
  123.     public static void actionPing(Context ctx) {   
  124.         Intent i = new Intent(ctx, PushService.class);   
  125.         i.setAction(ACTION_KEEPALIVE);   
  126.         ctx.startService(i);   
  127.     }   
  128.   
  129.     @Override  
  130.     public void onCreate() {   
  131.         super.onCreate();   
  132.            
  133.         log("Creating service");   
  134.         mStartTime = System.currentTimeMillis();   
  135.   
  136.         /*try {  
  137.             //mLog = new ConnectionLog();  
  138.             //Log.i(TAG, "Opened log at " + mLog.getPath());  
  139.         } catch (IOException e) {  
  140.             Log.e(TAG, "Failed to open log", e);  
  141.         }*/  
  142.   
  143.         // Get instances of preferences, connectivity manager and notification manager   
  144.         mPrefs = getSharedPreferences(TAG, MODE_PRIVATE);   
  145.         mConnMan = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);   
  146.         mNotifMan = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);   
  147.        
  148.         /* If our process was reaped by the system for any reason we need  
  149.          * to restore our state with merely a call to onCreate.  We record  
  150.          * the last "started" value and restore it here if necessary. */  
  151.         handleCrashedService();   
  152.     }   
  153.        
  154.     // This method does any necessary clean-up need in case the server has been destroyed by the system   
  155.     // and then restarted   
  156.     private void handleCrashedService() {   
  157.         if (wasStarted() == true) {   
  158.             log("Handling crashed service...");   
  159.              // stop the keep alives   
  160.             stopKeepAlives();    
  161.                    
  162.             // Do a clean start   
  163.             start();   
  164.         }   
  165.     }   
  166.        
  167.     @Override  
  168.     public void onDestroy() {   
  169.         log("Service destroyed (started=" + mStarted + ")");   
  170.   
  171.         // Stop the services, if it has been started   
  172.         if (mStarted == true) {   
  173.             stop();   
  174.         }   
  175.            
  176.     /*  try {  
  177.             if (mLog != null)  
  178.                 mLog.close();  
  179.         } catch (IOException e) {}      */  
  180.     }   
  181.        
  182.     @Override  
  183.     public void onStart(Intent intent, int startId) {   
  184.         super.onStart(intent, startId);   
  185.         log("Service started with intent=" + intent);   
  186.   
  187.         // Do an appropriate action based on the intent.   
  188.         if (intent.getAction().equals(ACTION_STOP) == true) {   
  189.             stop();   
  190.             stopSelf();   
  191.         } else if (intent.getAction().equals(ACTION_START) == true) {   
  192.             start();   
  193.         } else if (intent.getAction().equals(ACTION_KEEPALIVE) == true) {   
  194.             keepAlive();   
  195.         } else if (intent.getAction().equals(ACTION_RECONNECT) == true) {   
  196.             if (isNetworkAvailable()) {   
  197.                 reconnectIfNecessary();   
  198.             }   
  199.         }   
  200.     }   
  201.        
  202.     @Override  
  203.     public IBinder onBind(Intent intent) {   
  204.         return null;   
  205.     }   
  206.   
  207.     // log helper function   
  208.     private void log(String message) {   
  209.         log(message, null);   
  210.     }   
  211.     private void log(String message, Throwable e) {   
  212.         if (e != null) {   
  213.             Log.e(TAG, message, e);   
  214.                
  215.         } else {   
  216.             Log.i(TAG, message);               
  217.         }   
  218.            
  219.         /*if (mLog != null)  
  220.         {  
  221.             try {  
  222.                 mLog.println(message);  
  223.             } catch (IOException ex) {}  
  224.         }       */  
  225.     }   
  226.        
  227.     // Reads whether or not the service has been started from the preferences   
  228.     private boolean wasStarted() {   
  229.         return mPrefs.getBoolean(PREF_STARTED, false);   
  230.     }   
  231.   
  232.     // Sets whether or not the services has been started in the preferences.   
  233.     private void setStarted(boolean started) {   
  234.         mPrefs.edit().putBoolean(PREF_STARTED, started).commit();          
  235.         mStarted = started;   
  236.     }   
  237.   
  238.     private synchronized void start() {   
  239.         log("Starting service...");   
  240.            
  241.         // Do nothing, if the service is already running.   
  242.         if (mStarted == true) {   
  243.             Log.w(TAG, "Attempt to start connection that is already active");   
  244.             return;   
  245.         }   
  246.            
  247.         // Establish an MQTT connection   
  248.         connect();   
  249.            
  250.         // Register a connectivity listener   
  251.         registerReceiver(mConnectivityChanged, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));         
  252.     }   
  253.   
  254.     private synchronized void stop() {   
  255.         // Do nothing, if the service is not running.   
  256.         if (mStarted == false) {   
  257.             Log.w(TAG, "Attempt to stop connection not active.");   
  258.             return;   
  259.         }   
  260.   
  261.         // Save stopped state in the preferences   
  262.         setStarted(false);   
  263.   
  264.         // Remove the connectivity receiver   
  265.         unregisterReceiver(mConnectivityChanged);   
  266.         // Any existing reconnect timers should be removed, since we explicitly stopping the service.   
  267.         cancelReconnect();   
  268.   
  269.         // Destroy the MQTT connection if there is one   
  270.         if (mConnection != null) {   
  271.             mConnection.disconnect();   
  272.             mConnection = null;   
  273.         }   
  274.     }   
  275.        
  276.     //    
  277.     private synchronized void connect() {          
  278.         log("Connecting...");   
  279.         // fetch the device ID from the preferences.   
  280.         String deviceID = mPrefs.getString(PREF_DEVICE_ID, null);   
  281.         // Create a new connection only if the device id is not NULL   
  282.         if (deviceID == null) {   
  283.             log("Device ID not found.");   
  284.         } else {   
  285.             try {   
  286.                 mConnection = new MQTTConnection(MQTT_HOST, deviceID);   
  287.             } catch (MqttException e) {   
  288.                 // Schedule a reconnect, if we failed to connect   
  289.                 log("MqttException: " + (e.getMessage() != null ? e.getMessage() : "NULL"));   
  290.                 if (isNetworkAvailable()) {   
  291.                     scheduleReconnect(mStartTime);   
  292.                 }   
  293.             }   
  294.             setStarted(true);   
  295.         }   
  296.     }   
  297.   
  298.     private synchronized void keepAlive() {   
  299.         try {   
  300.             // Send a keep alive, if there is a connection.   
  301.             if (mStarted == true && mConnection != null) {   
  302.                 mConnection.sendKeepAlive();   
  303.             }   
  304.         } catch (MqttException e) {   
  305.             log("MqttException: " + (e.getMessage() != null? e.getMessage(): "NULL"), e);   
  306.                
  307.             mConnection.disconnect();   
  308.             mConnection = null;   
  309.             cancelReconnect();   
  310.         }   
  311.     }   
  312.   
  313.     // Schedule application level keep-alives using the AlarmManager   
  314.     private void startKeepAlives() {   
  315.         Intent i = new Intent();   
  316.         i.setClass(this, PushService.class);   
  317.         i.setAction(ACTION_KEEPALIVE);   
  318.         PendingIntent pi = PendingIntent.getService(this0, i, 0);   
  319.         AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);   
  320.         alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,   
  321.           System.currentTimeMillis() + KEEP_ALIVE_INTERVAL,   
  322.           KEEP_ALIVE_INTERVAL, pi);   
  323.     }   
  324.   
  325.     // Remove all scheduled keep alives   
  326.     private void stopKeepAlives() {   
  327.         Intent i = new Intent();   
  328.         i.setClass(this, PushService.class);   
  329.         i.setAction(ACTION_KEEPALIVE);   
  330.         PendingIntent pi = PendingIntent.getService(this0, i, 0);   
  331.         AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);   
  332.         alarmMgr.cancel(pi);   
  333.     }   
  334.   
  335.     // We schedule a reconnect based on the starttime of the service   
  336.     public void scheduleReconnect(long startTime) {   
  337.         // the last keep-alive interval   
  338.         long interval = mPrefs.getLong(PREF_RETRY, INITIAL_RETRY_INTERVAL);   
  339.   
  340.         // Calculate the elapsed time since the start   
  341.         long now = System.currentTimeMillis();   
  342.         long elapsed = now - startTime;   
  343.   
  344.   
  345.         // Set an appropriate interval based on the elapsed time since start    
  346.         if (elapsed < interval) {   
  347.             interval = Math.min(interval * 4, MAXIMUM_RETRY_INTERVAL);   
  348.         } else {   
  349.             interval = INITIAL_RETRY_INTERVAL;   
  350.         }   
  351.            
  352.         log("Rescheduling connection in " + interval + "ms.");   
  353.   
  354.         // Save the new internval   
  355.         mPrefs.edit().putLong(PREF_RETRY, interval).commit();   
  356.   
  357.         // Schedule a reconnect using the alarm manager.   
  358.         Intent i = new Intent();   
  359.         i.setClass(this, PushService.class);   
  360.         i.setAction(ACTION_RECONNECT);   
  361.         PendingIntent pi = PendingIntent.getService(this0, i, 0);   
  362.         AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);   
  363.         alarmMgr.set(AlarmManager.RTC_WAKEUP, now + interval, pi);   
  364.     }   
  365.        
  366.     // Remove the scheduled reconnect   
  367.     public void cancelReconnect() {   
  368.         Intent i = new Intent();   
  369.         i.setClass(this, PushService.class);   
  370.         i.setAction(ACTION_RECONNECT);   
  371.         PendingIntent pi = PendingIntent.getService(this0, i, 0);   
  372.         AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);   
  373.         alarmMgr.cancel(pi);   
  374.     }   
  375.        
  376.     private synchronized void reconnectIfNecessary() {         
  377.         if (mStarted == true && mConnection == null) {   
  378.             log("Reconnecting...");   
  379.             connect();   
  380.         }   
  381.     }   
  382.   
  383.     // This receiver listeners for network changes and updates the MQTT connection   
  384.     // accordingly   
  385.     private BroadcastReceiver mConnectivityChanged = new BroadcastReceiver() {   
  386.         @Override  
  387.         public void onReceive(Context context, Intent intent) {   
  388.             // Get network info   
  389.             NetworkInfo info = (NetworkInfo)intent.getParcelableExtra (ConnectivityManager.EXTRA_NETWORK_INFO);   
  390.                
  391.             // Is there connectivity?   
  392.             boolean hasConnectivity = (info != null && info.isConnected()) ? true : false;   
  393.   
  394.             log("Connectivity changed: connected=" + hasConnectivity);   
  395.   
  396.             if (hasConnectivity) {   
  397.                 reconnectIfNecessary();   
  398.             } else if (mConnection != null) {   
  399.                 // if there no connectivity, make sure MQTT connection is destroyed   
  400.                 mConnection.disconnect();   
  401.                 cancelReconnect();   
  402.                 mConnection = null;   
  403.             }   
  404.         }   
  405.     };   
  406.        
  407.     // Display the topbar notification   
  408.     private void showNotification(String content) {   
  409.         Notification n = new Notification();   
  410.                    
  411.         n.flags |= Notification.FLAG_SHOW_LIGHTS;   
  412.         n.flags |= Notification.FLAG_AUTO_CANCEL;   
  413.   
  414.         n.defaults = Notification.DEFAULT_ALL;   
  415.            
  416.         n.icon = R.drawable.ic_dialog_info;   
  417.         n.when = System.currentTimeMillis();   
  418.   
  419.         Log.i("PushService""json==="+content);   
  420.         String alert=null;   
  421.         String id=null;   
  422.         try {   
  423.             JSONObject json = new JSONObject(content);   
  424.             alert = json.optString("alert");   
  425. //          String title = json.optString("title");   
  426. //          String message_id = json.optString("message_id");   
  427. //          String url = json.optString("url");   
  428.             id = json.optString("id");   
  429. //          Log.i("PushService", "alert==="+alert );   
  430. //          Log.i("PushService", "title==="+title );   
  431. //          Log.i("PushService", "message_id==="+message_id );   
  432. //          Log.i("PushService", "url==="+url );   
  433. //          Log.i("PushService", "id==="+id );   
  434.   
  435.         } catch (JSONException e) {   
  436.             // TODO Auto-generated catch block   
  437.             e.printStackTrace();   
  438.         }   
  439.            
  440.         Intent intent = new Intent(this,NewMesageInfoActivity.class);   
  441.         //http://testing.portal.ataudc.com/message/4fd6fbf07d9704ba51000002   
  442. //      intent.putExtra("url", "http://testing.ataudc.com/my/message/get/");   
  443.         intent.putExtra("url""http://testing.portal.ataudc.com/message/"+id);   
  444.         intent.putExtra("message_id", id);   
  445.         intent.putExtra("id", id);   
  446. //        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);     
  447.         PendingIntent pi = PendingIntent.getActivity(this0,intent, 0);   
  448.         // Change the name of the notification here   
  449.         n.setLatestEventInfo(this, NOTIF_TITLE, alert, pi);   
  450.   
  451.         mNotifMan.notify(NOTIF_CONNECTED, n);   
  452.     }   
  453.        
  454.     // Check if we are online   
  455.     private boolean isNetworkAvailable() {   
  456.         NetworkInfo info = mConnMan.getActiveNetworkInfo();   
  457.         if (info == null) {   
  458.             return false;   
  459.         }   
  460.         return info.isConnected();   
  461.     }   
  462.        
  463.     // This inner class is a wrapper on top of MQTT client.   
  464.     private class MQTTConnection implements MqttSimpleCallback {   
  465.         IMqttClient mqttClient = null;   
  466.            
  467.         // Creates a new connection given the broker address and initial topic   
  468.         public MQTTConnection(String brokerHostName, String initTopic) throws MqttException {   
  469.             // Create connection spec   
  470.             String mqttConnSpec = "tcp://" + brokerHostName + "@" + MQTT_BROKER_PORT_NUM;   
  471.                 // Create the client and connect   
  472.                 mqttClient = MqttClient.createMqttClient(mqttConnSpec, MQTT_PERSISTENCE);   
  473.                 String clientID = MQTT_CLIENT_ID + "/" + mPrefs.getString(PREF_DEVICE_ID, "");   
  474.                 mqttClient.connect(clientID, MQTT_CLEAN_START, MQTT_KEEP_ALIVE);   
  475.   
  476.                 // register this client app has being able to receive messages   
  477.                 mqttClient.registerSimpleHandler(this);   
  478.                    
  479.                 // Subscribe to an initial topic, which is combination of client ID and device ID.   
  480.                 initTopic = MQTT_CLIENT_ID + "/" + initTopic;   
  481.                 subscribeToTopic(initTopic);   
  482.            
  483.                 log("Connection established to " + brokerHostName + " on topic " + initTopic);   
  484.            
  485.                 // Save start time   
  486.                 mStartTime = System.currentTimeMillis();   
  487.                 // Star the keep-alives   
  488.                 startKeepAlives();                         
  489.         }   
  490.            
  491.         // Disconnect   
  492.         public void disconnect() {   
  493.             try {              
  494.                 stopKeepAlives();   
  495.                 mqttClient.disconnect();   
  496.             } catch (MqttPersistenceException e) {   
  497.                 log("MqttException" + (e.getMessage() != null? e.getMessage():" NULL"), e);   
  498.             }   
  499.         }   
  500.         /*  
  501.          * Send a request to the message broker to be sent messages published with   
  502.          *  the specified topic name. Wildcards are allowed.      
  503.          */  
  504.         private void subscribeToTopic(String topicName) throws MqttException {   
  505.                
  506.             if ((mqttClient == null) || (mqttClient.isConnected() == false)) {   
  507.                 // quick sanity check - don't try and subscribe if we don't have   
  508.                 //  a connection   
  509.                 log("Connection error" + "No connection");     
  510.             } else {                                       
  511.                 String[] topics = { topicName };   
  512.                 mqttClient.subscribe(topics, MQTT_QUALITIES_OF_SERVICE);   
  513.             }   
  514.         }      
  515.         /*  
  516.          * Sends a message to the message broker, requesting that it be published  
  517.          *  to the specified topic.  
  518.          */  
  519.         private void publishToTopic(String topicName, String message) throws MqttException {           
  520.             if ((mqttClient == null) || (mqttClient.isConnected() == false)) {   
  521.                 // quick sanity check - don't try and publish if we don't have   
  522.                 //  a connection                   
  523.                 log("No connection to public to");         
  524.             } else {   
  525.                 mqttClient.publish(topicName,    
  526.                                    message.getBytes(),   
  527.                                    MQTT_QUALITY_OF_SERVICE,    
  528.                                    MQTT_RETAINED_PUBLISH);   
  529.             }   
  530.         }          
  531.            
  532.         /*  
  533.          * Called if the application loses it's connection to the message broker.  
  534.          */  
  535.         public void connectionLost() throws Exception {   
  536.             log("Loss of connection" + "connection downed");   
  537.             stopKeepAlives();   
  538.             // null itself   
  539.             mConnection = null;   
  540.             if (isNetworkAvailable() == true) {   
  541.                 reconnectIfNecessary();    
  542.             }   
  543.         }          
  544.            
  545.         /*  
  546.          * Called when we receive a message from the message broker.   
  547.          * 在这里处理服务器推送过来的数据  
  548.          */  
  549.         public void publishArrived(String topicName, byte[] payload, int qos, boolean retained) {   
  550.             // Show a notification   
  551.             String s = new String(payload);   
  552.             showNotification(s);       
  553.         }      
  554.            
  555.         public void sendKeepAlive() throws MqttException {   
  556.             log("Sending keep alive");   
  557.             // publish to a keep-alive topic   
  558.             publishToTopic(MQTT_CLIENT_ID + "/keepalive", mPrefs.getString(PREF_DEVICE_ID, ""));   
  559.         }          
  560.     }   
  561. }  

PushService是根据你的设备号来准确定位你的手机的,它是从SharedPreferences取得该设备号的,所以你得事先将你的设备号保存在SharedPreferences中,如下:

  1. //启动PUSH服务   
  2.         mDeviceID = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);    
  3.         Editor editor = getSharedPreferences(PushService.TAG, MODE_PRIVATE).edit();   
  4.         editor.putString(PushService.PREF_DEVICE_ID, mDeviceID);   
  5.         editor.commit();   
  6.         Log.i("tag""mDeviceID====="+mDeviceID);   
  7.         PushService.actionStart(getApplicationContext());  

将上面的代码写在需要的Activity中即可。至于服务器端代码,我们使用php写的,这里请参考Android推送通知指南

代码下载

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/6月/13日/Android实现推送PushService通知Notification/

相关内容