Closed ghost closed 6 years ago
You'll get notified about user action in completion handler, from where you can navigate to preferred view controller
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
Hi @pprevalon ,
Consider CavalcanteLeo reply. It may help you.
Thanks
If im using GLNotificationBar in App delegate I would like to direct the user to another viewController when they tap the notification.