Comment get une notification locale lorsque l'application est en cours d'exécution dans l'objective c

Je suis nouveau dans iOS et je suis confronté à un problème concernant la notification locale lorsque l'application est en cours d'exécution. Mon code est comme ça

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds. localNotification.alertBody = @"Message hear"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here. localNotification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

J'écris ce code sur le button clic mais il ne me montre pas de notification. Merci d'avance!

Non, il ne montre pas si vous utilisez le code ci-dessus.Tout d'abord, je veux poser quelques questions.

Does your above code work if you use iOS 10?

Where do you call above lines of code in your class or in which method do you apply?

iOS 10 a un nouveau framework appelé UNUserNotification . Nous UNUserNotification importer UNUserNotificationCenterDelegate pour afficher UILocalNotification lorsque l'application est en cours d'exécution.

Le protocole UNUserNotificationCenterDelegate définit des methods pour répondre aux notifications pouvant donner lieu à une action et recevoir des notifications lorsque votre application est au premier plan . Vous affectez votre object délégué à la propriété delegate de l'object UNUserNotificationCenter partagé. L'object Centre de notification user appelle les methods de votre délégué aux moments appropriés pour fournir des informations.

Puis, quand la notification arrive, il appelle willPresentNotification

Si votre application est au premier plan lorsqu'une notification arrive, le centre de notification appelle la méthode ci-dessous

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler; 

La méthode ci-dessus délivre la notification directement à votre application. Si vous implémentez cette méthode, vous pouvez prendre toutes les mesures nécessaires pour traiter la notification et mettre à jour votre application. Lorsque vous avez terminé, exécutez le bloc completionHandler et indiquez comment vous voulez que le système alerte l'user, voire pas du tout. Si votre délégué n'implémente pas cette méthode, le système arrête les alertes comme si vous aviez passé l'option UNNotificationPresentationOptionNone au bloc completionHandler. Si vous ne fournissez aucun délégué pour l'object UNUserNotificationCenter, le système utilise les options d'origine de la notification pour alerter l'user.

How can you show the notification?

Pour y parvenir, nous avons 3 options

 Sound Badge Alert 

UNNotificationPresentationOptions nous aide à mettre en œuvre cette

Ci-dessus est le bon à comprendre et à faire.

J'ai préféré quelques réponses aussi. Elles sont

Afficher la notification iOS lorsque l'application est en cours d'exécution

Afficher une bannière de notification iOS stock lorsque votre application est ouverte et au premier plan?

Obtention de notifications locales à afficher lorsque l'application est au premier plan

Notification lorsque l'application est en avant-plan

Notification dans iOS 10

Notification locale

didReceiveRemoteNotification non appelé, iOS 10

vous devez append ce code

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } 

dans didFinishLaunchingWithOptions

si vous définissez cela, vous devez changer cette ligne

 localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds. 

lorsque vous appuyez sur la notification de jeu de buttons, mais vous ne pouvez pas afficher parce que vous appliquez dans l'état actif et remplacez 0 à 60 intervalle de time et mettez votre fond d'application et vérifiez

essayer comme ça

 NSTimeInterval interval; interval = 60* 60* 12;//12 hours from now UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds. localNotification.alertBody= @"This is message Users will see"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here. localNotification.soundName= UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];