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.

Contents

  1. Dependencies
  2. Pod Installation
  3. Required Files
  4. App Permission
  5. Configuring and Connecting
  6. API references
  7. Real Time Updates

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 and Connecting

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()

API References

APIs for Conversations

Number of Conversations

CHService.main.getRecentChatCount(completion: {(count,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your Stuff here
})

Get Recent Conversations

  • To retrieve a list of all conversations request following API Call
//limit : Number of chats you want to get in one call
//offset : Index of starting chat like 0,30,etc.
CHService.main.getChats(limit: 50, offset: 0, completion: {(conversations,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Get Conversation With a particular user

  • To get conversation with a particular User, make following API request
CHService.main.getChat(with: USER_ID, completion: {(conversation,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Get Conversation of conversation id

  • To get conversation with a particular id, make following API request
CHService.main.getChat(of: CHAT_ID, completion: {(conversation,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Clearing a Conversation

  • To clear a conversation, make following API request using a conversation object.
conversation.clear(){(status, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Deleting a Conversation

  • To delete a conversation, make following API request using a conversation object.
conversation.delete(){(status, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Groups APIs

Get total Number of groups Count

  • To get total number of groups, make following API request
CHService.main.getGroupsCount(completion: {(groupsCount) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Get List of Groups

  • To get list of groups, make following API request
//limit: Numbers of groups list in one call
//offset: Starting index for groups list call
CHService.main.getGroups(limit: LIMIT, offset: OFFSET, completion: {(conversations,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Creating Groups

  • CHConversation objects are created by calling createGroup(title:memberIds:imageData:completion) using CHService class.
  • To create a groups, create following API request Fields are:
  • title:(String,Required) -> Title of the group
  • memberIds:(Array,Required) -> Groups Members Ids
  • imageData: (Data,Optional) -> Image Data for Group Profile Image
  • completion:(Required, ChatData): Completion handler which return the created object of CHConversation & error if the request failed.
CHService.main.createGroup(title: GROUP_TITLE, memberIds: [MEMBERS_IDS], imageData: IMAGE_DATA, completion: {(conversation,error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
})

Leave a group

  • To leave a group, make following API request using conversation object of that group.
conversation.leave(){(status, error) in
    guard error == nil else {    // Error.
        return
    }
}

Add Member to a group

  • To add a member to the Group make following API request using conversation object of that group.
conversation.addMembers(userIds: [USER_IDS]){(conversation, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Remove Member from the Group

  • To remove members from the group, make following API request using conversation object of that group.
conversation.removeMembers(userIds: [USER_IDS]){(conversation, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Make a member Admin of the Group

  • To make members admin of the group, make following API request using conversation object of that group.
conversation.makeAdmin(userId: USER_ID){(status, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Updating a group Title

  • To update title of the group, make following API request using conversation object of that group.
conversation.changeTitle(title: TITLE_NAME){(conversation, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

Updating Group Profile Photo

  • To update a group profile photo, make following API request using conversation object of that group.
conversation.updateProfileImage(data: IMAGE_DATA){(conversation, error) in
    if let error = error {
        return print(error.localizedDescription)
    }
    // Do your stuff here
}

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)
     }
     //Do your stuff here  
  }
  • 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)
      }    
      //Do your stuff here
   }

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)
      }
      //Do your stuff here    
   }

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 mark a message read
  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)
     }    
     //Do your stuff here
  }
  • 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)
      }    
      //Do your stuff here
   }

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)
      }    
      //Do your stuff here
   }

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)
      }    
      //Do your stuff here
   }

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 
     }
  }

Real Time Updates