xmpp send and receive message in android application - android

I am working on a sms application. I am able to send a message from my application using the function sendMessage("subject" , "to");. I need to know how to receive the message sent from the other end. I need to get that data and display the data in textview in my application. How can i achive that. How can i achive that. Please guide me.
Thanks in aadvance.

this will help you:
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
// Listener for incoming message from any user
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
final Message message = (Message) packet;
if (message.getBody() != null) {
fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
}
}
});
}
}
}, filter);

Related

How to send Push Notification from device when the receiver device is not in foreground state using FCM in android?

I have created a one to one chat using FCM. I have stuck at one point, I want to send push notification when the device is not in foreground state. I am new to this can anyone suggest me how can I proceed.
Thankyou
You can try this bellow code in MyFirebaseMessagingService class:
if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
Log.d("TAGRK", "Message data payload: " + remoteMessage.getData());
message = "" + remoteMessage.getData().get(Config.MESSAGE_KEY);//MESSAGE_KEY is your key for message in your json.(eg. "message")
title = "" + remoteMessage.getData().get(Config.TITLE_KEY);//TITLE_KEY is your key for title in your json..(eg. "title")
//send pushnoification method
} else if (remoteMessage.getNotification() != null) {
Log.d("TAGRK", "Message Notification Body: " + remoteMessage.getNotification().getBody());
message = "" + remoteMessage.getNotification().getBody();
title = "" + remoteMessage.getNotification().getTitle();
//send pushnoification method
}

How to get message from GCM message body in android?

I have an app in which server sends some push notification using GCM server, The implementation is done on both side but i am facing problem is that i can't get notification from message body, it is always showing me "null". But i can see message in messag body using debug.
String message = bundle.getString("message");
Log.e(TAG, "onMessageReceived::" + message);
Log.e(TAG, "from::" + from);
and message body is :-
Bundle[{google.sent_time=1497866966996, google.message_id=0:1497866967011288%466cbbd8466cbbd8, notification=Bundle[{priority=high, body=Your wallet has been credited with 1.000 point(s)., title=Wallet credited}], collapse_key=com.s.baty}]
Try ,
Bundle notificationData = bundle.getBundle("notification");
String body = notificationData.getString("body");
It received correct data only
String bodymessage = bundle.getString("body");
Problem is print message after converting Sting
Log.e(TAG, "onMessageReceived::" + message.toString());
You can receive message in onMessage() function of GCMIntentService implementing class. I have created a function for getting proper message and key.
#Override
protected void onMessage(Context context, Intent intent) {
dumpIntent(intent);
}
public void dumpIntent(Intent i) {
Bundle bundle = i.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
Log.e("Intent Data", "[" + key + "=" + bundle.get(key) + "]");
}
}
}
After that you will easily set message with NotificationManager.

How to get time stamp messages with sender time

I am developing a chat application. In that if i am online, sending and receiving messages with current date and time is fine. But if i am offline again coming to online after 2 hours, i am receiving messages with current time itself, but i want to receive the message time with sender time. help me out of this.
Thanks in advance.
send part
if (connection != null) {
connection.sendPacket(msg);
data = new Msg();
data.setMessage(text);
data.setSenderName(userFrom);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(true);
MessageListAdapter.messagesItems.add(data);
notifyMyAdapter();
}
receive part
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
#Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
String from = to + "#localhost/Smack";
if (from.equalsIgnoreCase(message.getFrom())) {
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Msg data = new Msg();
String subject = message.getSubject();
if (subject == null) {
String text = message.getBody();
data.setMessage(text);
data.setSenderName(NAME);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
notifyMyAdapter();
}
});
}
}
}
}, filter);
}
I think all you have to do is while publishing the message data.setCreatedAtTime(System.currentTimeMillis();
the good thing about System.currentTimeMillis(); is that it is in epoch and millisecondsformat and would remain constant over all timeZones.
now on the reciever side you can convert the milliseconds into which ever format you desire

On Affliation change using MucAdmin() gives forbidden(403) Error in asmack android

First I change owner affliation to administrator by revokeOwnership() nad than I try revokeAdmin() but it's not working,than I try following method for change admin affliation to none
when I call following method at time of leave group gives forbidden(403) Error,How to solve this Error and set admin affliation none in muc.
private void changeAffiliationByAdmin(String jid, String affiliation,String grpnm) throws XMPPException
{
MUCAdmin iq = new MUCAdmin();
iq.setTo(grpnm + "#conference." + Utils.SERVER_NAME);
iq.setType(IQ.Type.SET);
MUCAdmin.Item item = new MUCAdmin.Item(affiliation,null);
item.setJid(jid + "#" + Utils.SERVER_NAME);
iq.addItem(item);
// Wait for a response packet back from the server.
filter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(filter);
// Send the change request to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
MUCAdmin answer = (MUCAdmin) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
}
else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
}
if i user this code for change owner affliation to none than it's not woking,
MUCOwner iq = new MUCOwner();
iq.setTo(grpnm + "#conference." + Utils.SERVER_NAME);
iq.setType(IQ.Type.SET);
System.out.println("before affliation changed" + jid + grpnm);
// Set the new affiliation.
MUCOwner.Item item = new MUCOwner.Item(affiliation,null);
item.setJid(jid + "#" + Utils.SERVER_NAME);
iq.addItem(item);
System.out.println("after affliation changed" + jid + grpnm);
// Wait for a response packet back from the server.
filter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(filter);
// Send the change request to the server.
System.out.println("call IQ packet" + jid + grpnm);
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
}
else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}

Get Message body at login xmpp in android

How to get Message body at login xmpp in android?
I want to get offline message at login time in xmpp.
Any one have idea about. Any help would appreciated.
Thanks in advance.
This depends on your server. XEP-0160 recommends that the server store offline messages, and deliver them whenever the recipient sends presence with non-negative priority. If that's not what you're seeing, the first place to look is probably the server configuration.
Finally i got offline message at login in xmpp server.
connection.connect();
connection.login(username, password);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
final Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
}
}
}, null);

Categories

Resources