2017-06-28 16 views
2

実際に私のアプリで何をしようとしているのですか、私は通知のアラームを設定しています。アプリが強制的に実行されます。通知が来たら、私はそれをタップしています。アプリを開き、開きたい特定のコントローラ(HoroscopeNotificationViewController)を開きません。一方、アプリがバックグラウンドで実行されている場合はアラームを設定した後、通知は、特定のコントローラのオープンと正常に動作します。アプリが終了または終了したときにプッシュ通知をタップした後に特定のコントローラを開く方法

あなたの助けが本当に終了状態または殺された状態に通知のタップ上の特定のビューコントローラを開くには、あなたがアプリで起動オプションのオブジェクトの存在をチェックする必要があり

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
if let _:UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 

     NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(AppDelegate.showNotification), userInfo: nil, repeats: false) 

let firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() 
    firstCategory.identifier = "FIRST_CATEGORY" 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    print("MessagesID : \(userInfo["gcm_message_id"]!)") 
    print(userInfo) 
} 

func application(application: UIApplication, 
       handleActionWithIdentifier identifier:String?, 
              forLocalNotification notification:UILocalNotification, 
                   completionHandler: (() -> Void)) 
{ 

    if (identifier == "FIRST_ACTION") 
    { 
     NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil) 
    } 
    else if (identifier == "SECOND_ACTION") 
    { 
     NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil) 
    } 

    completionHandler() 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    print("MessageId: \(userInfo["gcm_message_id"])") 
    print(userInfo) 
} 

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    if #available(iOS 8.2, *) { 
     print("Function goes here : \(notification.alertTitle)") 
    } else { 
     // Fallback on earlier versions 
    } 
    if notification.category == "FIRST_CATEGORY" { 
     sleep(8) 
     let horoscopeNotificationObj = window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier("HoroscopeNotificationViewController") as! HoroscopeNotificationViewController 
     window?.rootViewController?.presentViewController(horoscopeNotificationObj, animated: true, completion: nil) 
     } 
    } 

func showNotification() { 

    NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil) 
} 
+0

おそらくビューコントローラがまだメモリに読み込まれていません。 View Controllerを表示しようとする前に 'myController.view'を呼び出して、View Controllerを強制的にメモリにロードしてみてください。 – Zhang

+0

Zhang、HoroscopeNotificationViewControllerをロードする場所を教えてください。Appdelegateにありますか?はいの場合、ロードする場所と方法を教えてください。コードの詳細を教えていただければ、私は非常に義務づけられます。 –

+0

あなたはアプリを作った人ではありませんか?あなたはそれがどこにあるかを知るべきです。 Cmd-Shift-Oを押して検索フィールドを表示し、コントローラの名前を入力します。 – Zhang

答えて

1

事前にappreciated..ThanksなりますデリゲートメソッドdidFinishLaunchingWithOptions paramsこれを参照してください:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 
    //launchOptions is not nil 
//if this will not work then call this below method when application root view controller is ready to handle child view transition or just do a sec delay 
       self.application(application, didReceiveLocalNotification: localNotification) 
      } 
+0

このソリューションがあなたのために働くならば、plsは答えを受け入れます@Sirsendu Das – SachinVsSachin

0

私のクエリを解決するために皆さんに感謝します。"NSTimer.scheduledTimerWithTimeInterval(3.0、target:self、selector:#セレクタ(AppDelegate.showNotification)、userInfo:なし、繰り返し:false)"および "func showNotification(){NSNotificationCenter.defaultCenter()。postNotificationName(" "adjustListNotification"、object:nil)} "" 私は、単にコントローラをdidFinishLaunchingWithOptionsから指定されたコントローラにinstantiateViewControllerWithIdentifierを使ってプッシュしてくれました。 もう一度@Sachinと@Zhang

1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

if let localNotifcation: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification { 

     let NotificationInfo = (launchOptions![UIApplicationLaunchOptionsKey.localNotification])! 

    self.application(application, didReceive: NotificationInfo as! UILocalNotification) 
}