Skip to content

In app notifications 2.x

Marco Brescianini edited this page Aug 30, 2024 · 4 revisions

Starting from 1.5.0 version the BandyerSDK offers a component, namely InAppNotificationsCoordinator, handling the lifecycle of in-app notifications. If you are using an earlier BandyerSDK version you should take a look at the In-app message notifications guide instead.

Table of contents

Overview

The InAppNotificationsCoordinator component is responsible for managing when in-app notifications should be displayed, where the should appear on screen and respond to user interaction. It provides a convenient API you can use to be notified when a user interacts with one in-app notification. InAppNotificationsCoordinator clients are responsible for enabling or disabling in-app notifications, they are also responsible for updating the user interface of their app, navigating the user to the Bandyer provided app screens when the user interacts with any in-app notification. We will guide you through all the steps required to integrate Bandyer in-app notifications in your app in a moment, but first let us point out what in-app notifications are not. In-app notifications are not a replacement for standard push notifications, nor they are related whatsoever.

The InAppNotificationsCoordinator replaces the MessageNotificationController, if you are upgrading from 1.4.0 version or lower you should take a look at the migration guide showing you how to replace the old MessageNotificationController with the new InAppNotificationsCoordinator.

Start / Stop

In order to start showing in-app notifications to users, the InAppNotificationsCoordinator must be started. You can retrieve an InAppNotificationsCoordinator instance from the BandyerSDK singleton instance, then you can start it calling the start method. We strongly suggest you to do it in one of your main view controllers after the user as signed-in.

The following snippets of code will show you how to start the notifications coordinator:

class MyViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        BandyerSDK.instance().notificationsCoordinator?.chatListener = self
        BandyerSDK.instance().notificationsCoordinator?.fileShareListener = self
        BandyerSDK.instance().notificationsCoordinator?.start()
    }
}
@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    BandyerSDK.instance.notificationsCoordinator.chatListener = self;
    BandyerSDK.instance.notificationsCoordinator.fileShareListener = self;
    [BandyerSDK.instance.notificationsCoordinator start];
}

@end

As you might have noticed we also registered the view controller as an InAppChatNotificationTouchListener and as InAppFileShareNotificationTouchListener. Conforming to those protocols is mandatory if you want to react to in-app notifications being touched by your users. Once started, in-app notifications are enabled so when a new chat message is received or a new file is ready to be downloaded your user will be presented an in-app notification when your user is not facing the chat or file sharing screen. Don't forget to stop the InAppNotificationsCoordinator when you don't wont your user to be bothered with in-app notifications or when your user signs off. To do that the InAppNotificationsCoordinator provides a stop method. The following snippets of code will stop the coordinator when the view controller is dismissed pretending the used signed off.

class MyViewController: UIViewController {
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        BandyerSDK.instance().notificationsCoordinator?.stop()
    }
}
@implementation MyViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [BandyerSDK.instance.notificationsCoordinator stop];
}

@end

Chat notifications

In-app chat notifications are presented to your user when she/he is not facing the ChannelViewController for the chat channel where the message belongs to. Whenever your user touches the notification, the InAppChatNotificationTouchListener onTouch(_ notification: ChatNotification) method is called. You are responsible for handling that interaction presenting a ChannelViewController, if you are not acquainted with this topic please head over to our Opening a chat guide.

The following snippets of code will show you how to respond to a chat notification being touched by a user:

extension MyViewController: InAppChatNotificationTouchListener {
    func onTouch(_ notification: ChatNotification) {
        if presentedViewController is ChannelViewController {
            presentedViewController?.dismiss(animated: true) { [weak self] in
                self?.presentChat(from: notification)
            }
        } else {
            presentChat(from: notification)
        }
    }
    
    private func presentChat(from notification: ChatNotification) {
        guard let intent = OpenChatIntent.openChat(from: notification) else {
            return
        }

	    let channelViewController = ChannelViewController()
	    channelViewController.delegate = self
	    channelViewController.intent = intent
	    // Channel view controller configuration has been omitted to keep this snippet concise and simple. You are required to configure the controller properly before presenting it	    
        present(channelViewController, animated: true)
    }
}
@implementation MyViewController

- (void)didTouchChatNotification:(BDKChatNotification * _Nonnull)notification
{
    if ([self.presentedViewController isKindOfClass:BDKChannelViewController.class])
    {
        [self.presentedViewController dismissViewControllerAnimated:YES completion:^{
            [self presentChatFrom:notification];
        }];
        return;
    }

    [self presentChatFrom:notification];
}

- (void)presentChatFrom:(BDKChatNotification *)notification
{
    BCHOpenChatIntent *intent = [BCHOpenChatIntent openChatFrom:notification];
    BCHChannelViewController *channelViewController = [[BDKChannelViewController alloc] init];
    channelViewController.delegate = self;
    channelViewController.intent = intent;
    // Channel view controller configuration has been omitted to keep this snippet concise and simple. You are required to configure the controller properly before presenting it
    [self presentViewController:channelViewController animated:YES completion:nil];    
}

@end

File sharing notifications

In-app file sharing notifications are presented to your user when she/he is not facing the file sharing screen (file sharing screen is presented from the CallViewController when a call is being performed). Whenever your user touches the notification, the InAppFileShareNotificationTouchListener onTouch(_ notification: FileShareNotification) method is called.

The following snippets of code will show you how to respond to a file sharing notification being touched by a user:

extension MyViewController: InAppFileShareNotificationTouchListener {
    func onTouch(_ notification: FileShareNotification) {
		// Here we are assuming the CallWindow is referenced in MyViewController
        callWindow?.presentCallViewController(for: OpenDownloadsIntent())
    }
}
@implementation MyViewController

- (void)didTouchFileShareNotification:(BDKFileShareNotification *)notification
{
    if (_callWindow)
    {
		// Here we are assuming the CallWindow is referenced in MyViewController    
        [self.callWindow presentCallViewControllerFor:[BDKOpenDownloadsIntent new] completion:^(NSError *_Nullable error) {}];
    }
}

@end

What's next

Clone this wiki locally