First make xmpp package in that package create class name XmppClient
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.easychat.app.ChatListAct;
import com.easychat.app.R;
import com.easychat.constant.Global;
import com.easychat.data.UserInfoChat;
import com.easychat.mlisteners.ChatReceiver;
import com.easychat.mlisteners.QueueChat;
import com.easychat.task.XMPPConn;
import com.easychat.utility.AroundLogger;
import com.easychat.utility.Utility;
public class XmppClient implements PacketListener{
private static final String TAG = "XmppClient";
static XMPPConnection connection;
static List<ChatReceiver> mChatReceivers;
static Context mContext;
private static XmppClient mXmppClient;
// static List<QueueChat> mQueueChats;
static HashMap<String, QueueChat> mQueueChats;
static QueueChat mCurrentChatListener;
private XmppClient(Context context) {
mContext = context;
mQueueChats = new HashMap<String, QueueChat>();
}
public static XmppClient getInstance(Context context) {
if(mXmppClient == null) {
mXmppClient = new XmppClient(context);
}
return mXmppClient;
}
/*public void setCurrentChatListener(QueueChat queueChat) {
mCurrentChatListener = queueChat;
}*/
public static void setCurrentChatListener(String sender, ChatReceiver chatReceiver) {
mCurrentChatListener = new QueueChat();
mCurrentChatListener.setChatReceiver(chatReceiver);
mCurrentChatListener.setCount(0);
mCurrentChatListener.setSender(sender.toUpperCase());
removeChatFromQueue(sender.toUpperCase());
}
public static void removeCurrentChatListener() {
if(mCurrentChatListener != null) {
mCurrentChatListener = null;
}
}
public static void addChatInQueue(String sender, ChatReceiver chatReceiver) {
if(mQueueChats == null) mQueueChats = new HashMap<String, QueueChat>();
if(mQueueChats.size() > 0 && mQueueChats.containsKey(sender.toUpperCase())) {
QueueChat chat = mQueueChats.get(sender.toUpperCase());
int count = chat.increaseCounter();
QueueChat mQueueChat = new QueueChat();
mQueueChat.setChatReceiver(null);
mQueueChat.setCount(count);
mQueueChat.setSender(sender.toUpperCase());
mQueueChats.put(sender.toUpperCase(), mQueueChat);
// mQueueChats.put(sender.toUpperCase(), mQueueChats.get(sender.toUpperCase()).increaseCounter());
}else {
QueueChat mQueueChat = new QueueChat();
mQueueChat.setChatReceiver(null);
mQueueChat.setCount(1);
mQueueChat.setSender(sender.toUpperCase());
mQueueChats.put(sender.toUpperCase(), mQueueChat);
}
}
public static void removeChatFromQueue(String sender) {
if(mQueueChats != null) {
mQueueChats.remove(sender.toUpperCase());
}
}
public static int getChatQueueCount(String sender) {
if(mQueueChats != null && mQueueChats.containsKey(sender.toUpperCase())) {
return mQueueChats.get(sender.toUpperCase()).getCount();
}else {
return 0;
}
}
/*public static void addChatReceiver(ChatReceiver chatReceiver) {
if(mChatReceivers == null) {
mChatReceivers = new ArrayList<ChatReceiver>();
}
mChatReceivers.add(chatReceiver);
}*/
public static void removeChatReceiver(ChatReceiver chatReceiver) {
if(mChatReceivers != null && mChatReceivers.size()>0) {
mChatReceivers.remove(chatReceiver);
}
}
public static void setConnection(XMPPConnection xmppConnection, PacketListener packetListener) {
connection = xmppConnection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(packetListener, filter);
/*filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITETOCHAT));
connection.addPacketListener(packetListener, filter);
filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITATION_ACCEPT));
connection.addPacketListener(packetListener, filter);
filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITATION_DECLINE));
connection.addPacketListener(packetListener, filter);*/
AroundLogger.info(TAG + "Registered for chat as " + connection.getUser());
}
}
public static void disconnect() {
if(connection != null && connection.isConnected()) {
if(mXmppClient != null) {
connection.removePacketListener(mXmppClient);
}
connection.disconnect();
}
}
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
if(message.getBody().equals(Global.TYPE_INVITETOADDFRIEND)) {
Intent inivitetoAddFriend = new Intent(Global.ACTION_RECEIVED_INVITOADDFRIEND);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender= StringUtils.parseName(message.getFrom());
inivitetoAddFriend.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(inivitetoAddFriend);
}else if(message.getBody().equals(Global.TYPE_FRIEND_REQUEST_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_ACCEPT_FRIEND_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_FRIEND_REQUEST_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_DECLINE_FRIEND_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITETOCHAT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_INVITATIONTOCHAT);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITATION_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_ACCEPTCHATINVITATION);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITATION_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_DECLINECHATINVITATION);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST_ACCCEPT);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST_DECLINE);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_NOTIFY_ONLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_NOTIFY_FRIEND_ONLINE);
invitationIntent.putExtra("sender", StringUtils.parseName(message.getFrom()));
mContext.sendBroadcast(invitationIntent);
}else {
// String fromName = StringUtils.parseBareAddress(message.getFrom());
AroundLogger.info(TAG + "Got text [" + message.getBody() + "] from [" + StringUtils.parseName(message.getFrom()) + "]");
if(mCurrentChatListener != null && mCurrentChatListener.getChatReceiver() != null && mCurrentChatListener.getSender().equalsIgnoreCase(StringUtils.parseName(message.getFrom()))) {
mCurrentChatListener.getChatReceiver().receive(message);
}else {
createNotification(message);
addChatInQueue(StringUtils.parseName(message.getFrom()), null);
}
}
}
}
public static boolean isConnected() {
if(connection != null && connection.isConnected()) {
return true;
}else {
return false;
}
}
private void createNotification(Message message) {
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.app_icon;
CharSequence tickerText = mContext.getString(R.string.app_name);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
UserInfoChat mInfoChat = (UserInfoChat) message.getProperty("userinfo");
CharSequence contentTitle = StringUtils.parseName(message.getFrom());
CharSequence contentText = message.getBody();
Intent notificationIntent = new Intent(mContext, ChatListAct.class);
notificationIntent.putExtra("username", contentTitle.toString());
notificationIntent.putExtra("message", contentText);
notificationIntent.putExtra("userid", mInfoChat.userid);
// notificationIntent.putExtra("senderid", mInfoChat.senderId);
// notificationIntent.putExtra("status", (String)message.getProperty("status"));
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
public static void sendMessage(final Context context, Message message) {
if(connection != null && connection.isConnected()) {
connection.sendPacket(message);
}else {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Utility.showToast(context.getString(R.string.xmpp_disconnected_error), context);
new XMPPConn(context.getApplicationContext(), null).execute();
}
});
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.easychat.app.ChatListAct;
import com.easychat.app.R;
import com.easychat.constant.Global;
import com.easychat.data.UserInfoChat;
import com.easychat.mlisteners.ChatReceiver;
import com.easychat.mlisteners.QueueChat;
import com.easychat.task.XMPPConn;
import com.easychat.utility.AroundLogger;
import com.easychat.utility.Utility;
public class XmppClient implements PacketListener{
private static final String TAG = "XmppClient";
static XMPPConnection connection;
static List<ChatReceiver> mChatReceivers;
static Context mContext;
private static XmppClient mXmppClient;
// static List<QueueChat> mQueueChats;
static HashMap<String, QueueChat> mQueueChats;
static QueueChat mCurrentChatListener;
private XmppClient(Context context) {
mContext = context;
mQueueChats = new HashMap<String, QueueChat>();
}
public static XmppClient getInstance(Context context) {
if(mXmppClient == null) {
mXmppClient = new XmppClient(context);
}
return mXmppClient;
}
/*public void setCurrentChatListener(QueueChat queueChat) {
mCurrentChatListener = queueChat;
}*/
public static void setCurrentChatListener(String sender, ChatReceiver chatReceiver) {
mCurrentChatListener = new QueueChat();
mCurrentChatListener.setChatReceiver(chatReceiver);
mCurrentChatListener.setCount(0);
mCurrentChatListener.setSender(sender.toUpperCase());
removeChatFromQueue(sender.toUpperCase());
}
public static void removeCurrentChatListener() {
if(mCurrentChatListener != null) {
mCurrentChatListener = null;
}
}
public static void addChatInQueue(String sender, ChatReceiver chatReceiver) {
if(mQueueChats == null) mQueueChats = new HashMap<String, QueueChat>();
if(mQueueChats.size() > 0 && mQueueChats.containsKey(sender.toUpperCase())) {
QueueChat chat = mQueueChats.get(sender.toUpperCase());
int count = chat.increaseCounter();
QueueChat mQueueChat = new QueueChat();
mQueueChat.setChatReceiver(null);
mQueueChat.setCount(count);
mQueueChat.setSender(sender.toUpperCase());
mQueueChats.put(sender.toUpperCase(), mQueueChat);
// mQueueChats.put(sender.toUpperCase(), mQueueChats.get(sender.toUpperCase()).increaseCounter());
}else {
QueueChat mQueueChat = new QueueChat();
mQueueChat.setChatReceiver(null);
mQueueChat.setCount(1);
mQueueChat.setSender(sender.toUpperCase());
mQueueChats.put(sender.toUpperCase(), mQueueChat);
}
}
public static void removeChatFromQueue(String sender) {
if(mQueueChats != null) {
mQueueChats.remove(sender.toUpperCase());
}
}
public static int getChatQueueCount(String sender) {
if(mQueueChats != null && mQueueChats.containsKey(sender.toUpperCase())) {
return mQueueChats.get(sender.toUpperCase()).getCount();
}else {
return 0;
}
}
/*public static void addChatReceiver(ChatReceiver chatReceiver) {
if(mChatReceivers == null) {
mChatReceivers = new ArrayList<ChatReceiver>();
}
mChatReceivers.add(chatReceiver);
}*/
public static void removeChatReceiver(ChatReceiver chatReceiver) {
if(mChatReceivers != null && mChatReceivers.size()>0) {
mChatReceivers.remove(chatReceiver);
}
}
public static void setConnection(XMPPConnection xmppConnection, PacketListener packetListener) {
connection = xmppConnection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(packetListener, filter);
/*filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITETOCHAT));
connection.addPacketListener(packetListener, filter);
filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITATION_ACCEPT));
connection.addPacketListener(packetListener, filter);
filter = new MessageTypeFilter(Message.Type.fromString(Global.TYPE_INVITATION_DECLINE));
connection.addPacketListener(packetListener, filter);*/
AroundLogger.info(TAG + "Registered for chat as " + connection.getUser());
}
}
public static void disconnect() {
if(connection != null && connection.isConnected()) {
if(mXmppClient != null) {
connection.removePacketListener(mXmppClient);
}
connection.disconnect();
}
}
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
if(message.getBody().equals(Global.TYPE_INVITETOADDFRIEND)) {
Intent inivitetoAddFriend = new Intent(Global.ACTION_RECEIVED_INVITOADDFRIEND);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender= StringUtils.parseName(message.getFrom());
inivitetoAddFriend.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(inivitetoAddFriend);
}else if(message.getBody().equals(Global.TYPE_FRIEND_REQUEST_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_ACCEPT_FRIEND_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_FRIEND_REQUEST_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_DECLINE_FRIEND_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITETOCHAT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_INVITATIONTOCHAT);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITATION_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_ACCEPTCHATINVITATION);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_INVITATION_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_DECLINECHATINVITATION);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY_ACCEPT)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST_ACCCEPT);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_REQUEST_GALLERY_DECLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_GALLERY_REQUEST_DECLINE);
UserInfoChat userInfoChat = (UserInfoChat) message.getProperty("userinfo");
userInfoChat.sender = StringUtils.parseName(message.getFrom());
invitationIntent.putExtra("userinfo", userInfoChat);
mContext.sendBroadcast(invitationIntent);
}else if(message.getBody().equals(Global.TYPE_NOTIFY_ONLINE)) {
Intent invitationIntent = new Intent(Global.ACTION_RECEIVED_NOTIFY_FRIEND_ONLINE);
invitationIntent.putExtra("sender", StringUtils.parseName(message.getFrom()));
mContext.sendBroadcast(invitationIntent);
}else {
// String fromName = StringUtils.parseBareAddress(message.getFrom());
AroundLogger.info(TAG + "Got text [" + message.getBody() + "] from [" + StringUtils.parseName(message.getFrom()) + "]");
if(mCurrentChatListener != null && mCurrentChatListener.getChatReceiver() != null && mCurrentChatListener.getSender().equalsIgnoreCase(StringUtils.parseName(message.getFrom()))) {
mCurrentChatListener.getChatReceiver().receive(message);
}else {
createNotification(message);
addChatInQueue(StringUtils.parseName(message.getFrom()), null);
}
}
}
}
public static boolean isConnected() {
if(connection != null && connection.isConnected()) {
return true;
}else {
return false;
}
}
private void createNotification(Message message) {
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.app_icon;
CharSequence tickerText = mContext.getString(R.string.app_name);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
UserInfoChat mInfoChat = (UserInfoChat) message.getProperty("userinfo");
CharSequence contentTitle = StringUtils.parseName(message.getFrom());
CharSequence contentText = message.getBody();
Intent notificationIntent = new Intent(mContext, ChatListAct.class);
notificationIntent.putExtra("username", contentTitle.toString());
notificationIntent.putExtra("message", contentText);
notificationIntent.putExtra("userid", mInfoChat.userid);
// notificationIntent.putExtra("senderid", mInfoChat.senderId);
// notificationIntent.putExtra("status", (String)message.getProperty("status"));
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
public static void sendMessage(final Context context, Message message) {
if(connection != null && connection.isConnected()) {
connection.sendPacket(message);
}else {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Utility.showToast(context.getString(R.string.xmpp_disconnected_error), context);
new XMPPConn(context.getApplicationContext(), null).execute();
}
});
}
}
}
create next class name XmppConnectionListener
import org.jivesoftware.smack.ConnectionListener;
import android.content.Context;
import com.easychat.utility.AroundLogger;
public class XmppConnectionListener implements ConnectionListener{
static final String Tag = "XmppConnectionListener";
Context mContext;
public XmppConnectionListener(Context context) {
this.mContext = context;
}
@Override
public void connectionClosed() {
AroundLogger.info(Tag + " connectionClosed()");
// new XMPPConn(mContext, null).execute();
}
@Override
public void connectionClosedOnError(Exception arg0) {
// TODO Auto-generated method stub
AroundLogger.info(Tag + " connectionClosedOnError()");
}
@Override
public void reconnectingIn(int arg0) {
// TODO Auto-generated method stub
AroundLogger.info(Tag + " reconnectingIn()");
}
@Override
public void reconnectionFailed(Exception arg0) {
// TODO Auto-generated method stub
AroundLogger.info(Tag + " reconnectionFailed()");
}
@Override
public void reconnectionSuccessful() {
// TODO Auto-generated method stub
AroundLogger.info(Tag + " reconnectionSuccessful()");
}
}
create BroadcastReceiver class name ChatBroadcastReceiver
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.easychat.app.AddFriendInvitationDialogActivity;
import com.easychat.app.ChatInvitationDialogActivity;
import com.easychat.app.R;
import com.easychat.app.RequestGalleryImagesDialogActivity;
import com.easychat.app.SimpleDialogActivity;
import com.easychat.app.SimpleGalleryDialogActivity;
import com.easychat.constant.Global;
import com.easychat.data.UserInfoChat;
import com.easychat.utility.Utility;
public class ChatBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Global.ACTION_RECEIVED_INVITOADDFRIEND)) {
Intent chatInvitationIntent = new Intent(context,AddFriendInvitationDialogActivity.class);
UserInfoChat mUserInfoChat =(UserInfoChat)intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_ACCEPT_FRIEND_REQUEST)) {
Intent chatInvitationIntent = new Intent(context, SimpleDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", true);
chatInvitationIntent.putExtra("flag", true);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_DECLINE_FRIEND_REQUEST)) {
Intent chatInvitationIntent = new Intent(context, SimpleDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", false);
chatInvitationIntent.putExtra("flag", true);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_INVITATIONTOCHAT)) {
Intent chatInvitationIntent = new Intent(context, ChatInvitationDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_ACCEPTCHATINVITATION)) {
Intent chatInvitationIntent = new Intent(context, SimpleDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", true);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_DECLINECHATINVITATION)) {
Intent chatInvitationIntent = new Intent(context, SimpleDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", false);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_GALLERY_REQUEST)) {
Intent chatInvitationIntent = new Intent(context, RequestGalleryImagesDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_GALLERY_REQUEST_ACCCEPT)) {
Intent chatInvitationIntent = new Intent(context, SimpleGalleryDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", true);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_GALLERY_REQUEST_DECLINE)) {
Intent chatInvitationIntent = new Intent(context, SimpleGalleryDialogActivity.class);
UserInfoChat mUserInfoChat = (UserInfoChat) intent.getSerializableExtra("userinfo");
chatInvitationIntent.putExtra("userinfo", mUserInfoChat);
chatInvitationIntent.putExtra("accepted", false);
chatInvitationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chatInvitationIntent);
}else if(intent.getAction().equals(Global.ACTION_RECEIVED_NOTIFY_FRIEND_ONLINE)) {
String notifyFriendsGetOnline = Utility.getSharedKey(Global.PREF_NOTIFY_FRIENDS_GET_ONLINE, context);
if(!TextUtils.isEmpty(notifyFriendsGetOnline) && Boolean.parseBoolean(notifyFriendsGetOnline)) {
String isStatusBarNotification = Utility.getSharedKey(Global.PREF_STATUSBAR_NOTIFICATION, context);
if(!TextUtils.isEmpty(isStatusBarNotification) && Boolean.parseBoolean(isStatusBarNotification)) {
//Create notification
createNotification(context, intent.getStringExtra("sender"));
}else {
//Display Toast
Utility.showToast(String.format(context.getString(R.string.xxx_user_is_online), intent.getStringExtra("sender")), context);
}
}
}
}
private void createNotification(Context mContext, String username) {
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.app_icon;
CharSequence tickerText = mContext.getString(R.string.app_name);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentText = String.format(mContext.getString(R.string.xxx_user_is_online), username);
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, username, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
}