2017-09-27 1 views
0

Je travaille sur un projet mais j'ai du mal à obtenir la notification pour m'amener à mon "NotiViewController". Parfois, cela fonctionne comme vous le souhaitez, mais d'autres fois, il suffit d'ouvrir mon application à quel que soit ViewController j'étais dernier.Comment puis-je obtenir la notification pour m'amener au viewController souhaité?

Je piraté ce code ainsi que quelques sources en ligne:

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

Je veux être en mesure d'envoyer une notification qui demande à l'utilisateur une question, une fois qu'il est cliqué que l'utilisateur prises pour une réponse.

par exemple notificationQuestion: "Combien y a-t-il d'états dans les États-Unis contigus?" => Tap notification => charge le NotiViewController approprié: "48"

+0

'tout viewController ma dernière on' sonne comme il y a un' NotiViewController' affiché, ce qui a probablement pas de code pour gérer la notification (donc rien ne se passe). Est-ce exact? – ColdLogic

+0

Oui. J'ai ajouté du code pour gérer la notification sur ce viewController et les choses ont commencé à fonctionner mais maintenant si je quitte l'application. la notification charge NotiViewcontroller, comme il se doit, mais l'application ne tient pas sur la variable Int qui a été utilisée pour le texte qui a été utilisé dans la notification pour commencer. Exemple de problème actuel: * quitte l'application. une notification apparaît: Combien de roues les bicyclettes ont-elles? => notification de notification => charge NotiViewController avec: Il y a 48 états dans les États-Unis contigus. – KarmaDeli

Répondre

0

Essayez ce code:

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

Merci trushali ... Je suis un programmeur novice en iOs seulement 4 mois, donc c'est un peu complexe mais j'essaie de comprendre. J'essaye simplement de sauvegarder la variable Int particulière qui persiste après que l'application se soit arrêtée et charge cette même variable Int. Ici, je pense que je sauve la variable dans le planificateur de notification func: userDefaults.standard.set (intVar, forKey: « savedIntVar ») Ici, je suis en train de charger IntVar dans le NotiViewController après cesser de fumer, mais pas de résultat: si laissez x = UserDefaults.standard.object (forKey: "savedIntVar") comme? Int { notiFreeze = x} – KarmaDeli

+0

que se passe-t-il réellement n'est-il pas sauvegardé dans l'userdefault? –

+0

C'était étrange, pour une raison quelconque, lorsque j'ai programmé la notification depuis la fonction viewDidLoad, c'était comme si ma variable de nombre aléatoire n'était pas enregistrée. Cela fonctionne comme il se doit lorsque je programme la notification dans un bouton d'action. Il a été difficile de contrôler les notifications. Connaissez-vous un moyen d'effacer la notification du centre de notification une fois l'application ouverte ?? Si l'utilisateur clique sur une notification restée trop longtemps, il se peut qu'il ne charge pas le numéro aléatoire correspondant pour IntVar. Effacer la notification serait une solution rapide. – KarmaDeli