本文介绍了安卓嫌库订阅(不显示inbond或outbond通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我使用咂嘴库即时通讯一切正常,但巨大的问题是在订阅的机器人聊天应用程序。

I am working on an android chat application in which i am using smack library for instant messaging everything is working fine but the huge problem is in subscription.

如何发送订阅通知给其他用户。

How to send subscription notification to another user.

这是我的code发送订阅请求:

public void run() {
        /*runOnUiThread(new Runnable() {
            public void run() {*/
                Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
                roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
                try {
                    if(!_userName.contains("@"))
                        _userName=_userName+"@www.naijapings.net";

                    /*Presence presence = (Presence) packet;
                    Presence presence_request = new Presence(Presence.Type.subscribed);
                    presence_request.setTo(presence.getFrom());
                    presence_request.setType(Presence.Type.subscribed);
                    presence_request.setFrom("current_logged_in_user");
                    XMPPSmackConnection.getInstance().connection.sendPacket(presence_request);
                    roster.createEntry(presence.getFrom(), null, null);*/

                    /*Presence response = new Presence(Presence.Type.subscribe);
                    response.setTo(_userName);
                    XMPPSmackConnection.getInstance().connection.sendPacket(response);*/

                    //PROCESS TWO WAY COMMUNICATION
                    Presence response1 = new Presence(Presence.Type.subscribe);
                    response1.setTo(_userName);
                    XMPPSmackConnection.getInstance().connection.sendPacket(response1);
                    //////////////////////////////////////////////////////

                    //roster.createEntry(_userName, null, null);

                    /*roster.createEntry(_userName, _nickName, Friends);*/
                    //roster.createEntry(_tempUserName, null, null);
                    //roster.createEntry(_userName, _nickName, my_friends[]);
                    //addBuddy(_userName, _nickName, Friends);
                } catch (Exception e) {
                    _progDailog.dismiss();
                    e.printStackTrace();
                }
                _progDailog.dismiss();
                successAlert("Buddy requested successfully");
            /*}
        });*/

以上code只能建立名册进入其他用户的阵容名单,但没有表现出认购通知。

The above code only create roster entry to other user's roster list but not showing subscription notification.

下面是code接受预订请求:

runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
                        ///roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
                        //Presence presence = roster.getPresence(_buddyUserName);
                        roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
                        //Presence subscribed = new Presence(Presence.Type.subscribed);

                        Presence response = new Presence(Presence.Type.subscribed);
                        response.setTo(_buddyUserName);
                        XMPPSmackConnection.getInstance().connection.sendPacket(response);

                        /*response = new Presence(Presence.Type.subscribed);
                        response.setTo(_buddyUserName);
                        XMPPSmackConnection.getInstance().connection.sendPacket(response);*/

                        roster.createEntry(_buddyUserName, _nickNameEditText.getText().toString(), Friends);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

请建议确切的情况。

推荐答案

好了,我辛苦劳碌在这几天,最后得到的东西的工作。我已经实现了它与手动订阅模式(即用户需要接受其他用户的请求,以手动)。

Okay, I toiled hard at this for a couple of days and finally got things working. I have implemented it with a manual subscription mode (ie. user needs to accept another user's request manually).

服务器保留推订阅请求给用户(在重新登录),如果用户未发送订阅或退订回来。那么,你能做的就是在本地保存收到的认购请求在列表中,并显示为好友请求列表手工接受/拒绝。如果您的应用程序被重新启动(并因此重新连接到服务器),服务器将再次推订阅请求。

The server keeps pushing subscribe request to the user (upon re-login) if the user hasn't sent a subscribed or unsubscribed back. So what you can do is save the incoming subscribe requests locally in a list and display that as a "friend request list" for manual accept/reject. If your application gets restarted (and hence re-connects to server), the server will push subscribe requests again.

这是如何工作的:

  • 在用户1发送订阅presence到用户2。
  • 进入名册就会自动在用户1的花名册创建(而不是在用户2的名册)。
  • 用户2接收来自用户1订阅的请求。
  • 在用户2发回订阅presence到用户2(用户2>用户1认购完成)。
  • 在用户2检查,如果用户1在用户2的花名册。用户1是不是在用户2的花名册。用户2发回一个认购presence为user1。
  • 进入名册就会自动在用户2的花名册创建。
  • 在用户1接收订阅presence从用户2。
  • 用户1检查,如果用户2是在用户1的花名册。用户2在用户1的花名册。用户1发回一个订阅presence到用户2(用户2>用户1认购完成)。

  • User1 sends subscribe presence to User2.
  • Roster entry gets automatically created in User1's roster (but not in User2's roster).
  • User2 receives subscribe request from User1.
  • User2 sends back a subscribed presence to User2 (User2 > User1 subscription complete).
  • User2 checks if User1 is in User2's roster. User1 is not in User2's roster. User2 sends back a subscribe presence to User1.
  • Roster entry gets automatically created in User2's roster.
  • User1 receives subscribe presence from User2.
  • User1 checks if User2 is in User1's roster. User2 is in User1's roster. User1 sends back a subscribed presence to User2 (User2 > User1 subscription complete).

        final Presence newPresence = (Presence) packet;
        final Presence.Type presenceType = newPresence.getType();
        final String fromId = newPresence.getFrom();
        final RosterEntry newEntry = getRoster().getEntry(fromId);

        if (presenceType == Presence.Type.subscribe)
        {
            //from new user
            if (newEntry == null)
            {
                //save request locally for later accept/reject
                //later accept will send back a subscribe & subscribed presence to user with fromId
                //or accept immediately by sending back subscribe and unsubscribed right now
            }
            //from a user that previously accepted your request
            else
            {
                //send back subscribed presence to user with fromId
            }
        }

这篇关于安卓嫌库订阅(不显示inbond或outbond通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:12