Skip to content

Commit

Permalink
Fix users not receiving message problem
Browse files Browse the repository at this point in the history
  • Loading branch information
rutura committed Feb 18, 2017
1 parent 1788a8e commit cd00718
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 72 deletions.
1 change: 0 additions & 1 deletion Rooster/.idea/.name

This file was deleted.

6 changes: 0 additions & 6 deletions Rooster/.idea/encodings.xml

This file was deleted.

10 changes: 2 additions & 8 deletions Rooster/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 1 addition & 24 deletions Rooster/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -34,7 +34,7 @@ private void populateWithInitialContacts(Context context)
//Create the Foods and add them to the list;


Contact contact1 = new Contact("zoza@izuba.tech");
Contact contact1 = new Contact("user@testServer.com");
mContacts.add(contact1);
Contact contact2 = new Contact("User2@server.com");
mContacts.add(contact2);
Expand Down
Expand Up @@ -9,6 +9,7 @@

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ChatMessageListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.ReconnectionManager;
Expand All @@ -24,7 +25,7 @@
/**
* Created by gakwaya on 4/28/2016.
*/
public class RoosterConnection implements ConnectionListener,ChatMessageListener {
public class RoosterConnection implements ConnectionListener {

private static final String TAG = "RoosterConnection";

Expand All @@ -34,6 +35,7 @@ public class RoosterConnection implements ConnectionListener,ChatMessageListener
private final String mServiceName;
private XMPPTCPConnection mConnection;
private BroadcastReceiver uiThreadMessageReceiver;//Receives messages from the ui thread.
private ChatMessageListener messageListener;


public static enum ConnectionState
Expand Down Expand Up @@ -86,6 +88,47 @@ public void connect() throws IOException,XMPPException,SmackException
mConnection.connect();
mConnection.login();

messageListener = new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
///ADDED
Log.d(TAG,"message.getBody() :"+message.getBody());
Log.d(TAG,"message.getFrom() :"+message.getFrom());

String from = message.getFrom();
String contactJid="";
if ( from.contains("/"))
{
contactJid = from.split("/")[0];
Log.d(TAG,"The real jid is :" +contactJid);
}else
{
contactJid=from;
}

//Bundle up the intent and send the broadcast.
Intent intent = new Intent(RoosterConnectionService.NEW_MESSAGE);
intent.setPackage(mApplicationContext.getPackageName());
intent.putExtra(RoosterConnectionService.BUNDLE_FROM_JID,contactJid);
intent.putExtra(RoosterConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
mApplicationContext.sendBroadcast(intent);
Log.d(TAG,"Received message from :"+contactJid+" broadcast sent.");
///ADDED

}
};

//The snippet below is necessary for the message listener to be attached to our connection.
ChatManager.getInstanceFor(mConnection).addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {

//If the line below is missing ,processMessage won't be triggered and you won't receive messages.
chat.addMessageListener(messageListener);

}
});

ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
reconnectionManager.setEnabledPerDefault(true);
reconnectionManager.enableAutomaticReconnection();
Expand Down Expand Up @@ -118,7 +161,7 @@ private void sendMessage ( String body ,String toJid)
{
Log.d(TAG,"Sending message to :"+ toJid);
Chat chat = ChatManager.getInstanceFor(mConnection)
.createChat(toJid,this);
.createChat(toJid,messageListener);
try
{
chat.sendMessage(body);
Expand All @@ -131,35 +174,6 @@ private void sendMessage ( String body ,String toJid)
}


@Override
public void processMessage(Chat chat, Message message) {

Log.d(TAG,"message.getBody() :"+message.getBody());
Log.d(TAG,"message.getFrom() :"+message.getFrom());

String from = message.getFrom();
String contactJid="";
if ( from.contains("/"))
{
contactJid = from.split("/")[0];
Log.d(TAG,"The real jid is :" +contactJid);
}else
{
contactJid=from;
}

//Bundle up the intent and send the broadcast.
Intent intent = new Intent(RoosterConnectionService.NEW_MESSAGE);
intent.setPackage(mApplicationContext.getPackageName());
intent.putExtra(RoosterConnectionService.BUNDLE_FROM_JID,contactJid);
intent.putExtra(RoosterConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
mApplicationContext.sendBroadcast(intent);
Log.d(TAG,"Received message from :"+contactJid+" broadcast sent.");

}



public void disconnect()
{
Log.d(TAG,"Disconnecting from serser "+ mServiceName);
Expand Down Expand Up @@ -191,6 +205,7 @@ public void disconnect()
public void connected(XMPPConnection connection) {
RoosterConnectionService.sConnectionState=ConnectionState.CONNECTED;
Log.d(TAG,"Connected Successfully");

}

@Override
Expand All @@ -199,6 +214,8 @@ public void authenticated(XMPPConnection connection) {
Log.d(TAG,"Authenticated Successfully");
showContactListActivityWhenAuthenticated();



}

@Override
Expand Down
2 changes: 1 addition & 1 deletion Rooster/gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

0 comments on commit cd00718

Please sign in to comment.