gokulgovind / GLNotificationBar

GLNotificationBar is a ios10 style notification bar, can be used to handle push notification in active state.
MIT License
302 stars 44 forks source link

GLNotificationBar in AppDelegate Presenting other View controllers #8

Closed ghost closed 6 years ago

ghost commented 7 years ago

If im using GLNotificationBar in App delegate I would like to direct the user to another viewController when they tap the notification.

gokulgovind commented 7 years ago

You'll get notified about user action in completion handler, from where you can navigate to preferred view controller

CavalcanteLeo commented 7 years ago

Don't put your logic in the app delegate, I did something similar with inheritance of the ViewController to BaseViewController, then in the BaseVIewController i could do any logic i needed, my code is in objective-c, but it's easy to convert to swift

BaseViewController.h

@interface BaseViewController : UIViewController 
- (void)pushNotification:(NSDictionary *)payload;

BaseViewController.m


#import <GLNotificationBar/GLNotificationBar-Swift.h>

@interface BaseViewController ()
@end

@implementation BaseViewController

- (void)pushNotification:(NSDictionary *)userInfo {
    //disable push if are in Login View Controller
    if ([self isKindOfClass:[LoginViewController class]]) {
        return;
    }

    NSString *title = (userInfo[@"aps"][@"title"]) ? userInfo[@"aps"][@"title"] : @"";
    NSString *message = (userInfo[@"aps"][@"alert"]) ? userInfo[@"aps"][@"alert"] : @"";

    GLNotificationBar * notificationBar = [[GLNotificationBar alloc]
                                           initWithTitle:title
                                           message:message
                                           preferredStyle:GLNotificationStyleDetailedBanner
                                           handler:nil];

    [notificationBar setColorStyle:GLNotificationColorTypeLight];

    [notificationBar addAction:[[GLNotifyAction alloc] nitWithTitle:@"Cancel" style:GLNotificationActionTypeDestructive handler:nil]];

}
@end

AppDelegate.m


#import "BaseViewController.h"
#import "UIViewController+Utils.h"

@implementation AppDelegate

- (void)application:(UIApplication *)application processRemoteNotification:(NSDictionary *)userInfo{
    //Process your Push Notification here
    UIViewController *currentVC = [UIViewController currentViewController]; //used VC Utils to get the current VC
    if ([currentVC isKindOfClass:[BaseViewController class]]) {
        [(BaseViewController *)currentVC pushNotification:userInfo];
    } 
}

@end

but you will also need an ultil to get the current view controller from you appdelegate

UIViewController+Ultils.h

#import <UIKit/UIKit.h>
@interface UIViewController (Utils)
+(UIViewController*) currentViewController;

@end

UIViewController+Ultils.m


#import "UIViewController+Utils.h"

@implementation UIViewController (Utils)

+(UIViewController*) findBestViewController:(UIViewController*)vc {
    if (vc.presentedViewController) {
        return [UIViewController findBestViewController:vc.presentedViewController];

    } else if ([vc isKindOfClass:[UISplitViewController class]]) {

        UISplitViewController* svc = (UISplitViewController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.viewControllers.lastObject];
        else
            return vc;

    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController* svc = (UINavigationController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.topViewController];
        else
            return vc;

    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController* svc = (UITabBarController*) vc;
        if (svc.viewControllers.count > 0)
            return [UIViewController findBestViewController:svc.selectedViewController];
        else
            return vc;
    } else {
        return vc;
    }
}

+(UIViewController*) currentViewController {
    // Find best view controller
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [UIViewController findBestViewController:viewController];

}
@end
gokulgovind commented 6 years ago

Hi @pprevalon ,

Consider CavalcanteLeo reply. It may help you.

Thanks