我正在开发一个Android聊天应用程序,我在其中使用smack库进行即时消息,一切正常,但是订阅的问题很大。
如何向其他用户发送订阅通知。
以下是我发送订阅请求的代码:
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");
/*}
});*/
上述代码仅为其他用户的名单列表创建名单条目,但未显示订阅通知。
以下是接受订阅请求的代码:
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();
}
}
});
请提供具体方案。
答案 0 :(得分:1)
好的,我辛苦劳作了几天,终于让事情顺利进行了。我已经用手动订阅模式实现了它(即用户需要手动接受另一个用户的请求)。
如果用户尚未发送订阅或取消订阅,服务器会继续向用户推送订阅请求(重新登录时)。因此,您可以做的是将传入的订阅请求本地保存在列表中,并将其显示为手动接受/拒绝的“朋友请求列表”。如果您的应用程序重新启动(因此重新连接到服务器),服务器将再次推送订阅请求。
这是它的工作原理:
User1检查User2是否在User1的名单中。 User2位于User1的名单中。 User1将订阅的状态发送回User2(User2> User1订阅完成)。
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
}
}
答案 1 :(得分:0)
要接收订阅请求,您必须:
1)发送在线状态:
<presence/>
2)检索名单:
<iq type='get' id='roster1'>
<query xmlns='jabber:iq:roster'/>
</iq>
第一次客户编写者经常对第二次写作者感到惊讶。
答案 2 :(得分:0)
你不能发送这样的订阅,这样做:
Presence subscription = new Presence(
Presence.Type.subscribe);
subscription.setTo(CurrentUser+"@reza-hp");
subscription.setPriority(24);
newp.setMode(Presence.Mode.available);
connection.sendPacket(subscription);