Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop push notifications for android without using google cloud messaging? [closed]

I need to develop push notification system for android applications, not using google cloud messaging(security reasons). Here the server will notify all those devices that are currently logged on to a particular android application.

I know the question is pretty broad, but could anyone point me the possible solutions.

Thanks in advance.

like image 868
Newbie Avatar asked Jul 01 '13 13:07

Newbie


1 Answers

You can use MQTT for building a push notification service. (Note: applications like Facebook uses MQTT for push notifications).

So, to build a push notification service you need a MQTT broker running on the server (I recommend MQTT Mosquitto and a bachground service running on Android device.

Code for MQTT service (can be used both on the server and on the client sode):

/**
* MQTTManager class provide methods to connect, subscribe, publish, and listen to MQTT broker
*/
public class MQTTManager {

private final String TAG = "MQTT";
private final String BROKER_URL = "http://mqtt-dashboard.com/info/broker:1883"; //change it to your broker URL
private MqttClient mqttClient;
private String CLIENT_ID = "123"
private String topic = "ABC"
private int keepAliveInterval=60*5;
private MqttConnectOptions opt;

/**
 * Constructor
 * @throws MqttException
 */
protected MQTTManager() throws MqttException {
    opt=new MqttConnectOptions();
    opt.setKeepAliveInterval(keepAliveInterval);
    opt.setConnectionTimeout(10);
    mqttClient = new MqttClient(BROKER_URL, CLIENT_ID, new MemoryPersistence());
    mqttClient.setCallback(new MQTTCallback(BROKER_URL, CLIENT_ID, topic));
}

/**
 * Connects to the MQTT broker service on server side.
 */
public void connect(){
    try {
        mqttClient.connect(opt);
    } catch (MqttException e) {
        Log.e(TAG, "Error while connecting to mqtt broker: "+e.toString());
    }
}

/**
 * Subscribes the device to the topic provided via constructor
 */
public void subscribeDevice(){
    try {
        mqttClient.subscribe(this.topic);
    } catch (MqttException e) {
        Log.e(TAG, "Error while subscribing to mqtt broker: "+e.toString());
    }
}

/**
 * Publishes the message to the MQTT broker service.
 * @param String Message that needs to be published 
 */
public void publishToDevice(String message){
    try {
        MqttTopic mtopic=mqttClient.getTopic(this.topic);
        MqttMessage msg= new MqttMessage(message.getBytes());
        mtopic.publish(msg);
    } catch (MqttException e) {
        Log.e(TAG, "Error while publishing to mqtt broker: "+e.toString());
    }
}


/**
 * Inner class for mqtt callback
 */
public class MQTTCallback implements MqttCallback{

    final private String TAG = "MQTT";
    private String BROKER_URL;
    private String CLIENT_ID;                  
    private String TOPIC;
    private MqttClient mqttClient;

    public MQTTCallback(String BROKER_URL, String CLIENT_ID, String TOPIC) {
        this.BROKER_URL= BROKER_URL;
        this.CLIENT_ID = CLIENT_ID;
        this.TOPIC=TOPIC;
    }
    
    public void connectionLost(Throwable arg0) {
        connect();          
    }

    public void deliveryComplete(MqttDeliveryToken arg0) {
        if(arg0==null)
            System.out.print("Message delivered");          
    }

    public void messageArrived(MqttTopic arg0, MqttMessage arg1)
            throws Exception {
        // MESSAGE ARRIVES HERE!! argo-> device id & arg1 --> message
    }
}
}

To learn more, you can check the MQTT push notification service implemented in this project: SenSocial that I implemented.

If you don't have a broker running on your server, then you can try a publically available MQTT Mosquitto-based broker: MQTT BROKER

like image 172
abhi Avatar answered Oct 10 '22 11:10

abhi