如果在应用程序终止后从通知主体启动应用程序,则打开弹出窗口

Open popover if application is launched from notification body after app was terminated

如果应用程序在应用程序终止后从通知正文启动,我正在尝试打开弹出窗口。我打算从 AppDelegate 开始。我正在使用 LocalNotifications。如果我使用操作按钮,我知道如何打开特定视图,但如果单击通知正文,我不知道如何打开某些内容。

编辑:我的解决方案只有在应用程序未终止时才有效。

Edit2: 这样做正确吗?:

为简单起见,我试图在代码中打开 viewController,但实际上我需要警告消息,因为我正在使用 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

}

我试过这个来检测应用程序处于哪个状态,但是如果应用程序被终止并从通知中打开,我无法测试它:

if application.applicationState == UIApplicationState.Active {
    print("App already open")

} else {
    print("App opened from Notification")
}

我试图将其添加到 else{ 但它没有打开特定视图:

让 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()

我想要与 Twitter 或 Instagram 相同的效果,如果单击通知正文,它会将您重定向到 post。但就我而言,我只想要 popover(modal).

如果您的应用程序终止并且您收到通知并点击通知,那么您可以通过以下代码获取此信息,并且您需要在 didFinishLaunchingWithOptions 方法中编写代码:

    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()

    }

如果您的应用程序未处于后台模式或活动模式,您可以通过这种方式为通知设置特定的根视图控制器。回来后,一旦您从通知视图控制器返回,您需要根据您的应用程序流程更改您的 rootviewcontroller。

查看您的示例代码后,我通过以下更改进行了修复:

 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
        }

    }