Skip to main content

Swrve Inbox API

The Inbox API returns a copy of push notifications sent to the user, so the message can be displayed and interacted with inside the application. This guide describes how the Inbox API is presented for iOS, Android, React Native, and Web.

Accessing the API​

To retrieve a list of all messages that the user qualifies for, and are not past their end date:

// add #import "SwrveSDK-Swift.h" to your imports
NSArray* pushInboxMessages = [SwrveSDK pushInboxMessages];

The array contains a collection of SwrvePushInboxMessage items. Each of these is an entry in the app’s notification inbox.

The array pushInboxMessages contains a collection of SwrvePushInboxMessage items. Each of these is an entry in the app’s notification inbox.

tip

To access Swrve's Inbox APIs, you must upgrade your apps to use Swrve's **iOS 9.1.0+ **and **Android 10.16.0+ **SDKs, Swrve React Native 5.1.0+ SDK, or Swrve Web 3.0.0+ SDK.

Inbox message properties​

The SwrvePushInboxMessage object includes the following properties:

ParameterDescription
messageIdThe unique identifier of the campaign. (Integer)
variantIdThe unique identifier of the notification's campaign variant. (Integer)
stateThe read/unread state of the campaign. (SwrvePushInboxMessageState)
sentDateThe time the notification was sent to the user.
Note: This is the time the corresponding remote notification is sent to the user, not the time the notification is downloaded to the device. (Integer)
endDateThe end date of the campaign. Cached messages that are past their endDate will be filtered out by the API. (Integer)
customerJsonThe inbox message's content. (NSDictionary)

Using the API​

Here are examples of how to retrieve and use the message properties:

NSArray *pushInboxMessages = [SwrveSDK pushInboxMessages];
for (SwrvePushInboxMessage *message in pushInboxMessages) {
long sentDate = message.sentDate; // when the notification was sent
SwrvePushInboxMessageState state = message.state; // current read or unread state
if (state == SwrvePushInboxMessageStateUNREAD ){
// show message UI as unread
}
NSDictionary *json = message.customerJson; // your notification content should be here
// do something with each message such as building a UI list of messages to display
}

Inbox message lifecycle examples​

The following examples illustrate how to use the Inbox API to manage the message lifecycle in your app.

Message state​

To retrieve the state associated with each inbox message entry, use the following:

SwrvePushInboxMessageState state = message.state

Possible values are:

ValueDescription
SwrvePushInboxMessageStateUNREADMessage has not been marked as read by the user.
SwrvePushInboxMessageStateREADMessage has been marked as read by the user.

Tracking user interactions & managing message state​

There are two API's available to change the state of a message from *"*unread" to *"*read", and another API to delete the message. All API's generate events, which are used by Swrve to calculate the inbox metrics in the corresponding campaign report.

The APIs take a messageId, found in the SwrvePushInboxMessage object, and a SwrvePushInboxDelegate callback (iOS) or SwrvePushInboxListener callback (Android).

React Native takes only a messageId and returns a Promise. The Promise resolves with a map containing SwrvePushInboxListener property values.

Web takes a messageId and an optional listener callback, and returns a Promise that resolves with an ISwrvePushInboxResult. The Promise resolves with an error result code rather than rejecting, so you can check the outcome from either the returned result or the listener.

Check the SUCCESS result code in the callback for successful updates. If there is an ERROR result code, then use the httpResponseCode to determine if it should be retried.

Mark message as "Read"​

Here's an example of how to mark a message as read using the readPushInboxMessage API:

[SwrveSDK readPushInboxMessage:message.messageId listener:self]; // where self conforms to protocol SwrvePushInboxDelegate
...
- (void)onComplete:(UInt64)messageId result:(SwrvePushInboxResult *)result {
if (result.resultCode == SwrvePushInboxResultCodeSUCCESS) {
// operation was successful
} else {
// operation was not successful. Check the http code in the result object
NSInteger httpResponseCode = result.httpResponseCode;
}
}

The readPushInboxMessage may send a read event, used internally for reporting.

Track engagements​

The engagePushInboxMessage API is intended to track a user’s direct engagement with the message. Use of the API implies that the user has read the message, so calling the API changes the message’s state from *"*unread" to "read".

[SwrveSDK engagePushInboxMessage:message.messageId listener:self]; // where self conforms to protocol SwrvePushInboxDelegate
...
- (void)onComplete:(UInt64)messageId result:(SwrvePushInboxResult *)result {
if (result.resultCode == SwrvePushInboxResultCodeSUCCESS) {
// operation was successful
} else {
// operation was not successful. Check the http code in the result object
NSInteger httpResponseCode = result.httpResponseCode;
}
}

The engagePushInboxMessage will send an engagement event and may send a read event, used internally for reporting.

Delete message​

Finally, to delete the message from visibility so it doesn’t appear in the inbox again, call the deletePushInboxMessage API.

[SwrveSDK deletePushInboxMessage:message.messageId listener:self]; // where self conforms to protocol SwrvePushInboxDelegate
...
- (void)onComplete:(UInt64)messageId result:(SwrvePushInboxResult *)result {
if (result.resultCode == SwrvePushInboxResultCodeSUCCESS) {
// operation was successful
} else {
// operation was not successful. Check the http code in the result object
NSInteger httpResponseCode = result.httpResponseCode;
}
}

The deletePushInboxMessage API will send a delete event, used internally for reporting.

Checking for Updates​

Configure the pushInboxUpdateListener (iOS and Web) and setPushInboxUpdateListener (Android and React Native) callback APIs to receive updates when the Push Inbox Messages content has changed.

The SwrvePushInboxUpdateDelegate, set via pushInboxUpdateListener API, is a delegate callback. Here's an example of how to configure the callback:

// add #import "SwrvePushInboxUpdateDelegate.h" to your imports
[SwrveSDK pushInboxUpdateListener:self]; // where self conforms to protocol SwrvePushInboxUpdateDelegate
...
- (void) messagesUpdated {
// refresh UI
}

The refreshCampaignsAndResources API can also be used to force a refresh from the server. Maintaining message state across devices The SDK informs Swrve of message state changes, which are stored to the user's Swrve ID. When the SDK requests a new list of campaigns (e.g. on app launch), Swrve returns the latest state value for each message. This ensures that the the message's state is consistently reflected across all of the user's devices.


General use case​

Use the NSArray of SwrvePushInboxMessage items that the [SwrveSDK pushInboxMessages] call returns and present them in an inbox or other custom view.

The message.customerJson (iOS, React Native, and Web) or message.getCustomerJson() (Android) returns a json object with the inbox content defined in the campaign (see Notification inbox). Build a UI to present this content to the user, and execute custom actions when the user interacts with it.

When the user indicates they’ve read a specific message, use the readPushInboxMessage API to update the message’s state to “read”.

When the user engages directly with the message's content, call the engagePushInboxMessage API to capture the engagement and update the message’s state to “read”.

When the user deletes a message, call the deletePushInboxMessage API to permanently remove the message from the user's inbox.