简体   繁体   中英

Android XMPP Server in Java From Google Docs

I am trying to create a server to handle XMPP for android GCM by following the Google Doc .

public static void main(String[] args) throws Exception {
    final long senderId = PROJECT_ID;
    final String password = "API_KEY";

    XMPPServer00 ccsClient = new XMPPServer00();

    ccsClient.connect(senderId, password);

    // Send a sample hello downstream message to a device.
    String toRegId = "REGISTRATION_ID";
    String messageId = ccsClient.nextMessageId();
    Map<String, String> payload = new HashMap<String, String>();
    payload.put("Hello", "World");
    payload.put("CCS", "Dummy Message");
    payload.put("EmbeddedMessageId", messageId);
    String collapseKey = "sample";
    Long timeToLive = 10000L;
    String message = createJsonMessage(toRegId, messageId, payload,
            collapseKey, timeToLive, true);

    ccsClient.sendDownstreamMessage(message);
}

So far the server is able to send downstream message to the client, but the client is not able to send upstream messages to the server.

Nov 26, 2014 6:07:43 PM XMPPServer00$LoggingConnectionListener authenticated
INFO: Authenticated.
Nov 26, 2014 6:07:43 PM XMPPServer00$3 interceptPacket
INFO: Sent: <message id='4259t-2'><gcm xmlns="google:mobile:data">   {&quot;to&quot;:&quot;API_KEY&quot;,&quot;delay_while_idle&quot;:true,&quot;collapse_key&quot;:&quot;sample&quot;,&quot;data&quot;:{&quot;EmbeddedMessageId&quot;:&quot;m-b9731a48-7730-40d6-a52e-2ea9253c85ca&quot;,&quot;title&quot;:&quot;World&quot;,&quot;CCS&quot;:&quot;Dummy Message&quot;},&quot;message_id&quot;:&quot;m-b9731a48-7730-40d6-a52e-2ea9253c85ca&quot;,&quot;time_to_live&quot;:10000}</gcm></message>`

Looking at the code I've copied from google, its creating the connection then sending a downstream message to the device then it finishes. And if I'm not wrong Google Docs said that there needs to be a persistent connection between third-party server and google server.

connection.addPacketListener(new PacketListener() {

        @Override
        public void processPacket(Packet packet) {
        ...

Inside the connect() it creates a listener to handle upstream messages but since the program ends after sending the downstream message, this listener also ends right?

Another question, instead of using a server application running, is it better to create a servlet/webapp to handle xmpp?

I was right. The main function is ending hence the whole program ends. The way I solved it was to use a new thread to run it and kept it running and everything now works ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM