Using Inbox Service
Inbox Service is responsible for managing messages in the Inbox. The inbox is a simple one-way delivery system that allows you to deliver messages to the user.
Note: Before using Inbox Service, you need to have a PowerAuthSDK
object available and initialized with a valid activation. Without a valid PowerAuth activation, the service will return an error.
Inbox Service communicates with the Mobile Token API.
Creating an Instance
The preferred way of instantiating Inbox Service is via WultraMobileToken
class.
See: Example Usage
Customized initialization
If you need to create a more customized instance, such as when your Push Service uses a different enrollment server URL than other services in the SDK, you can use an initializer. Simply define the networking configuration and provide the PowerAuthSDK instance.
import WultraMobileTokenSDK
import WultraPowerAuthNetworking
let networkingConfig = WPNConfig(
baseUrl: URL(string: "https://powerauth.myservice.com/enrollment-server")!,
sslValidation: .default
)
let networkingService = WPNNetworkingService(
powerAuth: powerAuth,
config: networkingConfig,
serviceName: "InboxService",
acceptLanguage: "en"
)
let opsService = WMTInbox(networking: networkingService)
Inbox Service Usage
Get Number of Unread Messages
To get the number of unread messages, use the following code:
inboxService.getUnreadCount { result in
switch result {
case .success(let count):
if count.countUnread > 0 {
print("There are \(count.countUnread) new message(s) in your inbox")
} else {
print("Your inbox is empty")
}
case .failure(let error):
print("Error \(error)")
}
}
Get a List of Messages
The Inbox Service provides a paged list of messages:
// First page is 0, next 1, etc...
inboxService.getMessageList(pageNumber: 0, pageSize: 50, onlyUnread: false) { result in
switch result {
case .success(let messages):
if messages.count < 50 {
// This is the last page
}
// Process result
case .faulure(let error):
// Process error...
}
}
To get the list of all messages, call:
inboxService.getAllMessages { result in
switch result {
case .success(let messages):
print("Inbox contains the following message(s):")
for msg in messages {
print(" - \(msg.subject)")
print(" * ID = \(msg.id)")
}
case .failure(let error):
print("Error \(error)")
}
}
Get Message Detail
Each message has its unique identifier. To get the body of the message, use the following code:
let messageId = messagesList.first!.id
inboxService.getMessageDetail(messageId: messageId) { result in
switch result {
case .success(let detail):
print("Received message:")
print("\(detail.subject)")
print("\(detail.body)")
case .failure(let error):
print("Error \(error)")
}
}
Set Message as Read
To mark the message as read by the user, use the following code:
let messageId = messagesList.first!.id
inboxService.markRead(messageId: messageId) {
switch result {
case .success:
print("OK")
case .failure(let error):
print("Error \(error)")
}
}
Alternatively, you can mark all messages as read:
inboxService.markAllRead {
switch result {
case .success:
print("OK")
case .failure(let error):
print("Error \(error)")
}
}
Error handling
Every error produced by the Inbox Service is of a WMTError
type. For more information see detailed error handling documentation.