I am using quickblox api for video chat and i want to get online available user .I know that it can be done through roster but i don't know how to get roster and how to add entries in roster .i want this through quickblox connection and dont know how to get xmpp connection.
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
#Override
public void connectionCreated(Connection arg0) {
Log.i(TAG, "receive xmpp connection : " + arg0);
connection = arg0;
roster = arg0.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;
Log.e(TAG, "user count" + entries.size());
for (RosterEntry entry : entries) {
presence = roster.getPresence(entry.getUser());
Log.i(TAG, "" + entry.getUser());
Log.i(TAG, "" + presence.getType().name());
Log.i(TAG, "" + presence.getStatus());
}
}
});
So at the start of your program register that XMPPConnection listener, usually it take few seconds to receive connection object.
But it will work only if you will use creatEntry only in that case rooster will see those created users.
To creat entry using Roster use next code:
try {
rooster.createEntry("name", "user_id", null);
} catch (XMPPException e) {
e.printStackTrace();
}
I didn't use any group, and with success see user on second device.
Related
I am creating an instant messaging app in android using smack library and openfire as a server but i cannot implement the feature of the person with whom the current user is talking to. i.e. like when user read the message or when he starts typing.
Is there any way of achieving this using smack or other ?
For knowing which user is current you must implement your own in your logic. You must use Roster (contact list in xmpp servers) to get contacts of current user and save them in database or somewhere. Then create an activity to show contacts in a list. Each contact has a unique jid that can be distinguished from others with it. So with click on each contact, send it's object(include jid) to chat-activity. In chat-activity you must get previous messages from database or MAM(archived messages in server) and you can send a message to current contact(set contact jid as To).
To achieving delivery of message you must use this link. you can set request of it with this code:
Message message = … //make your stanza
DeliveryReceiptRequest.addTo(message); //add delivery request to message
connection.sendStanza(message); //send message
then you can be notified of delivery with this code:
private void setDelRecListener() {
DeliveryReceiptManager d = DeliveryReceiptManager.getInstanceFor(connection);
d.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
Msg msg = F.getMsgBySid(receiptId);
if (msg == null)
return;
Boolean isUpdated = F.setMsgDelivered(msg);
Log.i("m/serv/UpdateDelivery", "for: " + receiptId + (isUpdated ? " Founded&Updated" : " NotFounded"));
if (isUpdated) {
BCTool.notifyPMDelivered(msg.id, msg.conv.frnd.getBareJid());
}
}
});
}
Keep in mind that every stanza has a sid(stanza id) and you must save each corresponding sid to message model in database when send is successful. This way you can detect which message delivery you got.
- For sending chat states like composing you can use this method:
public void sendChatState(String _jid, ChatState chatState) {
try {
Message msg = new Message();
msg.addExtension(new ChatStateExtension(chatState));
msg.setTo(JidCreate.bareFrom(_jid));
msg.setType(Message.Type.chat);
connection.sendStanza(msg);
Log.e("m/service", "ChatStateSent");
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) {
Log.e("m/service", "ChatState Not Sent: " + e.getMessage());
e.printStackTrace();
}
}
You must set a timer to prevent send composing in next 5Sec and reset timer when a character typed.
Consider reading this: ChatStateNotifications
I have offline messages option enabled in the openfire server.But I'm unable to get offline messages
User A is online ,User B is online ,in this case I'm able to get messages.
Now User B Turned off his WiFi(Note : User A waited till the user B Session completely killed in the server )
now User A send a message to User B
in this case I'm able to see the message in the openfire offline table.
Now User B Comes online again server is sending the message to user B as the server come to know that User B is online
(Message disappeared from offline messages table ).
But User B is not going to receive that message.
connection.login(userName, userPwd, UiUtility.getMyPhoneNO());
PacketFilter filter = new PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);
packetListener =new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
}
}
};
connection.addPacketListener(packetListener, filter);
Again after successful login im able to chat normally.But I wonder why those offline messages are missing ? .My PacketListener unable to catch those offline messages .Please Help me
Asmack is depreceated. Use Smack. An Open Source XMPP Client Library written in Java for JVMs and Android. Add the following lines to your gradle file:
compile 'org.igniterealtime.smack:smack-android:4.1.3'
compile 'org.igniterealtime.smack:smack-tcp:4.1.3'
compile 'org.igniterealtime.smack:smack-extensions:4.1.3'
The problem is easy to be solved.
Before making connection with the XMPP server just register providers using ProviderManager class provided by ASmack library.
If this can't solve ur problem visit ur local server and search for offline messages, and select the option ALWAYS STORE setting the storage limit to be 1000 kb. It is 100 kb by default.
Hope this works.
After lot struggle, I have resolved the issue. In your openfire admin page, go to "client settings" and reduce the idle time from 360sec (by default) to 1 sec(may be). Only then when you disconnected from Internet, it can detect that you are offline and preserve rest of the messages as OFFLINE.
#Override
public void onNetworkConnectionChanged(boolean isConnected) {
if(isConnected){
new Thread() {
public void run() {
try {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setUsernameAndPassword("phone", "admin");
builder.setSendPresence(true);
builder.setServiceName(<Service name>);
builder.setHost(<Host name>);
builder.setResource("Test");
builder.setDebuggerEnabled(true);
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Available");
connection = new XMPPTCPConnection(builder.build());
connection.connect();
connection.login();
Presence presence123 = new Presence(Presence.Type.available);
presence123.setStatus("Available");
try {
connection.sendStanza(presence123);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
StanzaFilter filter = new AndFilter(new StanzaTypeFilter(Message.class));
PacketListener myListener = new PacketListener()
{
public void processPacket(Stanza stanza)
{
retrieveMessage(stanza,userType);
}
};
connection.addPacketListener(myListener, filter);
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
} catch (SmackException | XMPPException | IOException e) {
e.printStackTrace();
}
//return connection.isConnected();
}
}.start();
The above is working fine and able to retrieve the offline messages. The method "retrieveMessage(stanza,userType);" is used to process the incoming message and update the Adapter. Make sure to send the Presence as "Available" when you reconnect. Please let me know if there are still any issues.
I am working on websocket connection to communicate with server.
Now, we have established a connection and able to send and receive message using Autobahn library.
Here is the code:
private void start() {
// wsuri is the url which has to be hit for connecting to the server i.e
// "ws://hostname:port "
final String wsuri = "ws://" + mHostname.getText() + ":"
+ mPort.getText();
// showing status on the UI.
mStatusline.setText("Status: Connecting to " + wsuri + " ..");
// enabling disconnect button clicking on which the connection will be
// disconnected.
setButtonDisconnect();
try {
mConnection.connect(wsuri, new WebSocketConnectionHandler() {
#Override
public void onOpen() {
// If connection is open, then status will be updated to
// "connected to..."
mStatusline.setText("Status: Connected to " + wsuri);
savePrefs();
// enabling mSendMessage button and mMessage edit Text.
mSendMessage.setEnabled(true);
mMessage.setEnabled(true);
}
#Override
public void onTextMessage(String payload) {
Log.d("ss", payload);
alert(payload);
}
#Override
public void onClose(int code, String reason) {
// If connection is lost, alert will be shown connection is
// lost and status will be set to "Ready".
alert("Connection lost.");
mStatusline.setText("Status: Ready.");
setButtonConnect();
// disabling mSendMessage button and mMessage edit Text.
mSendMessage.setEnabled(false);
mMessage.setEnabled(false);
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
We just have a single method onTextMessage(String payload) to handle response from server.
and to send message to server we use mConnection.sendTextMessage(mMessage.getText().toString());
But now we are facing issue that how to hit api on that server?or lets say I want some list of devices or device_Status etc how it will happen??
Any idea how to do that?? At present I am thinking of sending some "key":"pair" value in message in form of json, by which the server will identify what request I have made...
I am still not sure whether this is the right way? Please if somebody can help me in this.
I followed the guidlines given in the following SO question to connect to facebook chat using xmpp and I am able to connect to facebook and pull the correct number of contacts but when it prints the contacts they are all random numbers #chat.facebook.com and all return offline.
Android Facebook chat example project
public void connectToFb() throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(SecurityMode.required);
config.setRosterLoadedAtLogin(true);
config.setTruststorePath("/system/etc/security/cacerts.bks");
config.setTruststorePassword("changeit");
config.setTruststoreType("bks");
config.setSendPresence(false);
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom());
config.setCustomSSLContext(sc);
} catch (GeneralSecurityException e) {
Log.w("TAG", "Unable to use MemorizingTrustManager", e);
}
XMPPConnection xmpp = new XMPPConnection(config);
try {
xmpp.connect();
xmpp.login("user.name", "password"); // Here you have to used only facebookusername from facebookusername#chat.facebook.com
Roster roster = xmpp.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("Connected!");
System.out.println("\n\n" + entries.size() + " buddy(ies):");
// shows first time onliners---->
String temp[] = new String[50];
int i = 0;
for (RosterEntry entry : entries) {
String user = entry.getUser();
Log.i("TAG", user);
}
} catch (XMPPException e) {
xmpp.disconnect();
e.printStackTrace();
}
}
It sounds like you just want to readable name, so try using
rosterEntry.getName()
which returns the users name, instead of
rosterEntry.getUser()
which returns the JID.
Not sure about your offline problem though. How are you checking? You have to set up a roster listener to get changes in presence.
Its a bug in XMPP library . There is a work around for that .
Step 1: Connect to XMPP.
Step 2: Login to facebook account through xmpp.
Step 3: get online friend list using this fql query .
SELECT uid, name, online_presence ,
sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
Then i concat address with string uid#chat.facebook.com and communicate via XMPP.
(It's not 100% clear what problem you're having regarding the online/offline thing may be a bug or something you're doing incorrectly), but you won't get back the user's actual user IDs in the response, this is mentioned in the docs:
The user's own Jabber ID (JID) is different from the Jabber ID that their contacts will see because the translation is done internally.
I am able to add Entry to the Xmpp account by using this code. i can't get subscription "both", instead of this i am getting none.
roster.createEntry("abc#xyz.com", "abc", null);
How to add entry with the presence type=both, when i am subscribing entry to this account. I want to know whether xmpp publish-subscribe functionality ?
How to get Inbound presence notifications ?
How to send Outbound presence notifications ?
EDIT :
public void Addcontact() {
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
Roster roster = m_connection.getRoster();
if(!roster.contains("pa#ace.com")) { try {
roster.createEntry("pa#ace.com", "pa", null);
} catch (XMPPException e) {
e.printStackTrace();
}
}else {
Log.i("Acc = ", "contains");
}
}
I am adding entry like this still i am getting Presence Type = none..
Here is how I add another friend in my application.
protected void doAddContactToListAsync(String address, String name,
ContactList list) throws ImException {
debug(TAG, "add contact to " + list.getName());
Presence response = new Presence.Type.subscribed);
response.setTo(address);
sendPacket(response);
Roster roster = mConnection.getRoster();
String[] groups = new String[] { list.getName() };
if (name == null) {
name = parseAddressName(address);
}
try {
roster.createEntry(address, name, groups);
// If contact exists locally, don't create another copy
Contact contact = makeContact(name, address);
if (!containsContact(contact))
notifyContactListUpdated(list,
ContactListListener.LIST_CONTACT_ADDED, contact);
else
debug(TAG, "skip adding existing contact locally " + name);
} catch (XMPPException e) {
throw new RuntimeException(e);
}
}
Just use the essential part
Presence response = new Presence.Type.subscribed);
response.setTo(address);
sendPacket(response);
Roster roster = mConnection.getRoster();
roster.createEntry(address, name, groups);
In order to listen to incoming request, register addPacketListener to your connection
mConnection.addPacketListener(new PacketListener() {
#Override
public void processPacket(Packet packet) {
Presence presence = (Presence) packet;
if (presence.getType() == Type.subscribe) {
debug(TAG, "sub request from 1" + address);
//Implement accept or reject depend on user action.
mContactListManager.getSubscriptionRequestListener()
.onSubScriptionRequest(contact);
//or you can test with send back Presence.subscribe first and send Presence.subscribed back to requester.
} else {// Handle other Presence type.
int type = parsePresence(presence);
debug(TAG, "sub request from " + type);
contact.setPresence(new Presence(type,
presence.getStatus(), null, null,
Presence.CLIENT_TYPE_DEFAULT));
}
}
}, new PacketTypeFilter(Presence.class));
mConnection.connect();
The right order:
User1 send Subscribe to user2.
User2 send Subscribe and Subsribed back to user1.
User1 send Subsribed to user2.
Another SO question you can check