Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add and Subscribe a jabber entry to my XMPP Account?

Tags:

android

xmpp

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("[email protected]", "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 ?

  1. How to get Inbound presence notifications ?
  2. How to send Outbound presence notifications ?

EDIT :

public void Addcontact() {    
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
    Roster roster = m_connection.getRoster();

    if(!roster.contains("[email protected]")) {        try {           
            roster.createEntry("[email protected]", "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..

like image 404
RajaReddy PolamReddy Avatar asked Oct 25 '12 05:10

RajaReddy PolamReddy


1 Answers

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:

  1. User1 send Subscribe to user2.
  2. User2 send Subscribe and Subsribed back to user1.
  3. User1 send Subsribed to user2.

Another SO question you can check

like image 135
Trung Nguyen Avatar answered Oct 21 '22 10:10

Trung Nguyen