简体   繁体   English

使用Smack无法通过CCS从Google Cloud Messaging接收所有消息,仅每秒一次

[英]Not able receiving all messages, only every second, from Google Cloud Messaging with CCS using Smack

I'm trying to set up a connection to Google Cloud Messaging via Cloud Connection Server (XMPP-Connection) using Smack 4.1.2. 我正在尝试使用Smack 4.1.2通过Cloud Connection Server(XMPP-Connection)建立与Google Cloud Messaging的连接。 I was already able to establish connection and receive incoming messages. 我已经能够建立连接并接收传入的消息。 But my problem is the StanzaListener isn't triggered by each message (but only every second one). 但是我的问题是StanzaListener不是由每条消息触发的(而是每秒钟才触发的)。 The Smack Debugging Console shows all "Raw Received Packets", so the sending from Device-to-Cloud works for each message. Smack调试控制台显示所有“原始接收的数据包”,因此从设备到云的发送适用于每条消息。

Thank you for your help! 谢谢您的帮助!

Here's my code from the Server App: 这是服务器应用程序中的代码:

My Class GCMServer with Main: 我的带有Main的GCMServer类:

public class GCMServer {

public static final Logger logger = Logger.getLogger(GCMServer.class.getName());
public static SSLContext sslCtx;
public static XMPPTCPConnection connection;

private static final String GCM_SERVER = "gcm.googleapis.com";
private static final int GCM_PORT = 5235;

private static final String GCM_ELEMENT_NAME = "gcm";
private static final String GCM_NAMESPACE = "google:mobile:data";

private static final String YOUR_PROJECT_ID = "xxxxxxxxxxxx";
private static final String YOUR_API_KEY = "xxxx";



public static void main(String[] args) {

    ConnectionListener cl;

    try {        

        KeyStore windowsRootTruststore = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
        windowsRootTruststore.load(null, null);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(windowsRootTruststore);
        sslCtx = SSLContext.getInstance("TLS");
        sslCtx.init(null, tmf.getTrustManagers(), null);

       } catch (KeyStoreException | NoSuchProviderException | IOException | NoSuchAlgorithmException | CertificateException | KeyManagementException ex) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
        } 



        XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
                .setSecurityMode(SecurityMode.ifpossible)
                .setUsernameAndPassword(YOUR_PROJECT_ID, YOUR_API_KEY)
                .setHost(GCM_SERVER)
                .setServiceName(GCM_SERVER)
                .setPort(5235)
                .setDebuggerEnabled(true)
                .setCompressionEnabled(false)
                .setSocketFactory(sslCtx.getSocketFactory())
                .build();


    cl = new ConnectionListener() {

        @Override
        public void connected(XMPPConnection xmppc) {
             Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "connected");
             System.out.println("Conncetion is secure: "+connection.isSecureConnection());
        }

        @Override
        public void authenticated(XMPPConnection xmppc, boolean bln) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "authenticated");
        }

        @Override
        public void connectionClosed() {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "connection closed");
        }

        @Override
        public void connectionClosedOnError(Exception excptn) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "conncetion closed on error");
        }

        @Override
        public void reconnectionSuccessful() {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnection successful");
        }

        @Override
        public void reconnectingIn(int i) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnecting..");
        }

        @Override
        public void reconnectionFailed(Exception excptn) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnection failed");
        }
    };

        connection = new XMPPTCPConnection(conf);

        //disable Roster; it seems it's not supported by GCM
        Roster roster = Roster.getInstanceFor(connection);  
        roster.setRosterLoadedAtLogin(false);  


        try {
            connection.connect();
            connection.addAsyncStanzaListener(new MyStanzaListener(),new StanzaTypeFilter(Message.class));
            connection.addConnectionListener(cl);
            connection.login(YOUR_PROJECT_ID + "@gcm.googleapis.com", YOUR_API_KEY);
        } catch (SmackException ex) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XMPPException ex) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
        }

}

Class MyStanzaListener: 类MyStanzaListener:

private static class MyStanzaListener implements StanzaListener{

    @Override
    public void processPacket(Stanza stanza) {
                System.out.println("hei ho, new message: " + stanza.toXML());

                Message incomingMessage = (Message) stanza;
                GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GCM_NAMESPACE);
                String json = gcmPacket.getJson();

                try {
                    Map<String, Object> jsonObject = (Map<String, Object>) JSONValue.parseWithException(json);

                    Object messageType = jsonObject.get("message_type");
                    String from = (String)jsonObject.get("from");
                    String messageId = (String)jsonObject.get("message_id");
                    String category = (String)jsonObject.get("category");


                    if(messageType == null) {
                        String ack = createJsonAck(from, messageId);
                        System.out.println(ack);
                        send(ack);

                        handleMessage(jsonObject);
                    }
                    else{
                        Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, "ERROR IN HANDLING MESSAGE");
                    } 
                } catch (ParseException ex) {
                    Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
                }
    }

}

Methods HandleMessage, CreateJSONAck, Send: 方法HandleMessage,CreateJSONAck,发送:

public static void handleMessage(Map<String, Object> jsonObject) {
      DBConnect db = new DBConnect();

      Map<String, Object> messageData = (Map<String, Object>) jsonObject.get("data");
      String phoneNumber = (String)messageData.get("phoneNumber");
      String text = (String)messageData.get("message");


      db.inBoundMessage(phoneNumber, text);

  }

   public static String createJsonAck(String to, String messageId) {
        Map<String, Object> message = new LinkedHashMap<String, Object>();
        message.put("to", to);
        message.put("message_id", messageId);
        message.put("message_type", "ack");

        return JSONValue.toJSONString(message);
    }

   /**
     * Sends a downstream GCM message.
     */
    public static void send(String jsonRequest) {
        Stanza request = (Stanza)new GcmPacketExtension(jsonRequest).toPacket();
        try {
            connection.sendStanza(request);
        } catch (NotConnectedException ex) {
            Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, "ERROR WHILE SENDING: ", ex);
        }
    }

This is a bug ( SMACK-695 ) in Smack 4.1.3, which is fixed/will be fixed in 4.1.4. 这是Smack 4.1.3中的错误( SMACK-695 ),此错误已修复/将在4.1.4中修复。 For more information see https://community.igniterealtime.org/thread/56610 . 有关更多信息,请参见https://community.igniterealtime.org/thread/56610

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从服务器发布消息:Google Cloud Messaging - Not able to post messages from server : Google Cloud Messaging 谷歌云消息传递-使用smack的xmpp服务器端不起作用 - google cloud messaging - xmpp server side using smack not working Firebase云消息传递-未收到任何消息(Android) - Firebase Cloud Messaging - Not receiving any messages (Android) 使用Smack 4.1.0 API作为Google GCM CCS的XMPP客户端时,SecurityMode.required无效 - When using Smack 4.1.0 API as a XMPP Client for Google’s GCM CCS, SecurityMode.required is not working GCM CCS服务器实现未接收上游消息 - GCM CCS server implementation not receiving upstream messages Google Cloud Messaging:向“所有”用户发送消息 - Google Cloud Messaging: Send message to “all” users 从Google Cloud Messaging中获得未经授权的401错误,我正在使用服务器API密钥并允许所有IP - Getting Unauthorized 401 error from Google Cloud Messaging and I am using Server API key with all IPs allowed Google Cloud Messaging - 即时收到或延迟收到的邮件 - Google Cloud Messaging - messages either received instantly or with long delay 谷歌云连接服务器和smack - Google Cloud Connection Server and smack 如何使用Android代码中的GCM CCS进行上游消息传递服务? - How to do upstream messaging service using GCM CCS from Android code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM