NetmeraInbox allows you to access and manage previously sent push notifications in an inbox-style interface. You cannot instantiate NetmeraInbox directly; instead, you must obtain an instance through the SDK and use it to interact with push notifications.
Filter and Fetch Notification
Create a NetmeraInboxFilter instance to specify which push notifications to fetch. You can filter by:
Status: Read, Unread, or Deleted.
Categories: Filter by specific categories.
Expired Notifications: Include or exclude expired notifications.
Page Size: Number of notifications to fetch per request.
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
Request the list of push notifications matching the filter:
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 nilerror. Otherwise, block is called with a nilinbox 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 Notification Status
Push notifications can have three statuses:
Unread
Read
Deleted
You can update the status of notifications to allow users to mark messages as read, unread, or deleted.
You can update the status of all notifications in the Inbox with a single method, without needing to call the fetch or inbox method.
Examples:
Update the status of a single notification:
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 there are more notifications than the defined page size, fetch the next page:
// Fetch next page using `NetmeraInbox` object
localInbox?.fetchNextPage(completionBlock: {(_ error: Error?) -> Void in
if error != nil {
print("Error : \(String(describing: error?.localizedDescription))")
}
})
// Fetch next page using `NetmeraInbox` object
[inbox fetchNextPageWithCompletionBlock:^(NSError *error) {
if(error) {
NSLog(@"Error : %@", [error debugDescription]);
}
}];
Incremental Fetching
The NetmeraInbox instance returned from -fetchInboxUsingFilter:completion: keeps the list of fetched notifications in inbox.objects. As you fetch more pages, new notifications are added to this list.
For example, if you set a page size of 10 and fetch 3 pages, inbox.objects will contain all 30 notifications from the 3 pages. This allows you to display the entire list of notifications in a table or collection view.
Error Handling
If the operation fails, the completion block will be triggered with an error describing the failure. If there are no more pages to fetch, the method will immediately call the completion block with an appropriate error.
Checking if All Pages are Fetched
You can check if there are more pages to fetch by using the hasNextPage property of the NetmeraInbox instance. If it returns false, it means all pages have been fetched.
Push Notification Counts
To get the total count of notifications based on status:
// 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
To get the count without fetching all push notifications:
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))")
}
}
You can define Message Categories under Settings > Message Category in the panel.
User Category Preferences
To retrieve the user's category preferences, use the following method. It returns a list of preferences with each entry containing the ID, name, and enable status:
Netmera.getUserCategoryPreferenceList { categories, error in
if let categories = categories {
print(categories)
} else if let error = error {
print(error)
}
}
Managing Category Preferences
To manage category preferences and switch their status, use this method. Provide the category ID and set the categoryEnabled parameter to true or false
Netmera.setUserCategoryPreferenceWithCategoryId("{categoryId}", categoryEnabled: {true/false}) { error in
// Handle the result or error accordingly
...
}