Message Actions API for Cocoa Swift SDK

This SDK has been replaced by a new PubNub Swift SDK written purely in Swift. Check it out here

Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message reaction independently or when they fetch original messages.

Terminology

All message reactions in code are collectively called message reactions in the docs and the term applies both to reacting to a message (through emojis) and acting on a message (by deleting it).

Add Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action on a published message. Returns the added action in the response.

Method(s)

To Add a Message Reaction you can use the following method(s) in the Swift SDK:

open func addMessageAction(with: PNAddMessageActionRequest, completion: PNAddMessageActionCompletionBlock?)
* required
ParameterDescription
request *Add message reaction request with all information about new message reaction which will be passed to PubNub service.
closure
Type: PNAddMessageActionCompletionBlock
Add message reaction request completion closure.

PNAddMessageActionRequest

* required
ParameterDescription
type *
Type: String
What feature this message reaction represents. Maximum 15 characters.
value *
Type: String
Value which should be added with message reaction type.
channel *
Type: String
Name of channel which stores the message for which action should be added.
messageTimetoken *
Type: NSNumber
Timetoken (PubNub's high precision timestamp) of message to which action should be added.

Basic Usage

let request = PNAddMessageActionRequest(channel: "chat", messageTimetoken: 1234567890)
request.type = "reaction"
request.value = "smile"

self.client.addMessageAction(with: request, completion: { status in
if !status.isError {
/**
* Message reaction successfully added.
* Created message reaction information available here: status.data.action
*/
} else {
if status.statusCode == 207 {
// Message reaction has been added, but event not published.
} else {
/**
show all 23 lines

Response

Response objects which is returned by client when add message reaction Message Reaction API is used:

open class PNAddMessageActionData : PNServiceData {
// Added message reaction.
open var action: PNMessageAction? { get }
}

open class PNAddMessageActionStatus : PNAcknowledgmentStatus {
// Add message reaction request processed information.
open var data: PNAddMessageActionData { get }
}

Add Message Reaction (Builder Pattern)

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action on a published message. Returns the added action in the response.

Method(s)

To Add a Message Reaction you can use the following method(s) in the Swift SDK:

addMessageAction()
.channel(String)
.messageTimetoken(NSNumber)
.type(String)
.value(String)
.performWithCompletion(PNAddMessageActionCompletionBlock?)
* required
ParameterDescription
channel *
Type: String
Name of channel which store message for which action should be added.
messageTimetoken *
Type: NSNumber
Timetoken of message for which action should be added.
type *
Type: String
What feature this message reaction represents.
value *
Type: String
Value which should be stored along with message reaction.
closure
Type: PNAddMessageActionCompletionBlock
Add message reaction request completion closure.

Basic Usage

self.client.addMessageAction()
.channel("chat")
.name(1234567890)
.type("reaction")
.value("smile")
.performWithCompletion({ status in
if !status.isError {
/**
* Message reaction successfully added.
* Created message reaction information available here: status.data.action
*/
} else {
if status.statusCode == 207 {
// Message reaction has been added, but event not published.
} else {
show all 24 lines

Response

Response objects which is returned by client when add message reaction Message Reaction API is used:

open class PNAddMessageActionData : PNServiceData {
// Added message reaction.
open var action: PNMessageAction? { get }
}

open class PNAddMessageActionStatus : PNAcknowledgmentStatus {
// Add message reaction request processed information.
open var data: PNAddMessageActionData { get }
}

Remove Message Reaction

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a peviously added action on a published message. Returns an empty response.

Method(s)

To Remove a Message Reaction you can use the following method(s) in the Swift SDK:

open func removeMessageAction(
with request: PNRemoveMessageActionRequest,
completion: PNRemoveMessageActionCompletionBlock?
)
* required
ParameterDescription
request *Remove message reaction request with information about existing message reaction.
closure
Type: PNRemoveMessageActionCompletionBlock
Remove message reaction request completion closure.

PNRemoveMessageActionRequest

* required
ParameterDescription
actionTimetoken *
Type: NSNumber
Message reaction addition timetoken.
channel *
Type: NSString
Name of channel which store message for which action should be removed.
messageTimetoken *
Type: NSNumber
Timetoken (PubNub's high precision timestamp) of message to which reaction should be removed.

Basic Usage

let request = PNRemoveMessageActionRequest(channel: "chat", messageTimetoken: 1234567890)
request.actionTimetoken = 1234567891

self.client.removeMessageAction(with: request, completion: { status in
if !status.isError {
// Message reaction successfully removed.
} else {
/**
* Handle remove message reaction error. Check 'category' property to find out possible
* issue because of which request did fail.
*
* Request can be resent using: status.retry()
*/
}
})

Response

Response objects which is returned by client when remove message reaction Message Reaction API is used:

open class PNErrorData : PNServiceData {
// Stringified error information.
open var information: String { get }
}

open class PNAcknowledgmentStatus : PNErrorStatus {
// Whether status object represent error or not.
open var isError: Bool { get }

// Additional information related to error status object.
open var data: PNErrorData { get }
}

Remove Message Reaction (Builder Pattern)

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a peviously added action on a published message. Returns an empty response.

Method(s)

removeMessageAction()
.channel(String)
.messageTimetoken(NSNumber)
.actionTimetoken(NSNumber)
.performWithCompletion(PNRemoveMessageActionCompletionBlock?)
* required
ParameterDescription
channel *
Type: String
Name of channel which store message for which reaction should be removed.
messageTimetoken *
Type: NSNumber
Timetoken of message for which reaction should be removed.
actionTimetoken *
Type: NSNumber
Reaction addition timetoken.
closure
Type: PNCreateSpaceCompletionBlock
Remove message reaction request completion closure.

Basic Usage

self.client.removeMessageAction()
.channel("channel")
.messageTimetoken(1234567890)
.actionTimetoken(1234567891)
.performWithCompletion({ status in
if !status.isError {
// Message reaction successfully removed.
} else {
/**
* Handle remove message reaction error. Check 'category' property to find out possible
* issue because of which request did fail.
*
* Request can be resent using: status.retry()
*/
}
show all 16 lines

Response

Response objects which is returned by client when remove message reaction Message Reaction API is used:

open class PNErrorData : PNServiceData {
// Stringified error information.
open var information: String { get }
}

open class PNAcknowledgmentStatus : PNErrorStatus {
// Whether status object represent error or not.
open var isError: Bool { get }

// Additional information related to error status object.
open var data: PNErrorData { get }
}

Get Message Reactions

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Get a list of message reactions in a channel. Returns a list of actions sorted by the action's timetoken in ascending order.

Method(s)

To Get Message Reactions you can use the following method(s) in the Swift SDK:

open func fetchMessageActions(
with request: PNFetchMessageActionsRequest,
completion: PNFetchMessageActionsCompletionBlock
)
* required
ParameterDescription
request *Fetch message reactions request with all information which should be used to fetch existing messages actions.
closure *
Type: PNFetchMessageActionsCompletionBlock
Fetch message reactions request completion closure.

PNFetchMessageActionsRequest

* required
ParameterDescription
start *
Type: NSNumber
Return values will be less than start.
end *
Type: NSNumber
Return values will be greater than or equal to end.
limit *
Type: UInt
Number of messages actions to return in response.
channel *
Type: String
Name of channel from which list of message reactions should be retrieved.

Basic Usage

self.client.fetchMessageActions()
.channel("chat")
.start(1234567891)
.limit(200)
.performWithCompletion({ (result, status) in
if !(status?.isError ?? false) {
/**
* Message reactions successfully fetched.
* Result object has following information:
* result.data.actions - list of message reaction instances
* result.data.start - fetched messages actions time range start (oldest message
* action timetoken).
* result.data.end - fetched messages actions time range end (newest action timetoken).
*/
} else {
show all 23 lines

Response

Response objects which is returned by client when fetch message reactions Message Reaction API is used:

open class PNFetchMessageActionsData : PNServiceData {
// List of fetched messages actions.
open var actions: [PNMessageAction] { get }

/**
* Fetched messages actions time range start (oldest message reaction timetoken).
*
* This timetoken can be used as 'start' value to fetch older messages actions.
*/
open var start: NSNumber { get }

// Fetched messages actions time range end (newest action timetoken).
open var end: NSNumber { get }
}

show all 19 lines

Error response which is used in case of Message Reaction API call failure:

open class PNErrorData : PNServiceData {
// Stringified error information.
open var information: String { get }
}

open class PNErrorStatus : PNStatus {
// Whether status object represent error or not.
open var isError: Bool { get }

// Additional information related to error status object.
open var data: PNErrorData { get }
}

Get Message Reactions (Builder Pattern)

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Get a list of message reactions in a channel. Returns a list of actions sorted by the action's timetoken in ascending order.

Method(s)

To Get Message Reactions you can use the following method(s) in the Swift SDK:

fetchMessageActions()
.channel(String)
.start(NSNumber)
.end(NSNumber)
.limit(UInt)
.performWithCompletion(PNFetchMessageActionsCompletionBlock)
* required
ParameterDescription
channel *
Type: String
Name of channel from which list of messages actions should be retrieved.
start
Type: NSNumber
Message reaction timetoken denoting the start of the range requested. Return values will be less than start.
end
Type: NSNumber
Message reaction timetoken denoting the end of the range requested. Return values will be greater than or equal to end.
limit
Type: UInt
Number of message reactions to return in response.
closure *
Type: PNFetchMessageActionsCompletionBlock
Fetch message reactions request completion closure.

Basic Usage

self.client.fetchMessageActions()
.channel("chat")
.start(1234567891)
.limit(200)
.performWithCompletion({ (result, status) in
if !(status?.isError ?? false) {
/**
* Message reaction successfully fetched.
* Result object has following information:
* result.data.actions - list of message reaction instances
* result.data.start - fetched messages actions time range start (oldest message
* action timetoken).
* result.data.end - fetched messages actions time range end (newest action timetoken).
*/
} else {
show all 23 lines

Response

Response objects which is returned by client when fetch message reactions Message Reaction API is used:

open class PNFetchMessageActionsData : PNServiceData {
// List of fetched messages actions.
open var actions: [PNMessageAction] { get }

/**
* Fetched messages actions time range start (oldest message reaction timetoken).
*
* This timetoken can be used as 'start' value to fetch older messages reactions.
*/
open var start: NSNumber { get }

// Fetched messages actions time range end (newest action timetoken).
open var end: NSNumber { get }
}

show all 19 lines

Error response which is used in case of Message Reaction API call failure:

open class PNErrorData : PNServiceData {
// Stringified error information.
open var information: String { get }
}

open class PNErrorStatus : PNStatus {
// Whether status object represent error or not.
open var isError: Bool { get }

// Additional information related to error status object.
open var data: PNErrorData { get }
}
Last updated on