iOS: si l'application est en arrière-plan et qu'une notification locale est reçue; quelle méthode sera appelée automatiquement?

Mon problème est, si l'application est en arrière-plan et la notification arrive et j'ai ouvert l'application à partir de l' icône ; app restaure les états, mais je veux mettre à jour datatables de l'écran dans ce cas. Est-il possible de mettre à jour datatables en arrière-plan lorsque la notification arrive?

Voici le code que j'utilise pour aborder ce cas:

ViewController.m file ViewController.m :

  - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appIsComingFromBackground:) name:UIApplicationDidBecomeActiveNotification object:nil]; // Do any additional setup after loading the view, typically from a nib. } - (void) appIsComingFromBackground:(NSNotification *) note { // code NSSsortingng *hasMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"alertmsg"]; if([hasMessage length]!=0) { _labelText.text = hasMessage; [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"alertmsg"]; } else{ _labelText.text = @""; } } 

AppDelegate.m file AppDelegate.m :

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (application.applicationState == UIApplicationStateActive) { } else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) { [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"]; } NSLog(@"Alert Message: %@", notification.alertTitle); NSLog(@"Alert Body: %@", notification.alertBody); } 

L'application n'est pas en cours d'exécution

Lorsque l'application n'est pas en cours d'exécution, les users voient les notifications des manières suivantes, en fonction des parameters de notification:

  • Afficher une alerte ou une bannière

  • Badging l'icône de l'application

  • Jouer un son

En appuyant sur le button d'action de la notification, les users lanceront l'application. Dans ce cas, l'application: didFinishLaunchingWithOptions: méthode du délégué de l'application est appelée.

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Handle launching from a notification UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (locationNotification) { // Set icon badge number to zero application.applicationIconBadgeNumber = 0; } return YES; } 

L'application est en cours d'exécution au premier plan

Si l'application est en cours d'exécution pendant la notification, aucune alerte ne s'affiche à l'écran. L'application appelle automatiquement l'application de son délégué: didReceiveLocalNotification: méthode.

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { } // Request to reload table view data [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; // Set icon badge number to zero application.applicationIconBadgeNumber = 0; } 

L'application fonctionne en arrière-plan

L'application a déjà été lancée mais les users passent à une autre application. Lorsque la notification est déclenchée, les users voient généralement une bannière d'alerte en haut de l'écran. Quand il est exploité, l'application sera mise au premier plan. Similaire au cas où l'application s'exécute en premier plan, l'application appelle automatiquement l'application de son délégué: didReceiveLocalNotification: method.

  In Appdelegate - (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { if (application.applicationState == UIApplicationStateActive) { } else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) { // [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"]; [[NSNotificationCenter defaultCenter]postNotificationName:@"PushNotificationReceived" object:nil userInfo:userInfo]; } NSLog(@"Alert Message: %@", notification.alertTitle); NSLog(@"Alert Body: %@", notification.alertBody); } In view controller: -(void)viewDidLoad { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doChangesWhatdidYouWant:) name:@"PushNotificationReceived" object:nil]; } -(void)doChangesWhatdidYouWant:(NSNotification *)notification{ //Todo }