Netmera Developer Guide
Ask or search…
K
Comment on page

iOS Inbox

If your application needs information about the push notifications that are previously sent to device by Netmera, you can use NetmeraInbox class to fetch that information from Netmera.
The most common use case for this would be to show the list of notifications inside your application in an inbox-style interface.
NetmeraInbox is the core class providing methods and properties needed for operations on push notifications like fetching push objects or updating push objects' status, but you can not directly initialize a NetmeraInbox instance. You get an instance from SDK, then operate on that instance for future inbox actions. Here is the common workflow to use inbox feature of Netmera.

Determine properties of push notifications to fetch

You must first define filtering properties by creating a NetmeraInboxFilter instance. You determine which push notifications will be included in the fetched list by setting related properties of this NetmeraInboxFilter instance.
NetmeraInboxFilter class provides filtering according to the following options:
  • Inbox Status: Read / Unread / Deleted
  • Categories: Categories to which push notifications are belong.
  • Including expired push notifications or not.
  • Page Size: This is not to filter, but to determine the size of chunks which will be gathered during one request.
Here is a sample code to determine filtering options:
Swift
Objective-C
let filter = NetmeraInboxFilter()
// Default value is NetmeraInboxStatusRead | NetmeraInboxStatusUnread
filter.status = NetmeraInboxStatus.all
// Default value is NSUIntegerMax
filter.pageSize = 20
// Default value is nil
filter.categories = ["category_1", "category_2"]
// Default value is NO
filter.shouldIncludeExpiredObjects = true
NetmeraInboxFilter *filter = [[NetmeraInboxFilter alloc] init];
// Default value is NetmeraInboxStatusRead | NetmeraInboxStatusUnread
filter.status = NetmeraInboxStatusAll;
// Default value is NSUIntegerMax
filter.pageSize = 20;
// Default value is nil
filter.categories = @[@"category_1", @"category_2"];
// Default value is NO
filter.shouldIncludeExpiredObjects = YES;

Fetch the first page and get the NetmeraInbox instance

Now, you can request from Netmera to return the list of push notification objects matching with the filter object using the following code:
Swift
Objective-C
Netmera.fetchInbox(using: filter, completion: {(_ inbox: NetmeraInbox, _ error: Error?) -> Void in
if error != nil {
print("Error : \(String(describing: error?.localizedDescription))")
}
// Store returned inbox object for future operations
// var localInbox: NetmeraInbox? //local inbox definition global variable
localInbox = inbox
})
If fetch operations succeeds, method will call given block with an inbox object which contains the first chunk of push notifications matching with given filter, and a nil error. Otherwise, block is called with a nil inbox parameter and an error object containing details about the reasons of failure.
Now you can present the list of push objects inside your application. You get the list using inbox.objects property, which is an array of NetmeraPushObject instances.
Filter properties of an inbox instance could not be changed. If you need modified filter properties, you have to start a new fetch operation using [Netmera fetchInboxUsingFilter:completion:] method.
[Netmera fetchInboxUsingFilter:filter
completion:^(NetmeraInbox *inbox, NSError *error) {
if(error) {
NSLog(@"Error : %@", [error debugDescription]);
}
// Store returned inbox object for future operations
self.inbox = inbox;
}];

Update the status of push notifications

Push notifications may have 3 different states, which are the following:
  • Unread
  • Read
  • Deleted
These three states allows you to implement a simple notification inbox interface for your users where they can read messages, mark previously read message as unread, delete messages and restore them again if needed.
You can make transitions among states for push notifications inside inbox using -updateStatus:forPushObjects:completion: method. Calling this method will start an asynchronous request to update status for given push objects, and given completion block will be called upon the result of the request.
Here is a sample implementation which deleted the first 5 push objects from inbox:
Swift
Objective-C
let array = [0,1,2,3,4]
let indexSet = IndexSet(array)
let deletedObjects: [Any]? = localInbox?.objects.objects(at: indexSet)
localInbox?.update(NetmeraInboxStatus.deleted, for: deletedObjects as! [NetmeraPushObject], completion: {(_ error: Error?) -> Void in
if error != nil {
print("Error : \(String(describing: error?.localizedDescription))")
}
})
If operation fails for some reason, completion block will be called with a nonnull error parameter describing the reasons of failure.
You can change the status of all the notifications in Inbox with a single method. You can call this method without calling the fetch / inbox method.
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];
NSArray *deletedObjects = [inbox.objects objectsAtIndexes:set];
[self.inbox updateStatus:NetmeraInboxStatusDeleted
forPushObjects:deletedObjects
completion:^(NSError *error) {
if(error) {
NSLog(@"Error : %@", [error debugDescription]);
}
}];

Updating the status of all notifications

You may use this code when you want to update the status of all notifications. For instance, using .(.read) here marks all items as read. To mark all items as unread or deleted, add (.unread) or (.delete) within the parentheses.
Swift
Objective-C
Netmera.update(.read) { (error) in
//TODO:
}
[Netmera updateStatus:NetmeraInboxStatusRead forAllWithCompletion:^(NSError * _Nonnull error) {
// TODO:
}];

Fetch more pages

If you set a custom pageSize value as a filtering option, result of the first fetch operation may not contain all push objects which matches with the given filtering criteria. In this case, you can fetch next chunk of objects using the following code:
Swift
Objective-C
// Fetch next page using `NetmeraInbox` object
localInbox?.fetchNextPage(completionBlock: {(_ error: Error?) -> Void in
if error != nil {
print("Error : \(String(describing: error?.localizedDescription))")
}
})
NetmeraInbox instance returned as the result of -fetchInboxUsingFilter:completion: method stores the fetched list of objects incrementally. Specifically, inbox.objects property will include all list of objects fetched until that time.
For instance, if you set pageSize as 10, and fetch 3 pages in total (one with -fetchInboxUsingFilter:completion:, two with -fetchNextPageWithCompletionBlock:), inbox.objects array will contain all 30 objects in these 3 pages. Therefore, you can solely rely on this array while showing push notifications to your users inside a table view or collection view.
If operation fails for some reason, completion block will be called with a nonnull error parameter describing the reasons of failure.
If you call this method when there is no more page left, method immediately calls completion block with an appropriate error.
You can check if you have fetched all pages via hasNextPage property of NetmeraInbox instance. It will have value NO when all pages have been fetched.
// Fetch next page using `NetmeraInbox` object
[inbox fetchNextPageWithCompletionBlock:^(NSError *error) {
if(error) {
NSLog(@"Error : %@", [error debugDescription]);
}
}];

Get count of push notifications according to status

You can show your users information about total count of push notifications according to inbox status using -countForStatus: method like this:
Swift
Objective-C
// Get the total count of all unread and read push notifications
// existing in Netmera servers for this device
if localInbox?.count(for: NetmeraInboxStatus.unread) != nil && localInbox?.count(for: NetmeraInboxStatus.read) != nil {
let numOfReadOrUnread = (localInbox?.count(for: NetmeraInboxStatus.unread))! + (localInbox?.count(for: NetmeraInboxStatus.read))!
}
// Get the total count of all unread and read push notifications
// existing in Netmera servers for this device
NSUInteger *numOfReadOrUnread = [inbox countForStatus:NetmeraInboxStatusUnread | NetmeraInboxStatusRead];

Light fetching

You can count inbox without fetching all the pushes.
Method
+ (void)fetchInboxCountWithFilter:(NetmeraInboxCountFilter *)filter
completion:(void (^)(NetmeraInboxCountResponse * _Nullable response, NSError * _Nullable error))completionBlock
Swift Example:
let filter = NetmeraInboxCountFilter()
filter.status = {status}
filter.categories = {categoryList}
Netmera.fetchInboxCount(with: filter) { response, error in
if let error = error {
print("inbox count error: \(error)")
} else if let response = response {
print("Read: \(response.count(for: .read)) Unread: \(response.count(for: .unread)) deleted: \(response.count(for: .deleted)) all: \(response.count(for: .all))")
}
}