6

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

我知道这个问题非常广泛,但谁能指出可能的解决方案。

提前致谢。

4

2 回答 2

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
    }
}
}

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

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

最后,要考虑的替代方案是付费的 MQTT 推送通知服务,例如Pushy,它会为您解决这个问题。

于 2014-12-23T11:10:41.510 回答
0

我建议不要使用 GCM。保持从手机到推送服务器的持久连接是昂贵的(就电池使用而言,但也包括实际资金),而 GCM 可以免费为您做到这一点。也不清楚您打算如何在后台拥有一个持久的套接字侦听器。由于内存压力,它很可能会在某个时候被框架杀死。GCM 在一个特权进程中运行,这很难被杀死。

安全方面,我建议在将任何有效负载发送到设备之前对其进行加密,然后在手机本身中解密。就开发成本和电池使用而言,这将便宜得多。

于 2016-01-07T11:38:49.647 回答