2017-09-27 21 views
0

私はプロジェクトに取り組んでいますが、私の "NotiViewController"に私を連れて行く通知を得るのに苦労しています。ときには、それは望むように動作しますが、それは私が最後にあったどのviewControllerにでも私のアプリを開くだけです。希望のviewControllerに通知する通知を受け取るにはどうすればよいですか?

私は一緒にいくつかのオンラインソースコードをハッキング:私は、ユーザーに質問をする通知を送信できるようにしたい

extension QuestionViewController: UNUserNotificationCenterDelegate{ 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: 
    UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> 
    Void) { 
     let vc = storyboard?.instantiateViewController(withIdentifier: "NotiViewControllerID") 
     as! NotiViewController 
     show(vc, sender: Any?.self)   
     completionHandler() 
} 

を、それがクリックされると答えに運ばユーザーを持っています。 ..

例:notificationQuestion:「連続した米国にはいくつの州がありますか?」 =>タップ通知は=>は、適切なNotiViewControllerをロード: "48"

+0

'通知を処理するコードはありません(何も起こりません)。あれは正しいですか? – ColdLogic

+0

はい。私は、そのviewControllerの通知を処理するいくつかのコードを追加しています。通知はNotiViewControllerをロードする必要がありますが、通知に最初に使用されたテキストに使用されたInt変数にはアプリケーションが保持されません。現在の問題の例: *アプリケーションを終了します。 通知がポップアップ:自転車には何本の車輪がありますか? =>タップ通知=> NotiViewControllerをロードする:連続する米国には48の州がある。 – KarmaDeli

答えて

0

は、このコードを試してみてください。おそらく持って表示されている `NotiViewController`が、そこにあるように、私は最後のon`の音が鳴りました何のViewController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    //when application is close 

    if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable : Any] { 
     print(userInfo) 
     self.getBGUserInfo(userInfo: userInfo) 
     // if notification call to redirect perticular page 

    } 
} 
func getBGUserInfo(userInfo : [AnyHashable : Any]) { 
    //get userInfo details here 
    NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil) 
} 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil) 
} 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil) 
} 

//In ViewController which open first while open app 
//In ViewDidLoad 
NotificationCenter.default.addObserver(self, selector: #selector(self.receiveTestNotification(_:)), name: NSNotification.Name(rawValue: "NotificationRedirect"), object: nil) 
//Over 

func receiveTestNotification(_ notification: Notification) { 
    let notName : String = String(describing: notification.name.rawValue) 
    if (notName == "NotificationRedirect"){ 
     //Redirect your NotiViewController 
    } 

} 
+0

ありがとうtrushali ...私は新人プログラマーで4ヶ月しかないので、少し複雑ですが、私はそれを理解しようとしています。私は単に、アプリケーションが終了し、そのまったく同じInt変数をロードした後も持続する特定のInt変数を保存しようとしています。 ここで私は私が通知スケジューラFUNCで変数を保存していると思う: userDefaults.standard.set(intVar、forKey:「savedIntVar」)ここで 私は後に辞めNotiViewControllerにIntVarロードしようとしているが、これによ役に立たない: let x = UserDefaults.standard.object(forKey: "savedIntVar")? Int { notiFreeze = x} – KarmaDeli

+0

実際にはuserdefaultにintvarが保存されません。 –

+0

viewDidLoad関数内からの通知をスケジュールするたびに、何らかの理由で私の乱数変数が保存されていないかのように奇妙でした。それは私がアクションボタンで通知をスケジュールするときにそれが動作するはずです。 通知を管理するのが難しくなりました。アプリが開かれたら、通知センターから通知をクリアする方法を知っていますか? ユーザーが長すぎた場所に滞在した通知をクリックすると、タップするとIntVarの一致するランダム番号が読み込まれないことがあります。通知をクリアするのはすばやく行われます。 – KarmaDeli

関連する問題