I have successfully created a user using the following code:
accountmanager = new org.jivesoftware.smack.AccountManager(connection);
accountmanager.createAccount(fbuserid,fbuserid);
But I am not able to add other users to the logged in user's roster using the following code :
public void createEntry(String user, String name, String[] groups) throws XMPPException {
// Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item(user, name);
if (groups != null) {
for (String group : groups) {
if (group != null) {
item.addGroupName(group);
}
}
}
rosterPacket.addRosterItem(item);
// Wait up to a certain number of seconds for a reply from the server.
PacketCollector collector = connection.createPacketCollector(
new PacketIDFilter(rosterPacket.getPacketID()));
connection.sendPacket(rosterPacket);
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
}
// If the server replied with an error, throw an exception.
else if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
// Create a presence subscription packet and send.
Presence presencePacket = new Presence(Presence.Type.subscribe);
presencePacket.setTo(user);
connection.sendPacket(presencePacket);
}
I am always getting the response as null. Someone please help me to solve this and Thanks in advance
Rosters and presence use a permissions-based model where users must give permission before they are added to someone else's roster. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request.
If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:
Automatically accept all presence subscription requests.
Automatically reject all presence subscription requests.
Process presence subscription requests manually. The mode can be set using the
Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.
Try this in code first to add user in roster as well as requesting the users permission.
roster = Roster.getInstanceFor(connection);
if (!roster.isLoaded())
try {
roster.reloadAndWait();
} catch (SmackException.NotLoggedInException e) {
Log.i(TAG, "NotLoggedInException");
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
Log.i(TAG, "NotConnectedException");
e.printStackTrace();
}
roster.createEntry(jID, name, null);
On other user side/ in your code after login from one user:
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
To make user status online:
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
roster = Roster.getInstanceFor(connection);
Related
I can creating and joining MUC rooms. But user disconnects from the Openfire server, he is removed from the group on the server side. How can i similar to what Whatsapp does, i.e. even if the user goes offline, he is still part of the MUC room (which is configured to be persistent on the server side) and will receive messages from other occupants.
When inviting an user, you have to grant him Membership:
MultiUserChat muc = multiUserChatManager.getMultiUserChat("foo#conference.myserver");
muc.invite("jhondoe#myserver","Join this groupchat!");
then you can grant him voice and you must grantMembership (or Ownership or Moderation as you like/need):
muc.grantVoice("jhondoe#myserver");
muc.grantMembership("jhondoe#myserver");
finally you have to integrate a list like that with your client:
public List<String> retriveAllAffialiateOfMuc(MultiUserChat muc) throws NoResponseException, XMPPErrorException, NotConnectedException
{
List<Affiliate> affiliatesMembers = new ArrayList<Affiliate>();
if (muc.getAdmins() != null)
{
affiliatesMembers.addAll( muc.getAdmins() );
}
if ( muc.getMembers() != null)
{
affiliatesMembers.addAll( muc.getMembers() );
}
if ( muc.getOwners() != null )
{
affiliatesMembers.addAll( muc.getOwners() );
}
if (affiliatesMembers.size() == 0)
{
System.out.println("Error: looking for a non existant room");
return new ArrayList<String>(0);
}
List<String> affiliateMembersNames = new ArrayList<String>(affiliatesMembers.size());
for (Affiliate affiliate : affiliatesMembers)
{
affiliateMembersNames.add(affiliate.getJid().toString());
}
return affiliateMembersNames;
}
So you'll have a list of all users affiliate to the room.
You can use this list in some callback to make a list of "all members" like in WhatsApp.
Look at the end of this page:
https://www.igniterealtime.org/builds/smack/dailybuilds/documentation/extensions/muc.html
(dont' forget to vote!)
I am working on an android chat application in which i am using smack library for instant messaging everything is working fine but the huge problem is in subscription.
How to send subscription notification to another user.
Here is my code for sending subscription request:
public void run() {
/*runOnUiThread(new Runnable() {
public void run() {*/
Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
try {
if(!_userName.contains("#"))
_userName=_userName+"#www.naijapings.net";
/*Presence presence = (Presence) packet;
Presence presence_request = new Presence(Presence.Type.subscribed);
presence_request.setTo(presence.getFrom());
presence_request.setType(Presence.Type.subscribed);
presence_request.setFrom("current_logged_in_user");
XMPPSmackConnection.getInstance().connection.sendPacket(presence_request);
roster.createEntry(presence.getFrom(), null, null);*/
/*Presence response = new Presence(Presence.Type.subscribe);
response.setTo(_userName);
XMPPSmackConnection.getInstance().connection.sendPacket(response);*/
//PROCESS TWO WAY COMMUNICATION
Presence response1 = new Presence(Presence.Type.subscribe);
response1.setTo(_userName);
XMPPSmackConnection.getInstance().connection.sendPacket(response1);
//////////////////////////////////////////////////////
//roster.createEntry(_userName, null, null);
/*roster.createEntry(_userName, _nickName, Friends);*/
//roster.createEntry(_tempUserName, null, null);
//roster.createEntry(_userName, _nickName, my_friends[]);
//addBuddy(_userName, _nickName, Friends);
} catch (Exception e) {
_progDailog.dismiss();
e.printStackTrace();
}
_progDailog.dismiss();
successAlert("Buddy requested successfully");
/*}
});*/
The above code only create roster entry to other user's roster list but not showing subscription notification.
Here is code for accepting subscription request:
runOnUiThread(new Runnable() {
public void run() {
try {
Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
///roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
//Presence presence = roster.getPresence(_buddyUserName);
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
//Presence subscribed = new Presence(Presence.Type.subscribed);
Presence response = new Presence(Presence.Type.subscribed);
response.setTo(_buddyUserName);
XMPPSmackConnection.getInstance().connection.sendPacket(response);
/*response = new Presence(Presence.Type.subscribed);
response.setTo(_buddyUserName);
XMPPSmackConnection.getInstance().connection.sendPacket(response);*/
roster.createEntry(_buddyUserName, _nickNameEditText.getText().toString(), Friends);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Please suggest the exact scenario.
Okay, I toiled hard at this for a couple of days and finally got things working. I have implemented it with a manual subscription mode (ie. user needs to accept another user's request manually).
The server keeps pushing subscribe request to the user (upon re-login) if the user hasn't sent a subscribed or unsubscribed back. So what you can do is save the incoming subscribe requests locally in a list and display that as a "friend request list" for manual accept/reject. If your application gets restarted (and hence re-connects to server), the server will push subscribe requests again.
This is how it works:
User1 sends subscribe presence to User2.
Roster entry gets automatically created in User1's roster (but not in User2's roster).
User2 receives subscribe request from User1.
User2 sends back a subscribed presence to User2 (User2 > User1 subscription complete).
User2 checks if User1 is in User2's roster. User1 is not in User2's roster. User2 sends back a subscribe presence to User1.
Roster entry gets automatically created in User2's roster.
User1 receives subscribe presence from User2.
User1 checks if User2 is in User1's roster. User2 is in User1's roster. User1 sends back a subscribed presence to User2 (User2 > User1 subscription complete).
final Presence newPresence = (Presence) packet;
final Presence.Type presenceType = newPresence.getType();
final String fromId = newPresence.getFrom();
final RosterEntry newEntry = getRoster().getEntry(fromId);
if (presenceType == Presence.Type.subscribe)
{
//from new user
if (newEntry == null)
{
//save request locally for later accept/reject
//later accept will send back a subscribe & subscribed presence to user with fromId
//or accept immediately by sending back subscribe and unsubscribed right now
}
//from a user that previously accepted your request
else
{
//send back subscribed presence to user with fromId
}
}
In order to receive subscription requests, you must:
1) Send presence:
<presence/>
2) Retrieve roster:
<iq type='get' id='roster1'>
<query xmlns='jabber:iq:roster'/>
</iq>
First-time client writers are often surprised by the second one.
you can not send subscription like that, do it this way:
Presence subscription = new Presence(
Presence.Type.subscribe);
subscription.setTo(CurrentUser+"#reza-hp");
subscription.setPriority(24);
newp.setMode(Presence.Mode.available);
connection.sendPacket(subscription);
In my chat application. I am using smack library , with the help of Subscription Management I have done the part of adding a friend to the Roster of a particular person.
Now I want that when some person denies the friend request, I send a UNSUBSCRIBE PACKET to the other user for the same, the friend is not deleted from the roster of the other user. It simply shows NONE subscription.
CODE:
Presence unsubscribe = new Presence(Presence.Type.unsubscribe);
unsubscribe.setTo(ABC#ABC.COM);
connection.sendPacket(unsubscribe);
How can I delete the user from the Roster of the friend. I can do it from openfire portal but don't know how to do it from code.
From the Smack forum, this code might work:
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item("ABC#ABC.COM", null);
item.setItemType(RosterPacket.ItemType.REMOVE);
packet.addRosterItem(item);
connection.sendPacket(packet);
This code worked for me
if(selectedRoster != null) {
Presence presence = new Presence(Presence.Type.unsubscribe);
presence.setTo(selectedRoster.getUser());
presence.setStatus("Offline");
presence.setShow("unavailable");
ConnectionController.GetInstance(this).getXMPPConnection().sendPacket(presence);
try {
roster.removeEntry(selectedRoster);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am new to the use of smack library and making one chatting application. I have made upto much extent and at this step i want to ask two questions.
when i add a friend the friend got added in my list but there is not any notification sent to the FRIEND whom i have added, How to achieve the same. I have added the code below.
The second thing i want to ask is that how can I check whether the user which I am going to add is a part or member of the app or not ( mean it is on the server or not). So that the user who is not registered to the app should not be added in the friends list.
here is the code
public static boolean addFriend(String jid) {
String nickname = null;
nickname = StringUtils.parseBareAddress(jid);
RosterEntry entry4 = roster.getEntry("samsad");
if (!roster.contains(jid)) {
try {
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo(jid);
connection.sendPacket(subscribe);
roster.createEntry(jid, nickname, null);
// Send a roster entry (any) to user2
RosterExchangeManager REM = new RosterExchangeManager(connection);
REM.send(entry4, jid);
return true;
} catch (XMPPException e) {
System.err.println("Error in adding friend");
return false;
}
} else {
return false;
}
}
Roster Exchange manager running in the service in background
/**Remotr Exchange Manager*/
RosterExchangeManager rem = new RosterExchangeManager(connection);
// Create a RosterExchangeListener that will iterate over the received roster entries
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
public void entriesReceived(String from, Iterator remoteRosterEntries) {
notification("Receive==4");
while (remoteRosterEntries.hasNext()) {
try {
// Get the received entry
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) remoteRosterEntries.next();
// Display the remote entry on the console
System.out.println(remoteRosterEntry);
// Add the entry to the user2's roster
roster.createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
notification("Receive==1");
}
catch (XMPPException e) {
e.printStackTrace();
}
}
}
};
rem.addRosterListener(rosterExchangeListener);
}
else{
showToast("Connection lost-",0);
}
}
1, The problem is you must register a PacketListener for Presence.Type.subscribe before you connect to server. All the process of add and accept friend i answered in here
2, You can use UserSearch class to search for a specific user and if user is not found on server then you can assume that user is not registered on server.
I want to register as new users to the xmpp server (Openfire) through the android client and all the new users must be communicated. I create the users, but I'm not sure how to add these to the roster entries to start to talk. I dont know if its better to create a group and add all contacts, but I neither know how to do it.
try {
con.connect();
AccountManager am = con.getAccountManager();
am.createAccount(USERNAME, PASSWORD);
Log.i(TAG,"Creation complete");
}
catch (XMPPException e) {
Log.e(TAG,"Error at user creation "+e);
return false;
}
Can anyone please suggest me how to attain this... Any help will be grately appreciated...!!!
You add contacts to your roster by sending presence packets. See section "Adding Entries to the Roster" on http://www.igniterealtime.org/builds/smack/docs/latest/documentation/roster.html
And then you can add them to a Roster group: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/RosterGroup.html