如何在不使用谷歌云消息传递的情况下为Android开发推送通知?

New*_*bie 6 android publish-subscribe push-notification

我需要为Android应用程序开发推送通知系统,而不是使用谷歌云消息传递(安全原因).这里服务器将通知所有当前登录到特定Android应用程序的设备.

我知道问题非常广泛,但任何人都可以指出可能的解决方案.

提前致谢.

abh*_*nav 3

您可以使用 MQTT 构建推送通知服务。(注意:Facebook 等应用程序使用 MQTT 进行推送通知)。

因此,要构建推送通知服务,您需要在服务器上运行 MQTT 代理(我推荐MQTT Mosquitto和在 Android 设备上运行的 bachground 服务。

MQTT服务代码(可以在服务器端和客户端上使用):

/**
* 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
    }
}
}
Run Code Online (Sandbox Code Playgroud)

要了解更多信息,您可以查看我实现的本项目中实现的MQTT推送通知服务:SenSocial

如果您的服务器上没有运行代理,那么您可以尝试公开可用的基于 MQTT Mosquitto 的代理:MQTT BROKER