Reputation: 4533
I am trying to open popover if application is launched from notification body after app was terminated. I am tying to do it from AppDelegate
. I am using LocalNotifications
. I know how to open specific view if I use action buttons but do not know how to open something if notification body is clicked.
Edit: My solution works only if app is not terminated.
Edit2: Is it right way to do it?:
For simplicity I am trying to open viewController
in code but actually I need alert message, for that I am using JSSAlertView
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if let TappedNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? NSDictionary {
print("The notification is \(TappedNotification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
}
return true
}
I tried this to detect in which state app is but I can't test it if app is terminated and opened from notification:
if application.applicationState == UIApplicationState.Active {
print("App already open")
} else {
print("App opened from Notification")
}
I tried to add this into else{
but it does not open specific view:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
I want the same effect like Twitter or Instagram if notification body is clicked it redirects you to post. But in my case I want just popover(modal).
Upvotes: 1
Views: 317
Reputation: 49730
if your app is terminated and you get the notification and you tap on notification then you get this info by following code and that you need to write code in didFinishLaunchingWithOptions
method:
if let TappedNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
print("The notification is \(TappedNotification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
if your app is not in background mode or active mode you get this way you set a specific rootview controller for the notification. and back you need to change your rootviewcontroller based on your application flow once you are back from notification View Controller.
After review your sample code i fix with following changes:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert , categories: nil))
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
print("The notification is \(notification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
let alert = UIAlertController(title: "Alert", message: "YES NOTIFICITO", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
window!.rootViewController?.presentViewController(alert, animated: true, completion: nil)
return true
}else{
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("first") as UIViewController
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
let alert = UIAlertController(title: "Alert", message: "NO NOTIFICIATION", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
window!.rootViewController?.presentViewController(alert, animated: true, completion: nil)
return true
}
}
Upvotes: 1