I want to develop an instant messaging application. GCM, for pushing data is a popular(and efficient) way to go , if you are on android, but I am not using it due to the following reasons:
- It deletes more than 100 undelivered messages.
- It will not work on devices that do not have google apps.
Instead, I decided on setting up a traditional XMPP server (openFire), and I am using the Smack api(TCP connection) to connect. So far, it is going well , but I have a few concerns.
This is a small test code I wrote(it runs in a service):
Log.d("TAG","service has started");
SmackConfiguration.setDefaultPacketReplyTimeout(10000);
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("admin", "football100")
.setServiceName("harsh-pc")
.setHost("192.168.0.200")
.setPort(5222).setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
final AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect();
conn2.login();
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("online");
// Send the packet (assume we have an XMPPConnection instance called "con").
conn2.sendStanza(presence);
} catch (SmackException | IOException | XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG", e.toString());
}
StanzaFilter filter=new StanzaFilter() {
@Override
public boolean accept(Stanza stanza) {
return true;
}
};
StanzaListener listener= new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Log.d("TAG","recevied stuff");
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
Chat newChat = chatmanager.createChat("harsh@harsh-pc");
newChat.sendMessage("Reply :) ");
}
};
conn2.addAsyncStanzaListener(listener,filter);
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
Chat newChat = chatmanager.createChat("harsh@harsh-pc");
try {
Random r=new Random();
// newChat.sendMessage(Integer.toString(r.nextInt()));
Thread.sleep(1500);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG",e.toString());
}
}
}).start();
final Thread sleeper=new Thread(new Runnable() {
@Override
public void run() {
for(;;){
try {
Thread.sleep(100000);
Log.d("TAG","SLEEPI");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});sleeper.start();
Notice the last part of this snippet. I have to run an infinite loop so that I can keep listening for incoming packets (if I dont include this code, I am not able to intercept any incoming packets).
My questions are:
- Is this method going to take tremendous amounts of battery?
- Will this method prevent the device from sleeping?
- Is there a better way to get things done (without using GCM) ?
- Is there a way to integrate GCM with OPENFIRE ?