Text Messaging
Sending Messages
The Telephony SDK can handle sending of SMS and MMS messages. Sending messages requires a provisioned phone number, discussed in the phone numbers section of the documentation.
Refer to the Telephony Simulator page for details on sending messages from simulated phone numbers.
SMS Messages
SMS messages can be sent using the sendSMSMessage() method. This method takes a local number, which must be a PhoneNumber, an E.164 formatted remote number and a message body.
let localNumber: PhoneNumber
try! telephonyClient.sendSMSMessage(localNumber: localNumber,
remoteNumber: "+442071838750" , body: "Hi there") { result in
switch result {
case .success(let message):
// message: PhoneMessage (see details below)
case .failure(let error):
// error: SudoTelephonyClientError
}
}// val phoneNumber: PhoneNumber
// val destinationPhoneNumber = "+442071838750"
// val message = "Hi there"
telephonyClient.sendSMSMessage(phoneNumber,
destinationPhoneNumber,
message) { result ->
when (result) {
is Result.Success -> {
// result.value: PhoneMessage
}
is Result.Error -> {
// result.throwable: com.anonyome.telephonysdk.Result.Error
}
}
}The result of successfully sending a message is a PhoneMessage as described below.
MMS Messages
MMS messages can be sent using the sendMMSMessage() method. This is very similar to the method used for SMS but with an additional parameter localUrl: URL for the path to the MMS media file to be sent with the message.
The result of successfully sending a message is a PhoneMessage, described below.
Phone Message
SMS and MMS messages are represented in the Telephony SDK by a PhoneMessage as described below:
Getting Messages
To retrieve SMS and MMS messages, including both sent and received messages, use the getMessage() and getMessages() methods. Messages can be retrieved using either a message ID or the local and remote phone numbers.
Single Message using ID
To retrieve a single message, use the getMessage() method with the message ID. This returns a PhoneMessage object as described above.
Multiple Messages using a Local and Remote Number
To retrieve multiple messages for a specific local and remote number, use the getMessages() method. A limit can be specified for paging, as well as a paging token.
The result of the getMessages() call is a TelephonyListToken as described below.
Get Messages Result
When multiple messages are retrieved, for example through the getMessages() method, a list token object is returned which includes a list of results and a paging token that can be used to retrieve the next set of results.
Conversations
A conversation is a convenient way to retrieve all messages between a specific local number and a specific remote number, sometimes referred to in outside contexts as a message thread or chat history. There are three methods for fetching conversations using different parameters.
Conversation by ID
To retrieve a conversation by ID, use the getConversation(conversationId: String) method.
Conversation by local and remote number
To retrieve a conversation using the local and remote number, use the getConversation(localNumber: PhoneNumber, remoteNumber: String) method.
Conversations by local number
To retrieve the list of conversations for a given local number, use the getConversations method. A limit can be specified for paging, as well as a paging token.
The PhoneMessageConversation type contains a latestPhoneMessage property which can be used to retrieve the local and remote phone numbers involved in the conversation, as well as to display a preview of the conversation contents.
Downloading MMS Message Media
When MMS messages are retrieved, the media data is not automatically downloaded. This gives you control over performance when retrieving MMS messages, particularly those with large media attachments.
To download MMS message media, use the downloadData() method, passing it a MediaObject which can be found in the media property of an MMS message. This method returns the raw media data. Images (jpeg, png, gif) are currently the only supported message media type.
Deleting Messages
To delete a message, the deleteMessage() method is used and requires the message ID.
Subscribing to Message Events
To subscribe to new messages and message status change events, use the subscribeToMessages method. When new messages or message updates occur, the result handler or subscriber you provide will be called with the latest version of the message. On iOS this method returns a SubscriptionToken that cancels the subscription when it's released. On Android a unique ID is passed in to identify the subscriber and can be passed in to the unsubscribeFromPhoneMessages(id: String?) method on Android.
Last updated