File Sharing API for Kotlin SDK
Breaking changes in v9.0.0
PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0 ) of the Kotlin SDK.
For more details about what has changed, refer to Java/Kotlin SDK migration guide.
You can upload and share files up to 5 MB on PubNub. Common uses include sharing images in chat apps and documents in healthcare.
When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.
Request execution
Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously.
You must invoke the .sync() or .async() method on the Endpoint to execute the request, or the operation will not be performed.
1val channel = pubnub.channel("channelName")
2
3channel.publish("This SDK rules!").async { result ->
4 result.onFailure { exception ->
5 // Handle error
6 }.onSuccess { value ->
7 // Handle successful method result
8 }
9}
Send file
Upload the file to a specified channel.
This method covers the entire process of sending a file, including preparation, uploading the file to a cloud storage service, and post-uploading messaging on a channel.
For the last messaging step, sendFile internally calls the publishFileMessage method to publish a message on the channel.
The published message contains metadata about the file, such as the file identifier and name, enabling others on the channel to find out about the file and access it.
For details on the method that publishes the file message, see Publish file message.
Method(s)
1pubnub.sendFile(
2 channel: String,
3 fileName: String,
4 inputStream: InputStream,
5 message: Any? = null,
6 meta: Any? = null,
7 ttl: Int? = null,
8 shouldStore: Boolean? = null
9 customMessageType: String
10)
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | Channel for the file. |
fileName *Type: String Default: n/a | Name of the file to send. |
inputStream *Type: InputStream Default: n/a | Input stream with file content. |
messageType: Any? Default: n/a | Message to send along with the file to the specified channel. |
metaType: Any? Default: n/a | Metadata object which can be used with the filtering ability. |
ttlType: Int? Default: n/a | How long the message should be stored in the channel's storage. |
shouldStoreType: Boolean? Default: true | Whether to store the published file message in the channel history. |
customMessageTypeType: String Default: n/a | A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-. Examples: text, action, poll. |
Deprecated parameter
The cipherKey parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.
If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.
Sample code
Reference code
1
Response
1{
2 "timetoken": 15957709330808500,
3 "status": 200,
4 "file": {
5 "id": "d9515cb7-48a7-41a4-9284-f4bf331bc770",
6 "name": "cat_picture.jpg"
7 }
8}
Returns
The sendFile() operation returns a PNFileUploadResult which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
timetoken | Long | A representation of the timetoken when the message was published. |
status | Int | Remote call return code. |
file | PNBaseFile | Uploaded file information. |
PNBaseFile contains the following properties:
| Property Name | Type | Description |
|---|---|---|
id | Long | Id of the uploaded file. |
name | String | Name of the upload file. |
Other examples
Send file with push notification
Trigger push notifications when sharing files
To send mobile push notifications when sharing a file, include pn_apns (for iOS) and/or pn_fcm (for Android) payloads in the message parameter of the sendFile method.
When PubNub detects these reserved keys, it automatically forwards the push notification to the appropriate push service (APNs or FCM) for all devices registered on the channel.
1 val pushPayloadHelper = PushPayloadHelper().apply {
2 // APNS (iOS)
3 apnsPayload = APNSPayload().apply {
4 aps = APNSPayload.APS().apply {
5 alert = mapOf("title" to "New File", "body" to "A file was shared")
6 sound = "default"
7 }
8 apns2Configurations = listOf(
9 APNSPayload.APNS2Configuration().apply {
10 targets = listOf(
11 APNSPayload.APNS2Configuration.Target().apply {
12 topic = "com.yourapp.bundleid"
13 environment = PNPushEnvironment.PRODUCTION
14 }
15 )
show all 48 linesFor more details on push notification payload structure, see the iOS Push and Android Push documentation.
To prevent the sender from receiving their own push notification, add their device token to excluded_devices in the pn_push targets.
List channel files
Retrieve a list of files uploaded to a channel.
Method(s)
1pubnub.listFiles()
2 channel: String,
3 limit: Int,
4 next: String?
5)
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | Channel to get list of files. |
limitType: Int Default: 100 | Number of files to return. Minimum value is 1, and maximum is 100. |
nextType: String? Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
Sample code
1
Response
1{
2 "data":[
3 {
4 "name":"cat_picture.jpg",
5 "id":"fileId",
6 "size":25778,
7 "created":"202007 - 26T13:42:06Z"
8 }],
9 "status": 200
10 "totalCount": 1,
11 "next": null,
12 "prev": null
13}
Returns
The listFiles() operation returns a PNListFilesResult which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
timetoken | Long | A representation of the timetoken when the message was published. |
status | Int | Remote call return code. |
next | String | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
count | Int | Number of files returned. |
data | List<PNUploadedFile> | List of channel files. |
PNUploadedFile contains the following properties:
| Property Name | Type | Description |
|---|---|---|
id | Long | Id of the uploaded file. |
name | String | Name of the upload file. |
size | Int | Size of the uploaded file. |
created | String | Time of creation. |
Get file URL
Generate a URL to download a file from the target channel.
Method(s)
1pubnub.getFileUrl(
2 channel: String,
3 fileName: String,
4 fileId: String
5)
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | Name of channel to which the file has been uploaded. |
fileName *Type: String Default: n/a | Name under which the uploaded file is stored. |
fileId *Type: String Default: n/a | Unique identifier for the file, assigned during upload. |
Sample code
1
Response
1{
2 "url" : http://ps.pndsn.com/v1/files/demo/channels/my_channel/files/fileID/cat_picture.jpg?pnsdk=PubNub-kotlin-Unified/4.32.0×tamp=1595771548&uuid=someUuid
3}
Returns
The getFileUrl() operation returns a PNFileUrlResult which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
url | String | URL to be used to download the requested file. |