2

I am creating one chatting application which will work on both iOS and Android platform. Sometimes 'both' subscription is not received at both end. Can anyone tell me what can be the possible issue?

===================== For iOS =====================

Sending request,

XMPPJID *XMPPJIDObj=[XMPPJID jidWithString:aStrOtherJabberId];
[appDelegateObj.xmppRoster addUser:XMPPJIDObj withNickname:nil]; 

Accepting request,

[appDelegateObj.xmppRoster acceptPresenceSubscriptionRequestFrom:aReceiverJID andAddToRoster:TRUE];

Removing user,

[appDelegateObj.xmppRoster removeUser:[XMPPJID jidWithString:aPresenceObj.userJabberID]];

===================== For Android =====================

Sending request,

Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
myApp.getXmppConnection().getRoster().createEntry(visitorJabberId, visitorUserName, null);

Accepting request,

final Presence presence1 = new Presence(Type.subscribed);
presence1.setFrom(myApp.getUserJabberId());
presence1.setType(Type.subscribed);
presence1.setTo(visitorJabberId);
myApp.getXmppConnection().sendPacket(presence1);
myApp.getXmppConnection().getRoster().createEntry(visitorJabberId, visitorUserName, null);

Removing user,

final RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET); 
final RosterPacket.Item item = new RosterPacket.Item(visitorJabberId, null);
item.setItemType(RosterPacket.ItemType.remove);
rosterPacket.addRosterItem(item);
myApp.getXmppConnection().sendPacket(rosterPacket);
Leena
  • 2,678
  • 1
  • 30
  • 43
cjd
  • 549
  • 3
  • 11

2 Answers2

1

Hi Leena for the iOS we have used save thing but I think you have forgot some thing. The actual flow is call add user method of roster class then call subscribe method with subscription value YES and finally send presence tag with subscribe type to the serve. Following are the code here I used XMPPSharedPreference singleton class rather than appdelegate. Hope this will work for you...

XMPPJID *newBuddy = [XMPPJID jidWithString:JIDString]; [[XMPPSharedPreference sharedPreferences].xmppRoster addUser:newBuddy withNickname:@"nicknameValue"];

[[XMPPSharedPreference sharedPreferences].xmppRoster acceptPresenceSubscriptionRequestFrom:newBuddy andAddToRoster:YES];

NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"to" stringValue:JIDString];
[presence addAttributeWithName:@"type" stringValue:@"subscribe"];

[[self xmppStream] sendElement:presence];
Bhaskar
  • 51
  • 5
0

When you add a user to your roster you have to make sure you also subscribe to the friend's presence. That completes the cycle.

So, for iOS for example, you're adding a friend to roster like this:

[appDelegateObj.xmppRoster addUser:XMPPJIDObj withNickname:nil];

But you need to do use this instead:

- (void)addUser:(XMPPJID *)jid withNickname:(NSString *)optionalName groups:(NSArray *)groups subscribeToPresence:(BOOL)subscribe

and make sure you set subscribe to YES

Or, you could keep the code you have but manually subscribe to the user's presence by doing this:

[appDelegateObj.xmppRoster subscribePresenceToUser:XMPPJIDObj]

Let me know how that works out for you.

idancali
  • 847
  • 5
  • 10
  • That method is called internally in my code. I am sure that presence is sent every time. The problem is regarding 'both' subscription we receive is not received in some cases. – cjd Nov 07 '14 at 05:26
  • Eventhough the presence is sent, if you do not subscribe to the friend's presence I do believe the cycle is not finished. The XMPP spec is pretty clear about this. Read section 7.1. "Syntax and Semantics" (http://xmpp.org/rfcs/rfc3921.html): _"both" -- both the user and the contact have subscriptions to each other's presence information_ So you **must** tell the server that you want to subscribe to the friend's presence otherwise the friendship won't be flipped to _both_ – idancali Nov 07 '14 at 09:27