Custom File Upload Handler
How to upload files to a third party host or server
In certain circumstances, the Chat SDK needs to be able to upload files to an external server. Those situations are the following:
  1. 1.
    Uploading the user's profile image
  2. 2.
    Uploading thread images
  3. 3.
    Sending image messages
File upload is decoupled from the main project and is handled by the UploadHandler service.
In order to customize the upload behavior or upload files to a custom server, it's necessary to create a new upload handler service and then register it with the Chat SDK framework.

The Upload Handler

To make a new upload handler you will need to subclass the AbstractUploadHandler for Android or the BAbstractUploadHandler for iOS.
The upload handler only has one method that needs to be implemented:
Java
Objective-C
1
Observable<FileUploadResult> uploadFile(byte[] data, String name, String mimeType);
Copied!
1
-(RXPromise *) uploadFile:(NSData *)file withName: (NSString *) name mimeType: (NSString *) mimeType;
Copied!

iOS

To create a custom upload handler, you need to do the following:
  1. 1.
    Create a new subclass of the BAbstractUploadHandler class.
  2. 2.
    Implement the following method:
    1
    -(RXPromise *) uploadFile:(NSData *)file withName: (NSString *) name mimeType: (NSString *) mimeType;
    Copied!
  3. 3.
    Register your new upload handler with the framework. In your App Delegate after you have called BChatSDK.initialize add the following line of code:
    1
    BChatSDK.shared.networkAdapter.upload = [[YourUploadHandler alloc] init]
    Copied!
To see an example of how to implement the function, it's recommended to look at the BFirebaseUploadHandler class which demonstrates a model implementation for Google File Storage.

Android

To create a custom upload handler, you need to do the following:
  1. 1.
    Create a new subclass of the AbstractUploadHandler class.
  2. 2.
    Implement the following method:
    1
    Observable<FileUploadResult> uploadFile(byte[] data, String name, String mimeType);
    Copied!
  3. 3.
    Register your new upload handler with the framework. After you have called ChatSDK.initialize add the following line of code:
    1
    ChatSDK.shared().a().upload = [[YourUploadHandler alloc] init]
    Copied!
To see an example of how to implement the function, it's recommended to look at the FirebaseUploadHandler class which demonstrates a model implementation for Google File Storage.
Last modified 9mo ago
Copy link