Logging into same jabber account from 2 different android devices using Smack
I have developed an application on Android which makes use of the Smack API to connect to XMPP server.In this application I hardcode the User Sign-In information i.e. Username and Password.
xmpp.login("admin", "tigase");
and I get packets from a different user
PacketFilter filter = new AndFilter(new PacketTypeFilter开发者_如何学JAVA(Message.class), new FromContainsFilter("ameya@mwbn43-1"));
Now I am facing the problem that when I run the same app on 2 different android devices one device overwrites the other devices credentials and thus when I am trying to send the data,only one of them gets it. AFAIK in normal Google Chat, user can log into same account from 2 different places and can receive messages on both places at same time.
Can anyone tell me what could be the problem?Has it got something to do with Smack API?
Thanks,
Ameya
What you have to do is set different resource when you login
xmpp.login("admin", "tigase", "a random string here");
The resources is similar in concept to a TCP port. See this link. The way the resource string is use to differentiate devices login into the same account, viz. on desktop, the resource maybe "desktop" and "android" on Android for example. But in your case you have 2 Android devices so you cannot preset the resource. You might want to use a unique identifier string on the Android device like the account name or IMEI number.
Using the resource string, you can either send to a specific user eg
admin@server/12345
where 12345 is your resource or
admin@server
to all login users.
If you are not using the resources string in your application, you can use StringUtils.randomString(20)
to generate a random string of 20 characters. StringUtils
can be found in the Smack package.
As a general rule, the jabber message is sent to the last device which connected to the server, so if you are connecting
youruser@yourserver.org
From two different devices, the one which connected later will be the one getting messages.
BUT, if you specify a resource, then everything is easier, so, lets say you are connected from your laptop and your cellphone, then you can login with a full jid (jabber id), using a resource.
youruser@yourserver.org/laptop
youruser@yourserver.org/cellphone
Now, if you want to send a jabber message to be received specifically by the client connected from your cellphone, just make sure that the message is sent to "youruser@yourserver.org/cellphone" instead of just "youruser@yourserver.org"
The reason your only getting the item delivered to one device and not both is that Tigase, unlike Google Chat, is following the XEP and delivering the message to the connection with the lowest priority (or to the device that logged in last if the priority is the same.)
You need to be careful when trying to bind your JID to a specific resource and look to make sure the resource you requested was actually assigned. The server may not give it to you because it conflicts with an existing JID's defined resource.
Google Chat servers do something "special" in that they deliver the message to any connected JID regardless of resource or priority setting. There are some changes being suggested to allow that behaviour by design but those have not yet been accepted by the XSF yet.
If you want delivery of messages to all connected devices I would suggest that you look into creating a PubSub node on your Tigase server and set the node to deliver to any online JID - then all of your devices will receive any message that you push to the node if they are online.
精彩评论