Steps to integrate iFlyChatLibrary to Android project
- Download the
iFlyChatLibrary.jar
file from the above link. - Open your Android studio project.
- Look for libs folder in your
app
module. If thelibs
folder is not there then createlibs
folder inapp
module. - Copy and paste
iFlyChatLibrary.jar
file in thelibs
folder. - Right click on
iFlyChatLibrary.jar
file in Android Studio and click onAdd as Library
. -
To start the chat, we need to initialize the activity. In your activity’s
onCreate()
method :-
Create and Initialize a
LocalBroadcastManager
object.LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
-
Call
setiFlyChatContext(Context)
method ofiFlyChatUtilities
class in iFlyChatLibrary and give your application context object to this class.iFlyChatUtilities.setiFlyChatContext(getApplicationContext());
-
If Session Key is not available then, Create and initialize the
iFlyChatUserSession
object with your username and password.iFlyChatUserSession userSession = new iFlyChatUserSession("your_username", "your_password");
-
If Session Key is available then, Create and initialize the
iFlyChatUserSession
object with your username, password and session key.iFlyChatUserSession userSession = new iFlyChatUserSession("your_username", "your_password", “your_sessionKey”);
-
Create and initialize the
iFlyChatConfig
object. Use theserverHost
andauthUrl
provided by iFlyChat.iFlyChatConfig config = new iFlyChatConfig(serverHost, authUrl, isHttps);
-
Create and initialize the
iFlyChatUserAuthService
object.iFlyChatUserAuthService authService = new iFlyChatUserAuthService(config, userSession);
-
Get sessionKey from
userSession
object.String sessionKey = userSession.getSessionKey();
-
Create and initialize the
iFlyChatService
object.iFlyChatService service = new iFlyChatService(userSession, config, authService);
-
Connect the chat using
sessionKey
.service.connectChat(sessionKey);
-
-
In your activity’s
onStart()
method :- Create and Initialize a
IntentFilter
object.
IntentFilter intentFilter = new IntentFilter();
- Depending on the events you want to listen, you can add Actions to your intent filter. Following are some of the actions you can listen to.
intentFilter.addAction("iFlyChat.onChatConnect"); intentFilter.addAction("iFlyChat.onGlobalListUpdate"); intentFilter.addAction("iFlyChat.onMessageFromRoom"); intentFilter.addAction("iFlyChat.onMessageFromUser");
- You need to register a receiver with the above intent filter to your broadcast manager object
bManager
.
bManager.registerReceiver(bReceiver, intentFilter);
- Create and Initialize a
-
To send message to User
- Create a new
iFlyChatMessageObject
msg
.
iFlyChatMessage msgObj = new iFlyChatMessage("", currentUser.getId(), currentUser.toId() , currentUser.getName(), currentUser.toName(), messageToUser_String , "", currentUser.getProfileUrl(), currentUser.getAvatarUrl(), currentUser.getRole(), "", "user");
- Call
sendMessageToUser(messageObject)
service.sendMessageToUser(msgObj);
- Create a new
-
To send message to Room
- Create a new
iFlyChatMessageObject
msg
.
iFlyChatMessage msgObj = new iFlyChatMessage("", currentUser.getId(), currentUser.toId(), currentUser.getName(), currentUser.toName() , messageToRoom_String, "", currentUser.getProfileUrl(), currentUser.getAvatarUrl(), currentUser.getRole(), "", "room");
- Call
sendMessageToRoom(messageObject)
service.sendMessageToRoom(msgObj);
- Create a new
-
To receive events from server. Create and initialize your receiver object and call
onReceive()
method.private BroadcastReceiver bReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { }};
-
To receive event
iFlyChat.onMessageFromUser
corresponding to your action ofsendMessageToUser
. Match the intent action in youronReceive()
method. If matched then you will get correspondingiFlyChatMessage
object.if (intent.getAction().equals("iFlyChat.onMessageFromUser")) { message = intent.getParcelableExtra("messageObj"); }
-
To receive event
iFlyChat.onMessageFromRoom
corresponding to your action ofsendMessageToRoom
. Match the intent action in youronReceive()
method. If matched then you will get correspondingiFlyChatMessage
object.if (intent.getAction().equals("iFlyChat.onMessageFromRoom")) { message = intent.getParcelableExtra("messageObj"); }
-
Unregister your broadcast Receiver in the
onStop()
method of your activity.@Override protected void onStop() { super.onStop(); bManager.unregisterReceiver(bReceiver); }
-
Disconnect chat whenever the application is terminated. This can be done in your activity’s
onDestroy()
method by callingdisconnectChat()
method of service object.@Override protected void onDestroy(){ service.disconnectChat(); super.onDestroy(); }