8

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 ?

  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("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..

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

1 Answers1

12

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

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
  • i am using this code roster.createEntry(address, name, groups); still i am getting type to type = none. how can i use above presence code after create entry completed.. – RajaReddy PolamReddy Oct 25 '12 at 07:32
  • I don't think the order is the problem. Because when you add a contact it's equal with create Entry in your roster and send a subcription request. – Trung Nguyen Oct 25 '12 at 08:29
  • i was updated my code with new added code how am adding entry, after these changes also i am getting presence type = none. – RajaReddy PolamReddy Oct 25 '12 at 09:19
  • Did the invited accepted your request? Come to [Android People](http://chat.stackoverflow.com/rooms/5098/android-people) or Chat [App development room](http://chat.stackoverflow.com/rooms/info/9351/chat-app-development) – Trung Nguyen Oct 25 '12 at 09:25
  • Now am able to send invitation to the contact from the app, if they accept i am getting presence status properly. now i am unable to accept i did like this but no succeed. Presence response = new Presence(Type.subscribed); response.setTo(" sh@xyz.com"); m_connection.sendPacket(response); – RajaReddy PolamReddy Oct 25 '12 at 12:10
  • @RajaReddyPolamReddy I am also getting same problem could you help me assist about smack subscription notification process. Please have a look http://stackoverflow.com/questions/16815531/android-smack-library-subscriptionnot-showing-inbond-or-outbond-notifications – Ricky Khatri May 30 '13 at 05:22
  • Look at the above answer it will solver your problem, i got solution from that only.. – RajaReddy PolamReddy May 30 '13 at 05:39
  • Hi jul, is it possible to add contact without sending and receiving subscription request in xmpp ? – Jayesh Jul 10 '14 at 06:36
  • @Jayesh: I think it's no :). – Trung Nguyen Jul 10 '14 at 06:45
  • @jul: thanks for answer , one more question, can we add incoming subscription request autometic (without UI interaction) in application ? – Jayesh Jul 10 '14 at 07:23
  • @Jayesh i dont understand "add" incomming sub. Do you mean auto send and accept request without UI. If its true, i think it's possible. You can hidden add contact in your roster. – Trung Nguyen Jul 10 '14 at 08:13
  • @jul can tell me that what is this address,mContactListManager,parsePresence(presence);,contact.setPresence> – CoronaPintu Jul 15 '14 at 12:21
  • hey i dont have a method called addpacketlistener .its addpacketinterceptor . add adding packetlistener gives me a stanza object not a packet object op reciving a message. how do i deal with it . – Sagar Nayak Apr 21 '16 at 12:19