Steps to integrate iFlyChatLibrary to Android project
- Download the
iFlyChatLibrary.jarfile from the above link. - Open your Android studio project.
- Look for libs folder in your
appmodule. If thelibsfolder is not there then createlibsfolder inappmodule. - Copy and paste
iFlyChatLibrary.jarfile in thelibsfolder. - Right click on
iFlyChatLibrary.jarfile 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
LocalBroadcastManagerobject.LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this); -
Call
setiFlyChatContext(Context)method ofiFlyChatUtilitiesclass 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
iFlyChatUserSessionobject with your username and password.iFlyChatUserSession userSession = new iFlyChatUserSession("your_username", "your_password"); -
If Session Key is available then, Create and initialize the
iFlyChatUserSessionobject with your username, password and session key.iFlyChatUserSession userSession = new iFlyChatUserSession("your_username", "your_password", “your_sessionKey”); -
Create and initialize the
iFlyChatConfigobject. Use theserverHostandauthUrlprovided by iFlyChat.iFlyChatConfig config = new iFlyChatConfig(serverHost, authUrl, isHttps); -
Create and initialize the
iFlyChatUserAuthServiceobject.iFlyChatUserAuthService authService = new iFlyChatUserAuthService(config, userSession); -
Get sessionKey from
userSessionobject.String sessionKey = userSession.getSessionKey(); -
Create and initialize the
iFlyChatServiceobject.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
IntentFilterobject.
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
iFlyChatMessageObjectmsg.
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
iFlyChatMessageObjectmsg.
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.onMessageFromUsercorresponding to your action ofsendMessageToUser. Match the intent action in youronReceive()method. If matched then you will get correspondingiFlyChatMessageobject.if (intent.getAction().equals("iFlyChat.onMessageFromUser")) { message = intent.getParcelableExtra("messageObj"); } -
To receive event
iFlyChat.onMessageFromRoomcorresponding to your action ofsendMessageToRoom. Match the intent action in youronReceive()method. If matched then you will get correspondingiFlyChatMessageobject.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(); }
