【问题标题】:How to Add and Subscribe a jabber entry to my XMPP Account?如何向我的 XMPP 帐户添加和订阅 jabber 条目?
【发布时间】:2012-10-15 06:27:03
【问题描述】:

我可以使用此代码将Entry 添加到 Xmpp 帐户。我无法订阅“两者”,而不是订阅none

roster.createEntry("abc@xyz.com", "abc", null);

当我订阅此帐户的条目时,如何添加存在 type=both 的条目。我想知道 xmpp publish-subscribe 是否有功能?

  1. 如何获取入站状态通知?
  2. 如何发送出站状态通知?

编辑:

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");
    }
}

我正在添加这样的条目,但我仍然得到 Presence Type = none..

【问题讨论】:

    标签: android xmpp


    【解决方案1】:

    这是我在应用程序中添加另一个朋友的方法。

    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);
                }
            }
    

    只使用必要的部分

    Presence response = new Presence.Type.subscribed);
    response.setTo(address);
    sendPacket(response);
    
    Roster roster = mConnection.getRoster();
    roster.createEntry(address, name, groups);
    

    为了收听传入的请求,请将addPacketListener 注册到您的连接

        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();
    

    正确的顺序:

    1. User1 向 user2 发送订阅。
    2. User2 将 Subscribe 和 Subsribed 发送回 user1。
    3. 用户 1 向用户 2 发送订阅。

    另一个SO question你可以检查

    【讨论】:

    • 我正在使用这个代码 roster.createEntry(address, name, groups);我仍然在输入类型 = 无。创建条目完成后如何使用上述存在代码..
    • 我认为顺序不是问题。因为当您添加联系人时,它等同于在您的名册中创建条目并发送订阅请求。
    • 我用新添加的代码更新了我的代码如何添加条目,在这些更改之后,我也得到了存在类型 = 无。
    • 受邀者是否接受了您的请求?来Android People或聊天App development room
    • 现在可以从应用程序向联系人发送邀请,如果他们接受我正在正确获取状态。现在我无法接受我这样做了,但没有成功。存在响应 = new Presence(Type.subscribed); response.setTo("sh@xyz.com"); m_connection.sendPacket(响应);
    猜你喜欢
    • 2014-11-20
    • 2023-03-26
    • 2021-06-17
    • 2019-11-24
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多