【问题标题】:How to develop push notifications for android without using google cloud messaging? [closed]如何在不使用谷歌云消息的情况下为 android 开发推送通知? [关闭]
【发布时间】:2013-07-01 13:20:52
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 如果我问,你有什么样的安全原因让你三思而后行
  • 这是银行应用程序的一部分。出于同样的原因,我被告知我不能使用 GCM。
  • 如果我需要为推送通知编写自己的服务器(并且不使用任何云服务),我应该如何进行。任何人都可以指导我并简要概述使用哪些框架来开始。
  • 你有解决办法吗?

标签: android push-notification publish-subscribe


【解决方案1】:

您可以使用 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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 您如何在专用网络上实现推送通知(例如在没有外部互联网连接的发电厂中),同时保持 GCM 的电力使用优势?
    猜你喜欢
    • 2014-04-05
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多