I'm working on an Android app that utilizes ASmack to send XMPP messages to and from a server in a background service. I can join a MultiUserChat (MUC) by calling MultiUserChat.join(connection.getUser());. I can confirm that I joined the chat by calling MultiUserChat.isJoined();, which returns true. Also, since I'm using www.hosted.im, I can see that I am in the conference room using their online UI. In another function, I try to retrieve the list of joined rooms, using MultiUserChat.getJoinedRooms(connection, connection.getUser());, but that returns an empty iterator.
private XMPPConnection connection;
/*... Connect to server and login with username and password ...*/
public Iterator<String> getJoinedRooms() {
Log.i(ChatListActivity.TAG, "Trying to get joined rooms");
Iterator<String> result = null;
if(connection != null) {
Log.i(ChatListActivity.TAG, "Returning joined chat rooms as " + connection.getUser());
result = MultiUserChat.getJoinedRooms(connection, connection.getUser());
while(result.hasNext()) {
Log.w(ChatListActivity.TAG, result.next());
}
} else {
Log.e(ChatListActivity.TAG, "Cannot get joined rooms. Connection == NULL");
}
if(result == null || (result != null && !result.hasNext())) {
ArrayList<String> resultArr = new ArrayList<String>();
resultArr.add(getString(R.string.no_chat_rooms_joined));
result = resultArr.iterator();
Log.i(ChatListActivity.TAG, "Returning EMPTY ITERATOR for joined chat rooms");
}
return result;
}
public void joinRoom(String room) {
if(connection != null) {
Log.i(ChatListActivity.TAG, "Joining room " + room);
// Create a MultiUserChat using a Connection for a room
MultiUserChat muc2 = new MultiUserChat(connection, "testroom#conference.konstadtest.p1.im");
try {
muc2.join(connection.getUser());
muc2.grantVoice(connection.getUser());
muc2.grantMembership(connection.getUser());
if(muc2.isJoined())
Log.w(ChatListActivity.TAG, "Joined room " + room + " as " + connection.getUser());
else
Log.w(ChatListActivity.TAG, "Failed to join " + room + " as " + connection.getUser());
} catch (XMPPException e) {
e.printStackTrace();
Log.w(ChatListActivity.TAG, "Cannot join room " + room);
}
} else {
Log.w(ChatListActivity.TAG, "Cannot join room " + room + " because connection is NULL");
}
}
What am I doing wrong? I called SmackAndroid.init(getApplicationContext()); before calling anything else.
Thank you for the help,
Chris
What i did is that i add a packet listener after getting get joined rooms.. i was also getting an empty list but when i debug i check that the rooms was getting returned in the resultant xml stanze that was sent by the server therefore i manually add ha packet listener like this:
public void AddPacketListener(){
PacketFilter filter = new IQTypeFilter(IQ.Type.RESULT);
MyService.getConnection().addPacketListener(new PacketListener()
{
public void processPacket(Packet paramPacket) {
if(paramPacket.getFrom().equals(MyService.getConnection().getUser())){
String xml=paramPacket.toXML();
String from[];
System.out.println(xml);
from=paramPacket.getFrom().split("/");
Pattern pattern = Pattern.compile("<item jid=\"(.*?)/>");
Matcher matcher = pattern.matcher(xml);
String parts[];
Roomlist.clear();
while (matcher.find()) {
parts=matcher.group(1).split("#");
Roomlist.add(parts[0]);
}
return;
}
}
},filter);
}
Related
Hey i am developing Chat Application using XMPP Smack Library. Recently i am working on Group Chat While sending Group message some message will be drop so receiver wouldn't receives message from the sender side. it will gives me 400 bad request.
it is working sometimes. and sometimes not work
here i found this kind of message in 400 bad request.
<?xml version="1.0" encoding="UTF-8"?>
<message to="156#abc.com/Android" id="nXlV6-1144" type="error" from="24#confrence.abc.com/156#abc.com.com">
<received xmlns="urn:xmpp:receipts" id="nXlV6-1142" />
<error code="400" type="modify">
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
</error>
</message>
if successfully send message it will give this kind of message.
<?xml version="1.0" encoding="UTF-8"?>
<message to="156#abc.com/Android" id="nXlV6-1411" type="groupchat" from="24#conference.abc.com/156#abc.com">
<body>eyu4u4</body>
<chatDetail xmlns="jabber:x:oob">
<UID>156</UID>
<time>04:20 PM</time>
<user_icon>24_group_icon.jpg</user_icon>
<SentTime>1474368652960</SentTime>
<USERName>vasudev89</USERName>
<user_name>cryan</user_name>
<message>eyu4u4</message>
<type>group</type>
<phone_number>24</phone_number>
</chatDetail>
<request xmlns="urn:xmpp:receipts" />
</message>
how i can send message persistently? Any idea?
Thank You in Advance.
here is my code sending muc message:
public boolean sendGroupMessage(Message message, String strGroupID) {
DeliveryReceiptRequest.addTo(message);
try {
Log.i(TAG, "sendGroupMessage");
//Log.i("JOIN MUC","To join group chat: " + groupChat.getClassId());
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(AbstractXMPPConnection);
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat muc = manager.getMultiUserChat(strGroupID + AppWSConstants.XMPP_JID_GROUP_CHAT_SUFFIX);
muc.sendMessage(message);
return true;
} catch (NotConnectedException e) {
e.printStackTrace();
}
return false;
}
#LearnPainLess, follow these steps to solve group chat issue
-when creating groups, save the jid of group in the database, like "somegroup#conference.{domain}.com"
-create background task for creating xmpp connection (this way it will always be connected)
-after logging in to xmpp, get the group names from the database and connect to them
also, in openfire, Group Chat > Group Chat Settings > Edit Icon > Default Room Settings > Check "Make Room Persistant"
also, in other settings > Never kick idle users
I have an XmppBase class where i put all my xmpp code
All listeners in seperate folder
Connection is stored in static variable and i retrive it using
Utils.getConnection()
// this function m calling from background service and everywhere if not connectect to xmpp
public static XMPPConnection CreateXmppConnection() {
if (Utils.getConnection() == null) {
try {
Boolean isConnected = new XmppAsync(mUsername, mPassword,context).execute().get();
if (isConnected && Utils.getConnection() != null) {
RegisterConnListeners(Utils.getConnection());
updateMyProfileImg();
// connect to all groups
DBAdapter adapter = new DBAdapter(context);
adapter.openForRead();
List<UserDetail> groups = new ArrayList<>();
adapter.addAllGroups(groups);
adapter.addPastChatGroups(groups);
adapter.close();
for(UserDetail g : groups)
{
CreateXmppMUCSession(g.getGroupTemp());
}
return Utils.getConnection();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
} else
return Utils.getConnection();
}
// get muc chat manager
public static MultiUserChatManager getMucManager() {
if(mucManager != null)
return mucManager;
if (Utils.getConnection() != null) {
return MultiUserChatManager.getInstanceFor(Utils.getConnection());
} else {
if (CreateXmppConnection() != null)
return MultiUserChatManager.getInstanceFor(Utils.getConnection());
else {
Log.v("error", "Some Error Occured");
Toast.makeText(context, "Cant Connect to Xmpp", Toast.LENGTH_SHORT).show();
return null;
}
}
}
// create muc session and m passing group name - call when you open chat page
public static void CreateXmppMUCSession(String gName)
{
RegisterGroupChatListeners(gName);
}
// connect to muc if not already connected
public static void RegisterGroupChatListeners(String groupName)
{
try {
mStateManager = getChatStateManager();
multiUserChat = getMUC(groupName);
// if(multiUserChat != null) {
multiUserChat.addMessageListener(new MyMUCMessageListener());
try {
if (!multiUserChat.isJoined()) {
DiscussionHistory discussionHistory = new DiscussionHistory();
discussionHistory.setMaxStanzas(0);
multiUserChat.join(new MyPrefrence(context).getUsername().split("#")[0], "123",
discussionHistory, SmackConfiguration.getDefaultPacketReplyTimeout());
}
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
// }
}
catch (Exception ex)
{
//
}
}
// get muc
public static MultiUserChat getMUC(String groupName)
{
// Log.v("nick",multiUserChat.getNickname() + " , g = " + groupName);
// if(multiUserChat != null && multiUserChat.getRoom().contains(groupName))
// {
// return multiUserChat;
// }
if (Utils.getConnection() != null) {
MultiUserChatManager chatManager = getMucManager();
if (chatManager != null) {
return chatManager.getMultiUserChat(groupName);
} else {
Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show();
return null;
}
} else {
if (CreateXmppConnection() != null) {
MultiUserChatManager chatManager = getMucManager();
if (chatManager != null) {
return chatManager.getMultiUserChat(groupName);
} else {
Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show();
return null;
}
}
else {
Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show();
return null;
}
}
}
and whenever i want to send message i just call this
public static Boolean sendMUCChatMsg(Message msg)
{
if(multiUserChat != null)
try {
multiUserChat.sendMessage(msg);
return true;
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
return false;
}
Sorry if it looks clumpsy, if I missed any function there let me know, but this is working code which i am using
try this,
I modified your last function
static MultiUserChat multiUserChat;
// call this function when you open the chat window
private void CreateGroupConnection(String strGroupID ) {
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(AbstractXMPPConnection);
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat multiUserChat = manager.getMultiUserChat(strGroupID + AppWSConstants.XMPP_JID_GROUP_CHAT_SUFFIX);
return multiUserChat;
}
// whenever sending message from chat call this
publilc static void sendMucMessage(Message message){
if(multiUserChat != null)
multiUserChat.sendMessage(message);
}
I am working on "seen and delivered" in MUC and facing with this issue when replying back with the same packet id, still testing but I think in your case you should move your xmpp connection to background service and after connecting to xmpp on device launch up, connect to all the muc in your database. This way you will always be connected to groups.
cause : according to me, when the other user is not connected to the muc and you send a message or when you reply to the group with the same packet id.
Note: I am using multiuserchat.sendmessage to send group message and chat.sendmessage to send message to single user
SMACK 4.1
** update **
I fixed it by creating new packet instead of modifying the one i am receiving
here is the packet
Message msgg = new Message();
msgg.setBody(message.getPacketID());
msgg.setSubject(MessageModel.CHAT_STATUS_SEEN + "");
XmppBase.sendMUCChatMsg(msgg);
in your case, try with simple packet first. if all works well, then add extension one by one and see where you get the error, Thanks
I have developed application for two different sensors. They are working fine separately but when I try to use them togather and create two diffent buses than Alljoyn gives this exception.
org.alljoyn.services.common.BusAlreadyExistException: The object has
been set previously with a BusAttachment.
Below is my source code for connection. Can anyone tell me why I'm having this issue.
private void connect()
{ org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
bus = new BusAttachment("ControlPanelBrowser", BusAttachment.RemoteMessage.Receive);
bus.registerBusListener(new BusListener());
Status status = bus.registerBusObject(mControlPanelSignalInterface, Constants.SERVICE_PATH);
if (status != Status.OK) {
Log.d(TAG, "Problem while registering bus object");
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
srpPassword = settings.getString(PREFS_PASSWORD, DEFAULT_SECURED_SRP_PASSWORD);
SrpAnonymousKeyListener authListener = new SrpAnonymousKeyListener(this, logger, AUTH_MECHANISMS);
Status authStatus = bus.registerAuthListener(authListener.getAuthMechanismsAsString(),
authListener, getKeyStoreFileName());
if ( authStatus != Status.OK ) {
Log.e(TAG, "Failed to register AuthListener");
}
status = bus.connect();
if (Status.OK == status){
String daemonName = Constants.DAEMON_NAME_PREFIX + ".ControlPanelBrowser.G" +
bus.getGlobalGUIDString();
int flag = BusAttachment.ALLJOYN_REQUESTNAME_FLAG_DO_NOT_QUEUE;
Status reqStatus = bus.requestName(daemonName, flag);
if (reqStatus == Status.OK) {
Status adStatus = bus.advertiseName(Constants.DAEMON_QUIET_PREFIX +
daemonName, SessionOpts.TRANSPORT_ANY);
if (adStatus != Status.OK){
bus.releaseName(daemonName);
Log.e(TAG, "Failed to advertise daemon name: '" + daemonName + "', Error: '" + status + "'");
}
else{
Log.d(TAG, "Succefully advertised daemon name: '" + daemonName + "'");
}
}
else {
Log.e(TAG, "Failed to request daemon name: '" + daemonName + "', Error: '" + status + "'");
}
}
status = bus.registerSignalHandlers(mControlPanelSignalInterface);
if (status != Status.OK) {
Log.d(TAG, "Problem while registering signal handlers");
}
// Initialize AboutService
aboutClient = AboutServiceImpl.getInstance();
aboutClient.setLogger(logger);
try {
aboutClient.startAboutClient(bus);
for (String iface : ANNOUNCE_IFACES) {
aboutClient.addAnnouncementHandler(this, new String[] {iface});
}
} catch (Exception e) {
logger.error(TAG, "Unable to start AboutService, Error: " + e.getMessage());
}
}
use registerBusObject twince and then you can make one signle bus attachment
why dont you create two Interfaces, one interface for one sensor respectively. then add these two interfaces in a class which implements these two interfaces and the busObject and register an implemntation of this class as a BusObject.
For example
Sensor1_interface.java and Sensor2_interface.java //are my two interface classes
create a new class Sensor_InterfaceList which inplements the two interfaces and the BusObject
class Sensor_InterfaceList implements Sensor1_interface,Sensor2_interface,BusObject
{
// implment your interfaces here
.....
}
private Sensor_InterfaceList mySensor_InterfaceList;
mySensor_InterfaceList = new Sensor_InterfaceList();
myBus.registerBusObject(mySensor_InterfaceList,"/your/path");
This should solve your problem :)
First I change owner affliation to administrator by revokeOwnership() nad than I try revokeAdmin() but it's not working,than I try following method for change admin affliation to none
when I call following method at time of leave group gives forbidden(403) Error,How to solve this Error and set admin affliation none in muc.
private void changeAffiliationByAdmin(String jid, String affiliation,String grpnm) throws XMPPException
{
MUCAdmin iq = new MUCAdmin();
iq.setTo(grpnm + "#conference." + Utils.SERVER_NAME);
iq.setType(IQ.Type.SET);
MUCAdmin.Item item = new MUCAdmin.Item(affiliation,null);
item.setJid(jid + "#" + Utils.SERVER_NAME);
iq.addItem(item);
// Wait for a response packet back from the server.
filter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(filter);
// Send the change request to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
MUCAdmin answer = (MUCAdmin) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
}
else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
}
if i user this code for change owner affliation to none than it's not woking,
MUCOwner iq = new MUCOwner();
iq.setTo(grpnm + "#conference." + Utils.SERVER_NAME);
iq.setType(IQ.Type.SET);
System.out.println("before affliation changed" + jid + grpnm);
// Set the new affiliation.
MUCOwner.Item item = new MUCOwner.Item(affiliation,null);
item.setJid(jid + "#" + Utils.SERVER_NAME);
iq.addItem(item);
System.out.println("after affliation changed" + jid + grpnm);
// Wait for a response packet back from the server.
filter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(filter);
// Send the change request to the server.
System.out.println("call IQ packet" + jid + grpnm);
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
}
else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
I am working on an Android application that uses Twilio to make phone calls and to send SMS.
While making a call I get the IVRS message "Your call cannot be completed because of two missing digits".
The FROM number is "+18669135337" and TO number is "(949) 439-7570" or "+19494397570"
What could be the reason thats causing this issue?
This is the code snippet thats making the call
public void connect(String toNumber, ConnectionListener listener) {
Map<String, String> parameters = new HashMap<String, String>();
Log.e(TAG, "Calling from " + mFromNumber);
Log.e(TAG, "Calling to " + toNumber);
Log.e(TAG, "mDevice State is " + mDevice.getState());
parameters.put(Constants.KEY_TO_NUMBER, toNumber);
parameters.put(Constants.KEY_FROM_NUMBER, mFromNumber);
mConnection = mDevice.connect(parameters, listener);
Log.e(TAG, "Connection status " + mConnection);
if (mConnection == null) {
Log.w(TAG, "Failed to create new connection");
}
}
I am using parse for my data in android app. for some reason the code inside query.findInBackground is not getting executed.
public List<Date> sessionHeaderFetch(){
Log.d("test", "session fetch entry");
ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class);
final List<Date> sessionHeaders = null;
query.findInBackground(new FindCallback<Sessions>() {
#Override
public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) {
Log.d("test", "session internal entry");
if (e == null) {
Log.d("test", "Retrieved " + sessionsObjects.size() + " sessions");
for (Sessions session : sessionsObjects) {
sessionHeaders.add(session.getNetsDate());
};
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
Log.d("test", "session last value");
return sessionHeaders;
}
the code inside public void done() is not all invoked.
I don't think you have understood how to query correctly in Parse.
When you define the parse query. The get query should contain the name of the table that you are trying to query. Also the queries returned will be ParseObjects normally, so I would expect that your callback should be new FindCallback().
I've adjusted the parse query below.
ParseQuery<Sessions> query = ParseQuery.getQuery("ParseTableName");
final List<Date> sessionHeaders = null;
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> sessionsObjects, com.parse.ParseException e) {
Log.d("test", "session internal entry");
if (e == null) {
// Find succeeded, so do something
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
Obviously you will need to replace "ParseTableName" with the name of the table that you are trying to query in parse.
I actually solved it by using an ArrayList, there were some problems with the List initialization and also the results are fetched asynchronous manner so my functions which calls these functions were getting null values so I had to write by calling functions that calls the parse fetch functions asynchronously as well.
but the query can be written like this and it works.
public void sessionFetch(Date headers, final ParseIOListener<Sessions> listener){
ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class);
Log.d("test", "input header" + headers );
query.whereEqualTo("NetsDate",headers);
final ArrayList<Sessions> sessionsData = new ArrayList<Sessions>();
query.findInBackground(new FindCallback<Sessions>() {
#Override
public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) {
if (e == null) {
for (Sessions session : sessionsObjects) {
Log.d("test", "output objects" + session.toString() );
sessionsData.add(session);
Log.d("test", "Retrieved -- sessions" + sessionsObjects.size() );
}
listener.onDataRetrieved(sessionsData);
} else {
listener.onDataRetrieveFail(e);
}
}
});
}
If you want more details on implementing this, check this link
The reason why you feel the code is not being invoked is because you are returning from the method before the ".findInBackground" operation has completed. Remember the query is performed on a background thread. So this is how your code will run:
ParseQuery query = ParseQuery.getQuery(Sessions.class);
final List sessionHeaders = null;
------> 1. query.findInBackground (Popped onto background thread)
return sessionHeaders; (by the time you get here, sessionHeaders will probably still be null).
So change the logic of the code and wait till the callback returns before doing any processing on the "sessionHeaders" object.