EJABBERD USING AMAZON EC2 (AWS) AND XML RPC FOR ANDROID XMPP CHAT PART -3

ext {
smackVersion = '4.1.5'
}
dependencies {
compile project(':memorizingtrustmanager')
compile "org.igniterealtime.smack:smack-android-extensions:$smackVersion"
compile "org.igniterealtime.smack:smack-experimental:$smackVersion"
compile "org.igniterealtime.smack:smack-tcp:$smackVersion"
compile 'de.hdodenhof:circleimageview:2.0.0'
}
import org.jivesoftware.smack.ConnectionConfiguration;import org.jivesoftware.smack.ConnectionListener;import org.jivesoftware.smack.SmackException;import org.jivesoftware.smack.StanzaListener;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smack.filter.StanzaFilter;import org.jivesoftware.smack.packet.IQ;import org.jivesoftware.smack.packet.Message;import org.jivesoftware.smack.packet.Presence;import org.jivesoftware.smack.packet.Stanza;import org.jivesoftware.smack.provider.ProviderManager;import org.jivesoftware.smack.tcp.XMPPTCPConnection;import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;import org.jivesoftware.smack.util.TLSUtils;import org.jivesoftware.smackx.ping.PingFailedListener;import org.jivesoftware.smackx.receipts.DeliveryReceipt;import org.jivesoftware.smackx.receipts.DeliveryReceiptManager;import org.jivesoftware.smackx.receipts.DeliveryReceiptRequest;import org.jivesoftware.smackx.receipts.ReceiptReceivedListener;import java.io.IOException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.X509TrustManager;import de.duenndns.ssl.MemorizingTrustManager;import uk.co.chrisjenx.calligraphy.CalligraphyConfig;public class ChatApplication extends Application implements ConnectionListener, StanzaListener, PingFailedListener {private ConnectTOJabberTask mConnectTOJabberTask = null;private  XMPPTCPConnection mXmpptcpConnection;private  XMPPConnection mXmppConnection;@Overridepublic void onCreate() {super.onCreate();mConnectTOJabberTask = new ConnectTOJabberTask();}public void setupJabber() {mConnectTOJabberTask.execute();}<BR?private void setConnection() {Log.i(TAG, &amp;quot;SetXMPP Connection&amp;quot;);try {XMPPTCPConnectionConfiguration.Builder connectionConfiguration = XMPPTCPConnectionConfiguration.builder();connectionConfiguration.setServiceName(ChateConstants.Jabber.DOMAIN);connectionConfiguration.setPort(ChatConstants.Jabber.PORT);connectionConfiguration.setHost(ChatConstants.Jabber.HOST);Log.i(TAG, "Username :"+ pref.getName() + " Mobile:" + pref.getMobileNumber());connectionConfiguration.setUsernameAndPassword(pref.getName() + &amp;quot;-&amp;quot; + pref.getMobileNumber(), pref.getMobileNumber());connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);connectionConfiguration.setDebuggerEnabled(true);connectionConfiguration.setCompressionEnabled(true);TLSUtils.acceptAllCertificates(connectionConfiguration);connectionConfiguration.setResource(&amp;quot;ChatApp&amp;quot;);SSLContext sc = SSLContext.getInstance(&amp;quot;TLS&amp;quot;);MemorizingTrustManager mtm = new MemorizingTrustManager(this);sc.init(null, new X509TrustManager[]{mtm}, new java.security.SecureRandom());connectionConfiguration.setCustomSSLContext(sc);connectionConfiguration.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));mXmpptcpConnection = new XMPPTCPConnection(connectionConfiguration.build());mXmpptcpConnection.addAsyncStanzaListener(this, ACCEPT_ALL);mXmpptcpConnection.addConnectionListener(this);mXmpptcpConnection.setPacketReplyTimeout(30000);ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, new DeliveryReceiptRequest().getNamespace(), new DeliveryReceiptRequest.Provider());mXmpptcpConnection.connect();} catch (NoSuchAlgorithmException | KeyManagementException | IOException | SmackException | XMPPException ex) {Log.i(TAG, "XMPP Connection exception :" + ex.getMessage());}
}
}
connectionConfiguration.setServiceName(ChateConstants.Jabber.DOMAIN;connectionConfiguration.setPort(ChatConstants.Jabber.PORT);connectionConfiguration.setHost(ChatConstants.Jabber.HOST);
connectionConfiguration.setUsernameAndPassword(pref.getName() + " + pref.getMobileNumber(), pref.getMobileNumber());
TLSUtils.acceptAllCertificates(connectionConfiguration);connectionConfiguration.setResource("ChatApp");SSLContext sc = SSLContext.getInstance(&amp;quot;TLS&amp;quot;);MemorizingTrustManager mtm = new MemorizingTrustManager(this);sc.init(null, new X509TrustManager[]{mtm}, new java.security.SecureRandom());connectionConfiguration.setCustomSSLContext(sc);connectionConfiguration.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
mXmpptcpConnection = new XMPPTCPConnection(connectionConfiguration.build());mXmpptcpConnection.addAsyncStanzaListener(this, ACCEPT_ALL);mXmpptcpConnection.addConnectionListener(this);mXmpptcpConnection.setPacketReplyTimeout(30000);ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, new DeliveryReceiptRequest().getNamespace(), new DeliveryReceiptRequest.Provider());mXmpptcpConnection.connect();
@Overridepublic void connected(XMPPConnection connection) {Log.i(TAG, &amp;quot;connected&amp;quot;);mXmppConnection = connection;try {mXmpptcpConnection.login();} catch (XMPPException e) {e.printStackTrace();} catch (SmackException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);DeliveryReceiptManager.getInstanceFor(connection).autoAddDeliveryReceiptRequests();DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener() {@Overridepublic void onReceiptReceived(String fromJid, String toJid, String receiptId, Stanza receipt) {Log.i(TAG, &amp;quot;onReceiptReceived :&amp;quot; + fromJid + &amp;quot;-&amp;gt; &amp;quot; + toJid + &amp;quot; :&amp;quot; + receiptId);DeliveryReceipt receiptdata = receipt.getExtension(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);if (receiptdata == null) {return;}notifyAllonReceiptRecived((Message)receipt);}});}@Overridepublic void authenticated(XMPPConnection connection, boolean resumed) {Log.i(TAG, &amp;quot;authenticated&amp;quot;);mXmppConnection = connection;}after this connection successful we will look into how to send message.Stanza : http://tools.ietf.org/html/rfc6120#section-10.1public String sendMessage(String message, String to){Log.i(&amp;quot;XMPPClient&amp;quot;, &amp;quot;Sending text [&amp;quot; + message + &amp;quot;] to [&amp;quot; + to + &amp;quot;]&amp;quot;);Message msg = new Message(to, Message.Type.chat);msg.setBody(message);msg.addExtension(new DeliveryReceipt(msg.getPacketID()));try{mXmppConnection.sendStanza(msg);DeliveryReceiptManager.addDeliveryReceiptRequest(msg);}catch (SmackException.NotConnectedException ex){Log.i(TAG,&amp;quot;Send message exception:&amp;quot;+ex.getMessage());}return msg.getStanzaId();}we need to process the message received and process as we required.@Overridepublic void processPacket(Stanza stanza) throws SmackException.NotConnectedException {Log.i(TAG, &amp;quot;process Packet Received: &amp;quot;+stanza.getFrom());Log.i(TAG, &amp;quot;process Packet Received: &amp;quot;+stanza.getTo());if(stanza instanceof Presence){Log.i(TAG, &amp;quot;process Packet Presence&amp;quot;);}if (stanza instanceof Message){Message message = (Message) stanza;Log.i(TAG, &amp;quot;process Packet Message : &amp;quot; + message.getTo() + &amp;quot;:&amp;quot; + message.getFrom() + &amp;quot;:&amp;quot; + message.getBody());if(null != message.getBody())notifyAllXMPPPacketMessageReceivedListener(message);}if (stanza instanceof IQ ) {Log.i(TAG, &amp;quot;process Packet IQ : &amp;quot;);IQ iq = (IQ) stanza;String packetId = iq.getStanzaId();if (packetId != null &amp;amp;amp;&amp;amp;amp; (iq.getType() == IQ.Type.result || iq.getType() == IQ.Type.error)) {if (iq.getType() == IQ.Type.result) {Log.i(TAG, &amp;quot;process Packet IQ result :&amp;quot;+packetId);} else {Log.i(TAG, &amp;quot;process Packet IQ Error :&amp;quot; +packetId);}}}}
/*** Filter to accept all packets.*/static class AcceptAll implements StanzaFilter {@Overridepublic boolean accept(Stanza packet) {return true;}}

--

--

--

Android Techie — Code Monk

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

On-Prem Kubernetes By Cloud provides — On-Prem EKS | AKS | GKE

On-Prem Kubernetes By Cloud provides - On-Prem EKS | AKS | GKE

Some Python Features

What should web developers avoid implementing

Emergence of Self-Aware and Adaptive Software System

Getting Started with Flutter Desktop

Always-Valid Domain Model

Running Envoy API Gateways on Kubernetes or Consul or both

Let’s talk Big O Notation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anil Gudigar

Anil Gudigar

Android Techie — Code Monk

More from Medium

Chat implementation in Android with Firebase Cloud Firestore database

Find Nearby ATMS with integration of Huawei Site Kit in Banking app (Kotlin)

Creating Android app with all social logins and authentications with 3 simple steps

Android Paintflags Configuration, Explained