Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ADD FRIEND using Smack

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.

  1. 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.

  2. 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);
        }
    }
like image 453
Gaurav Arora Avatar asked Nov 13 '22 20:11

Gaurav Arora


1 Answers

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.

like image 116
Trung Nguyen Avatar answered Nov 15 '22 10:11

Trung Nguyen