-
Notifications
You must be signed in to change notification settings - Fork 0
Notification Center
A popular feature of modern iOS apps is an in-app "Notification Center". The Notification Center provides the user with an easy way to access the notifications delivered by your app from within your app itself. The Rover Campaigns SDK provides a turn-key implementation of a Notification Center that you can add to your application with a few simple steps.
The RoverNotifications module contains a view controller with everything needed to fetch and display the user's notifications in a familiar list view. Additionally it supports functionality for marking notifications as read and allowing the user to delete notifications when they are no longer needed.
The only step required to add the Notification Center to your app is to resolve the view controller and present it in response to some user interaction. The most common implementations are to present the Notification Center modally in response to a button tap or as one of the tabs in a tab bar.
The following example demonstrates how to present the Notification Center in response to a button tap.
class MyViewController: UIViewController {
// An IBAction connected to a UIButton through Interface Builder
@IBAction func presentNotificationCenter(_ sender: Any) {
// Resolve the Notification Center view controller
guard let notificationCenter = RoverCampaigns.shared?.resolve(UIViewController.self, name: "notificationCenter") else {
return
}
present(notificationCenter, animated: true, completion: nil)
}
}
The Notification Center understands when it's being presented modally and automatically includes a "Done" button in the top left corner so you do not have to worry about dismissing the Notification Center manually.
Rover's resolve mechanism doesn't support Interface Builder so to add the Notification Center to a tab bar it must be done programmatically. The below example demonstrates how this can be done from within the viewDidLoad()
method of a UITabBarController
subclass.
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Resolve the Notification Center view controller
guard let notificationCenter = Rover.shared?.resolve(UIViewController.self, name: "notificationCenter") else {
return
}
// Set the notification center's `tabBarItem` which defines how its tab is displayed
notificationCenter.tabBarItem = UITabBarItem(title: "Notifications", image: nil, tag: 0)
// Add the Notification Center to the tab bar's list of view controller's
self.viewControllers?.append(notificationCenter)
}
}
When authoring a campaign in the Rover Campaigns app, you have the option to enable "Notification Center" as one of the ways to deliver the campaign.
When Notification Center is enabled, the notification delivered by the campaign will be accessible in your app's Notification Center in addition to the system-displayed push notification.
The Notification Center uses a standard UITableView
internally. You can override the Rover Campaigns Notification Center to customize the UITableViewCell
that is returned in order to customize the appearance of your Notification Center.
Firstly, have a look in NotificationCell.swift to see what your opportunities for overrides are.
Define your own Notification Cell class like the following with the changes necessary to suit your product spec:
public class MyNotificationCell : NotificationCell {
// ... override methods as you deem fit here.
}
Then you need to override our Notification Center View Controller class in order to specify the use of your custom Cell:
public class MyCustomNotificationCenterViewController : NotificationCenterViewController {
override public func registerReusableViews() {
// Rover by default has a UITableViewCell called NotificationCell. You can replace it here with your own implementation:
tableView.register(MyNotificationCell.self, forCellReuseIdentifier: "notification")
}
}
Then define a custom Rover Assembler to wire up your customized Notification Center to the rest of Rover Campaigns:
public struct CustomNotificationCenterAssembler : Assembler {
public func assemble(container: Container) {
container.register(UIViewController.self, name: "notificationCenter") { resolver in
let presentWebsiteActionProvider: NotificationCenterViewController.ActionProvider = { [weak resolver] url in
resolver?.resolve(Action.self, name: "presentWebsite", arguments: url)
}
return NotificationCenterViewController(
dispatcher: resolver.resolve(Dispatcher.self)!,
eventQueue: resolver.resolve(EventQueue.self)!,
imageStore: resolver.resolve(ImageStore.self)!,
notificationStore: resolver.resolve(NotificationStore.self)!,
router: resolver.resolve(Router.self)!,
sessionController: resolver.resolve(SessionController.self)!,
syncCoordinator: resolver.resolve(SyncCoordinator.self)!,
presentWebsiteActionProvider: presentWebsiteActionProvider
)
}
}
}
Then include it in your call to RoverCampaigns.initialize
:
RoverCampaigns.initialize(assemblers: [
// ... (the other assemblers go here, as usual).
CustomNotificationCenterAssembler() // make sure you put this last in the list!
])