powered by Jive Software

Example request for how to handle subscribe requests


#1

Hi,

What I’m trying to do is listen for subscribe requests
According to the javadoc I need to setup a listener for presence packets,
I’m not sure how to do this and all the examples I’ve managed to find(I may not of found the right
combination of search terms yet) seem to be for previous versions so look to be doing something
else entirely

The closest I’ve come to finding examples of how to do this is the following



These are 9-14 years old and api looks like it has changed considerably since then

So if someone could point me to a example for the current version or could provide a example I would
really appreciate it

Best Regards
pheonixfire


#2

Hi, I think I’ve worked out how to do this after a lot of back tracking.

I needed to create a class for the listener to to use to start with

import java.util.Optional;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.SubscribeListener;
import org.jxmpp.jid.Jid;

public class SubscribePacketListener implements SubscribeListener
{
    SubscribeAnswer returnValue;
    @Override
    public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest)
    {
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                System.out.println("Subscribe Request Received");
                Alert alert = new Alert (AlertType.CONFIRMATION);
                alert.setTitle("Subscription Request");
                alert.setHeaderText("Subscription Request from " + from.toString());
                alert.setContentText(from.toString() + " want to join buddy list");
                //SubscribeAnswer returnValue;
                
                
                Optional<ButtonType> result = alert.showAndWait();
                if (result.get() == ButtonType.OK)
                {
                    returnValue =  SubscribeAnswer.ApproveAndAlsoRequestIfRequired;
                    //SubscribePacketListener.this.returnValue = SubscribeAnswer.ApproveAndAlsoRequestIfRequired;
                    //messageStickB.SubscribePacketListener.this.returnValue = SubscribeAnswer.ApproveAndAlsoRequestIfRequired;
                }
                else
                {
                    returnValue = SubscribeAnswer.Deny;
                    //SubscribePacketListener.this.returnValue = SubscribeAnswer.Deny;
                    //messageStickB.SubscribePacketListener.this.returnValue = SubscribeAnswer.Deny;
                }
            }
        });
        return returnValue;
    }
}

plus I needed to use that class with the listener on roster`

public void subscriptionRequestListener ()
    {
        System.out.println("Subscribe Request Received");
        roster = Roster.getInstanceFor(connection);
        roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                SubscribePacketListener mySubscribeListener = new SubscribePacketListener();
                roster.addSubscribeListener(mySubscribeListener);
            }
        });
    }

Is there anything I missed?
Please be kind if your going to reply and remember no one was willing to offer help or
advice to me before on this issue, I’ve had to work this out the hard way, since I didn’t
know what I was looking for most of the time

Best Regards
pheonixfire


closed #3

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.