Skip to content

Channelize API SDK Integration

rohitphogat19 edited this page Mar 12, 2019 · 31 revisions

Channelize-API SDK

The following documentation is built to help you with integration of our iOS API SDK into your project.

Dependencies

Requirements Before we begin, please do make sure that

  1. Your application is built on iOS 9.0 or above.
  2. Since Channelize SDK as of now only supports Version 9.0 or higher.
  3. You have Xcode 9.4.1 or later as your IDE to install and run Channelize SDK on iOS.
  4. Swift 4 / 4.1

Pod Installation

To use API SDK you need to install following pods

pod 'MQTTClient', '0.14.0'
pod 'MQTTClient/Websocket'
pod 'Alamofire', '4.7.3'
pod 'AlamofireObjectMapper', '5.1.0'

Required Files

You need create an .plist file with name as "Channelize-Info.plist" and place following keys

<key>PUBLIC_KEY</key>
<string>xxxx PublicKey xxxx</string>
<key>API_URL</key>
<string>xxxx API URL xxxxx </string>
<key>MQTT_URL</key>
<string>xxxxxxx MQTT URL xxxxxx</string>

App Permission

Make sure following app permissions are in your app info.plist file

<key>NSAppTransportSecurity</key>
  <dict>
  	<key>NSAllowsArbitraryLoads</key>
  	<true/>
  </dict>

Configuring Channelize

  • To configure Channelize you need to add the following code in didFinishLaunchingWithOptions function of your project's AppDelegate.swift file
Channelize.configure()

Login to Channelize Server

  • You need to Login into Channelize server before using APIs. To login add following code in your app(most preferred on login button action in your app)
Channelize.main.login(username: email, password: password){(user,error) in 
   guard error == nil else {
       return }
  //Use User details here

Connecting User to Channelize Server

  • Connect user to get various event delegate calls(Please connect only logged users)
Channelize.connect()

Disconnecting User from Channelize Server

  • Disconnect a user from Channelize server to stop receiving further notifications, messages and event notifications
Channelize.disconnect()

Logout From Channelize Server

  • To logout from Channelize server, please add following code, where you want user to logout
Channelize.main.logout()

Integration

Here are the few steps that you need to follow for integrating Channelize API with your application. It includes configuring Channelize.

Login to Channelize Server

  • You need to login first before launching channelize by adding the following code on login button action
  Channelize.main.login(username: email, password: password){(user,error) in
  	guard error == nil else {    // Error. 
         return
      }
  }		

Connect to Channelize Server

  • Connect logged in user to our server to get various event delegate calls
  Channelize.connect()

Disconnect to Channelize Server

  • Disconnect logged in user from our server to stop receiving further notifications, messages and event notifications
  Channelize.disconnect()

Logout from Channelize Server

  • For performing Logout action you need to add the following code
  Channelize.main.logout()

API References

Conversations

Retrieve a list of all or certain conversations

  • For getting the total number of chats you can request the following call -
 CHService.main.getRecentChatCount() {(count, error) in
         
     if let error = error {
         return print(error.localizedDescription)
     }    
  
  }
  • You can also retrieve a list of all conversations in Channelize application by requesting the following call.
  CHService.main.getChats(limit: DEFAULT_LIMIT, offset: OFFSET) {(conversations, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a list of all or certain group conversations

  • For getting the total number of groups you can request the following call -
 CHService.main.getGroupsCount() {(count, error) in
         
     if let error = error {
         return print(error.localizedDescription)
     }    
  
  }
  • You can also retrieve a list of group conversations in Channelize application by requesting the following call.
  CHService.main.getGroups(limit: DEFAULT_LIMIT, offset: OFFSET) {(conversations, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a conversation for a particular user

  • You can also retrieve a conversations with a user in Channelize application by requesting the following call.
  CHService.main.getChat(with: USER_ID) {(conversation, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a conversation using conversation id

  • You can also retrieve a conversations using a conversation id in Channelize application by requesting the following call.
  CHService.main.getChat(of: CONVERSATION_ID) {(conversation, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Creating a conversation

  • CHConversation objects are created by calling createGroup(title:memberIds:imageData:completion) using CHService class. The fields are as follows:
    • title (Required, String): text title of Group.
    • memberIds (Required, [String]): An array of String containing IDs of users.
    • imageData (Optional, Data): An optional Data object of the image used for profile picture.
    • completion (Required, ChatData): Completion handler which return the created object on CHConversation & error is the request failed.
  CHService.main.createGroup(title:String, memberIds:[String], imageData:Data?){(conversation, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Deleting a conversation

  • You can delete a conversation using the delete api call. Deleting a conversation also deletes it's associated messages
  conversation.delete(){(status, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Clearing a conversation

  • You can clear a conversation by calling clear() on conversation instance. It'll delete all the messages -
  conversation.clear(){(status, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Leave a Group

  conversation.leave(){(status, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Add members to a Group

  conversation.addMembers(userIds: [USER_IDS]){(conversation, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Remove members from a Group

  conversation.removeMembers(userIds: [USER_IDS]){(conversation, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Make a Admin of a Group

  conversation.makeAdmin(userId: USER_ID){(status, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Update Group Title

  conversation.changeTitle(title: TITLE_NAME){(conversation, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Update Group Profile Image

  conversation.updateProfileImage(data: IMAGE_DATA){(conversation, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Messages

Get the list of messages for a conversation

  • For getting the total number of messages -
 conversation.getMessageCount() {(count, error) in
         
     if let error = error {
         return print(error.localizedDescription)
     }    
  
  }
  • Using the getMessages(limit:Int?, offset:Int?, completion:completion) call you can retrieve the list of messages which provide an array of CHMessage type objects.
  conversation.getMessages(limit: DEFAULT_LIMIT, offset: OFFSET) {(messages, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Remove messages

  • Using the deleteMessages(messageIds:[String], completion: completion) call you can delete messages using their ID's.
  conversation.deleteMessages(messageIds:[MESSAGE_ID'S]){(status, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Mark messages as read

  • Using the markAllMessagesAsRead() call you can delete messages using their ID's.
  conversation.markAllMessagesAsRead()

Mark a message as read

  • Using the markAsRead() call you can delete messages using their ID's.
  message.markAsRead()

Sending Messages

  • For Normal Messages CHMessage objects are created by calling sendMessage(text:data:fileUrl:type:completion) using conversation object. The fields are as follows:
    • text (Optional, String?): text message as String. Add this if you are sending a text message.
    • data (Optional, Data?): file as Data if you sending audio,image or video file as Data.
    • fileUrl (Optional, URL?): url of the file you are trying to send.
    • type (Required, CHMessageType): attachment type of the message. Choose from the enum.
    • completion (Required, MessageResult): Completion handler which return the created object of CHMessage & error if the request failed.
  conversation.sendMessage(text: MESSAGE_TEXT, data: FILE_DATA, fileUrl: FILE_URL, type:ATTACHMENT_TYPE){
     (message, error) in
     guard error == nil else {    // Error.
        return
     }
  }
  • For Quoted Message CHMessage objects are created by calling sendQuotedMessage(text:quotedMessage:completion) using conversation object. The fields are as follows:
    • text (Required, String): text message as String.
    • quotedMessage (Required, [String]): An instance of the message that is being quoted.
    • completion (Required, MessageResult): Completion handler which return the created object of CHMessage & error if the request failed.
  conversation.sendQuotedMessage(text: MESSAGE_TEXT, quotedMessage: QUOTED_MESSAGE){(message, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Users

Retrieve a list of all or certain users

  • For getting the total number of user you can request the following call -
 CHService.main.getUserCount() {(count, error) in
         
     if let error = error {
         return print(error.localizedDescription)
     }    
  
  }
  • You can also retrieve a list of group conversations in Channelize application by requesting the following call.
  CHService.main.getUsers(limit: defaultLimit, offset: offset){(users, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a list of all or certain online users

  • You can also retrieve a list of group conversations in Channelize application by requesting the following call.
  CHService.main.getOnlineUsers {(onlineUsers, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a user using user id

  • You can also retrieve a user using it's user_id in Channelize application by requesting the following call.
  CHService.main.getUser(with: USER_ID) {(user, error) in
          
      if let error = error {
          return print(error.localizedDescription)
      }    
   
   }

Retrieve a list of all or certain blocked users in an application

  • You can also retrieve a list of all blocked users in Channelize application by requesting the following call. The method returns a list of User objects which contain information about blocked users.
  CHService.main.getBlockedUsers(){ (users, error) in
     guard error == nil else {    // Error.
        return
     }
  }

Blocking/Unblocking

  • User’s have the ability to block/unblcok other users, allowing them to have a sense of privacy and security around the kinds of people that they interact with. If User A blocks User B:

    • User B can not create a conversation with User A & vice versa.
    • User B can not send messages to User A & vice versa, if a conversation already exists between the two.
    • Note that these rules do not apply to Group Conversations
  CHService.main.blockUser(userId: USER_ID){ (status, error) in
     guard error == nil else {    // Error.
        return
     }
     if status {
          // Successfully Blocked 
     }
  }
  • To unblock a user using the following code -
  CHService.main.unblockUser(userId: USER_ID){ (status, error) in
     guard error == nil else {    // Error.
        return
     }
     if status {
          // Successfully UnBlocked 
     }
  }