Android smack library subscription(not showing inbond or outbond notifications)

后端 未结 3 1024
Happy的楠姐
Happy的楠姐 2021-01-07 07:52

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.

相关标签:
3条回答
  • 2021-01-07 08:32

    you can not send subscription like that, do it this way:

     Presence subscription = new Presence(
                   Presence.Type.subscribe);
           subscription.setTo(CurrentUser+"@reza-hp");
           subscription.setPriority(24);
           newp.setMode(Presence.Mode.available);
           connection.sendPacket(subscription);
    
    0 讨论(0)
  • 2021-01-07 08:33

    In order to receive subscription requests, you must:

    1) Send presence:

    <presence/>
    

    2) Retrieve roster:

    <iq type='get' id='roster1'>
      <query xmlns='jabber:iq:roster'/>
    </iq>
    

    First-time client writers are often surprised by the second one.

    0 讨论(0)
  • 2021-01-07 08:41

    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.

    This is how it works:

    • 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
                  }
              }
      
    0 讨论(0)
提交回复
热议问题