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

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.
Here is my code for sending subscription request:
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");
/*}
});*/
The above code only create roster entry to other user's roster list but not showing subscription notification.
Here is code for accepting subscription request:
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();
}
}
});
Please suggest the exact scenario.

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

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.

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

Related

How can a chat application user once online again get the messages that were sent to them when they were offline

I'm developing an android chat app, using Node Js and redis to stock messages and user information. I'm using socket io for communication, and Room to store message in local database. When the user is offline, I want them to receive their messages once online again. My problem is, that when user A is offline, and user B send him many messages (let's say for instance 5 messages ), when user A is online again, he only receives the first message, and the last message 4 times. Here is what I'm doing, once the user receives a message, I update the message status in Redis from "Sent" to "Delivered". In the case when the user is offline, I stock their messages in Redis with the status of message "Sent", and once online again, I check their messages received for example from user B, if their status is "Sent", I deliver it to the user, and then it will be updted to "Delivered", as shown in the code below:
//On this event, we update the socket ID of the sender in Redis so they can
receive private messages from their contacts
socket.on('sender', (sender, destinat) =>{
tempId = socket.id;
senderId = sender;
users[sender] = sender;
users [destinat] = destinat;
//We also update the user status: online
client.hset(senderId, 'lastSeen', 'Now', function(reply){
console.log( senderId + reply);
});
//Stocking to the user socket id
client.hset(users[sender], 'tempId', tempId, function(){
console.log("Welcome " + sender);
console.log("Welcome " + tempId);
});
//Getting all the messages of the sender from users
//If the sender has any messages that hasn't received yet, they'll be sent
here
//the id of each message is compsed of two parts: the phone number of the
receiver, and the id of the message itself
(receiverPhoneNumber:idMessage)
client.keys(users [sender] + ':*', function(err, results) {
results.forEach(function(key) {
client.hgetall(key, function(err, reply){
if(err)
console.log(err);
else if(reply){
//Compare the message status: if not sent, deliver it to receiver once online
if('Sent'.localeCompare(reply.status) == 0 && users
[destinat].localeCompare(reply.fromUser) == 0) {
io.to(tempId).emit('message', reply);
}
}
});
});
});
});
After receiving messages from the server, I use Async to store them in Room Database and then display them to the user, as shown in the following code
And here is the AsyncTask Class:
class AddMessage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... voids) {
//Creating a user account
m = new Message();
m.setContent( message );
m.setTime( time );
m.setUrl( url );
m.setStatus( status );
m.setFromUser( fromUser );
m.setToUser( toUser );
m.setUsername( receiver.getUsername() );
//adding to database
DatabaseClient.getInstance(getContext()).getAppDatabase()
.messageDao()
.insert(m);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText( getContext(), "Added!", Toast.LENGTH_SHORT ).show();
}
}
I've checked that messages are received from the server to the android app correctly (by re-sending the messages again to the server once delivered to the app). I believe the problem has something to do with AsyncTask, but I just can't figure it out, any help is greatly appreciated, thank you so much.
//When receving a message
socket.on("message", new Emitter.Listener() {
#Override
public void call(final Object... args) {
if(getActivity() != null){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event
idMessage = data.getString( "idMessage" );
message = data.getString("message");
fromUser = data.getString( "fromUser" );
toUser = data.getString( "toUser" );
time = data.getString( "time" );
status = data.getString( "status" );
url = data.getString( "url" );
//Here we call asyncTask to Add it to Database
addMessage = new AddMessage();
addMessage.execute( );
//We emit this event to update the status of
the message to delivered
socket.emit( "sent", idMessage, userID );
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
});
I solved the problem by switching to RxJava instead of AsyncTask. The problem had something to do with AsyncTask as it sometimes influnces the chain of data, which is not the case with RxJava, as mentioned in this link : "Another issue with AsyncTasks is if you have more than one running at once. You have no guarantee what order they’ll complete in, resulting in complex logic to check when all the tasks have completed. Even worse is the assumption that one will finish before the other, until you hit an edge case that makes the first call slower, which makes them complete in the wrong order and undesired results."

Implement receiver status(read or typing) in messaging app using smack - Android

I am creating an instant messaging app in android using smack library and openfire as a server but i cannot implement the feature of the person with whom the current user is talking to. i.e. like when user read the message or when he starts typing.
Is there any way of achieving this using smack or other ?
For knowing which user is current you must implement your own in your logic. You must use Roster (contact list in xmpp servers) to get contacts of current user and save them in database or somewhere. Then create an activity to show contacts in a list. Each contact has a unique jid that can be distinguished from others with it. So with click on each contact, send it's object(include jid) to chat-activity. In chat-activity you must get previous messages from database or MAM(archived messages in server) and you can send a message to current contact(set contact jid as To).
To achieving delivery of message you must use this link. you can set request of it with this code:
Message message = … //make your stanza
DeliveryReceiptRequest.addTo(message); //add delivery request to message
connection.sendStanza(message); //send message
then you can be notified of delivery with this code:
private void setDelRecListener() {
DeliveryReceiptManager d = DeliveryReceiptManager.getInstanceFor(connection);
d.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
Msg msg = F.getMsgBySid(receiptId);
if (msg == null)
return;
Boolean isUpdated = F.setMsgDelivered(msg);
Log.i("m/serv/UpdateDelivery", "for: " + receiptId + (isUpdated ? " Founded&Updated" : " NotFounded"));
if (isUpdated) {
BCTool.notifyPMDelivered(msg.id, msg.conv.frnd.getBareJid());
}
}
});
}
Keep in mind that every stanza has a sid(stanza id) and you must save each corresponding sid to message model in database when send is successful. This way you can detect which message delivery you got.
- For sending chat states like composing you can use this method:
public void sendChatState(String _jid, ChatState chatState) {
try {
Message msg = new Message();
msg.addExtension(new ChatStateExtension(chatState));
msg.setTo(JidCreate.bareFrom(_jid));
msg.setType(Message.Type.chat);
connection.sendStanza(msg);
Log.e("m/service", "ChatStateSent");
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) {
Log.e("m/service", "ChatState Not Sent: " + e.getMessage());
e.printStackTrace();
}
}
You must set a timer to prevent send composing in next 5Sec and reset timer when a character typed.
Consider reading this: ChatStateNotifications

How to add users to a roster in xmpp?

I have successfully created a user using the following code:
accountmanager = new org.jivesoftware.smack.AccountManager(connection);
accountmanager.createAccount(fbuserid,fbuserid);
But I am not able to add other users to the logged in user's roster using the following code :
public void createEntry(String user, String name, String[] groups) throws XMPPException {
// Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item(user, name);
if (groups != null) {
for (String group : groups) {
if (group != null) {
item.addGroupName(group);
}
}
}
rosterPacket.addRosterItem(item);
// Wait up to a certain number of seconds for a reply from the server.
PacketCollector collector = connection.createPacketCollector(
new PacketIDFilter(rosterPacket.getPacketID()));
connection.sendPacket(rosterPacket);
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
}
// If the server replied with an error, throw an exception.
else if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
// Create a presence subscription packet and send.
Presence presencePacket = new Presence(Presence.Type.subscribe);
presencePacket.setTo(user);
connection.sendPacket(presencePacket);
}
I am always getting the response as null. Someone please help me to solve this and Thanks in advance
Rosters and presence use a permissions-based model where users must give permission before they are added to someone else's roster. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request.
If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:
Automatically accept all presence subscription requests.
Automatically reject all presence subscription requests.
Process presence subscription requests manually. The mode can be set using the
Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.
Try this in code first to add user in roster as well as requesting the users permission.
roster = Roster.getInstanceFor(connection);
if (!roster.isLoaded())
try {
roster.reloadAndWait();
} catch (SmackException.NotLoggedInException e) {
Log.i(TAG, "NotLoggedInException");
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
Log.i(TAG, "NotConnectedException");
e.printStackTrace();
}
roster.createEntry(jID, name, null);
On other user side/ in your code after login from one user:
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
To make user status online:
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
roster = Roster.getInstanceFor(connection);

typing status / online status via GCM and xmpp

I have finished developing my IM application on android (using xmpp & GCM), and I am using the gcm for buth UpPayload and DownPayloads..
and to notify the user that his partner is online/ofline I send messaage that my xmpp server "understand"
that tell the statuse.
protected void onStart() {
super.onStart();
if(!isOnlineSent)
{SendOnlineStatus("Online");
isOnlineSent=true;}
.
.
.
.}
and the SendOnlineStatus look like:
Intent OnlineMsg = new Intent();
OnlineMsg.putExtra("action", "com.Esmaeel.sodfarim.sodfa01.MESSAGE");
String nowtime = String.valueOf(EsTools.getCurrentTime());
OnlineMsg.putExtra(ConstantsGCM.TYPECLM, ConstantsGCM.ONST);
OnlineMsg.putExtra(ConstantsGCM.STATUS_on_of, Status);
OnlineMsg.putExtra(ConstantsGCM.TO_CLM, "-01");
OnlineMsg.putExtra(ConstantsGCM.FROMCLM, UUID);
OnlineMsg.putExtra(ConstantsGCM.MESSAGE_ID_CLM, regid + nowtime);
OnlineMsg.putExtra(ConstantsGCM.NAME_CLM, "Name");
final Bundle bndl = OnlineMsg.getExtras();
new AsyncTask() {
#Override
protected String doInBackground(Object[] objects) {
if (ggcm == null) {
ggcm = GoogleCloudMessaging.getInstance(context);
}
try {
ggcm.send(PRO_ID + ConstantsGCM.GCM_SERVER, bndl.getString(ConstantsGCM.MESSAGE_ID_CLM), bndl); //// GCM_SERVER="gcm.googleapis.com"
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}.execute(null, null, null);
the server checks the value of
payload.get(ConstantsGCM.TYPECLM);
if the type ONST the server reads the value payload.get(ConstantsGCM.STATUS_on_of);
the valid valuse of payload.get(ConstantsGCM.STATUS_on_of); is "online" OR "offline".
then the server update the user status and sends the new status to all the user's friends.
the same I do with Typing status but the last step I send just to the other side of the active chat.
but I get problems like "user apear online while he is offline, and some times apears typing when he is not.
any help or more effictive Ideas?
I don't know for the typing part, but for the status, maybe you should only send to the friends something like : "you should check the status of this user" then each friends phone will get the real status directly on the server. Hope it helps

Delete a Friend from Roster in Xmpp/Openfire

In my chat application. I am using smack library , with the help of Subscription Management I have done the part of adding a friend to the Roster of a particular person.
Now I want that when some person denies the friend request, I send a UNSUBSCRIBE PACKET to the other user for the same, the friend is not deleted from the roster of the other user. It simply shows NONE subscription.
CODE:
Presence unsubscribe = new Presence(Presence.Type.unsubscribe);
unsubscribe.setTo(ABC#ABC.COM);
connection.sendPacket(unsubscribe);
How can I delete the user from the Roster of the friend. I can do it from openfire portal but don't know how to do it from code.
From the Smack forum, this code might work:
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item("ABC#ABC.COM", null);
item.setItemType(RosterPacket.ItemType.REMOVE);
packet.addRosterItem(item);
connection.sendPacket(packet);
This code worked for me
if(selectedRoster != null) {
Presence presence = new Presence(Presence.Type.unsubscribe);
presence.setTo(selectedRoster.getUser());
presence.setStatus("Offline");
presence.setShow("unavailable");
ConnectionController.GetInstance(this).getXMPPConnection().sendPacket(presence);
try {
roster.removeEntry(selectedRoster);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories

Resources