Block User not working properly

Hi am using smack 4.3.4.

When am blocking a user it work until I re-login.

after relogin messages can recieved again even i see user is blocked.

My code is:

public boolean blockContact(Jid jid) {
        String listName = "block-list";
        // Create the list of PrivacyItem that will allow or
        // deny some privacy aspect
        List<PrivacyItem> privacyItems = new Vector<>();
        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, jid, false, 1);
        item.setFilterIQ(false);
        item.setFilterMessage(false);
        item.setFilterPresenceIn(false);
        item.setFilterPresenceOut(false);
        privacyItems.add(item);
        // Get the privacy manager for the current connection.
        // Create the new list.
        PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(Connection.getInstance().getXmpptcpConnection());
        try {
            privacyManager.updatePrivacyList(listName, privacyItems);
            privacyManager.setActiveListName(listName);
            return true;
        } catch (Exception e) {
            Log.e("PRIVACY_ERROR: ", " " + e.toString());
            e.printStackTrace();
        }
        return false;
    }

Please let me know why a user can received message if he blocked and In still in blocked list after relogin.

You seem to be setting the ‘active’ list, instead of the ‘default’ list. The ‘active’ list is ment to be active for the duration of the session only. You can read up about the details in https://xmpp.org/extensions/xep-0016.html

Can you tell me how to set for default list. And after login if go to block contact list am getting contacts list there in the privacy list. with the following code:

PrivacyList plist = null;
        try {
            plist = privacyManager.getPrivacyList("public");
        } catch (NoResponseException e) {
            e.printStackTrace();
        } catch (NotConnectedException e) {
            e.printStackTrace();
        }
        if (plist != null) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {
              if(item.isAllow){
                // print item.getValue(); to get ID
                }
}
}

Use setDefaultListName() instead of setActiveListName() in the code that you provided.

2 Likes

Thank you @Guus it working now.

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