J'ai une application iPhone dans laquelle j'ajoute des notifications push.
Lorsque je reçois la notification push, j'ai besoin d'aller à une vue particulière où je charge une vue de table après avoir appelé un Service Web ici. Le problème est quand je suis debout dans le même point de vue. Si j'ai reçu un message push, je dois recharger la vue de la table à la fois au premier plan et en arrière-plan. Mais quand je fais cela, cela ne fonctionne pas correctement. Comment puis-je atteindre cela? P>
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"########################################didReceiveRemoteNotification****************************###################### %@",userInfo); // Check application in forground or background if (application.applicationState == UIApplicationStateActive) { if (tabBarController.selectedIndex==0) { NSArray *mycontrollers = self.tabBarController.viewControllers; NSLog(@"%@",mycontrollers); [[mycontrollers objectAtIndex:0] viewWillAppear:YES]; mycontrollers = nil; } tabBarController.selectedIndex = 0; //NSLog(@"FOreGround"); //////NSLog(@"and Showing %@",userInfo) } else { tabBarController.selectedIndex = 0; } }
3 Réponses :
Vous pouvez essayer avec la notification locale Lorsqu'une notification push est reçue, enfoncez la notification locale. Dans votre contrôleur d'affichage, écoutez la notification locale et effectuez la tâche. P> par exemple: p> dans votre DidreceiveremotNotification: p> nsnotificationCenter code> pour recharger votre table lors de la réception d'une notification push.
- (void)reloadTable:(NSNotification *)notification
{
[yourTableView reloadData];
}
Cela a fonctionné parfaitement, je me suis connecté à Stackoverflow juste pour vous uppoter. Ne connaissez jamais les observateurs de ios, merci
Merci beaucoup!!!! Je l'utilise pour afficher une notification personnalisée lors de la réception d'une notification et d'une application à distance est au premier plan. Vous enregistrez ma journée: D +1!
Vous n'avez pas besoin de supprimer l'observateur dans la viewwilldisappear aussi?
@Carmine: Oui vrai..vous devriez .. c'est juste une idée. Quoi qu'il en soit, j'ai édité la réponse afin que cela puisse être utile. Merci
SWIFT 3 Version:
dans Didreceiveremotenotification P>
NotificationCenter.default.removeObserver("reloadTheTable")
Swift 4 version: Dans DidreceiveremotENotification
NotificationCenter.default.addObserver(self, selector: #selector(onReloadEventsTable), name: Notification.Name(rawValue: "reloadEventsTable"), object: nil)