iOS Gérer les notifications après le démarrage de l'application de la non-exécution de l'état

J'ai essayé de gérer la réception des notifications dans mon application, mais ce n'est pas vraiment de travail.

Lorsque j'utilise didReceiveLocalNotification:(UILocalNotification *)notification. Je peux recevoir et d'utiliser la notification qui est utilisé pour accéder à l'application, sans aucun problème

Cependant, cette fonction n'est déclenché lorsque l'application est déjà de course (actif, inactif, arrière-plan, et, éventuellement, suspendu, mais je n'ai pas essayé encore).

Maintenant, il y a cette fonction didFinishLaunchingWithOptions:(NSDictionary *)launchOptions où vous pouvez utiliser [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] qui permettrait le retour d'un UILocalNotification.

Toutefois, lorsque vous lancez l'application à partir de non-exécution de état, cet événement n'est pas déclenché. Le LocalNotification puis ouvre l'application, mais je ne peux pas l'utiliser de toute façon.

Maintenant, ma question est: Comment puis-je le faire fonctionner, donc je peux recevoir et de traiter les notifications lors du démarrage de l'application, à partir d'une notification, lorsque l'application n'est pas en cours d'exécution? Est-il peut-être quelque chose que je fais mal?

Ici est un peu un exemple de code à partir de mon application:

Tout d'abord, le didFinishLaunchingWithOptions fonction, qui, malheureusement, ne fonctionne pas. La fonction [sharedLocalNotificationsInstance processNotification:notification] n'est jamais lancé...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {

    //Process the received notification
    [sharedLocalNotificationsInstance processNotification:notification];

    application.applicationIconBadgeNumber = 0;
}

return YES;
}

Et un second morceau de code: Le didReceiveLocalNotification fonction, qui fonctionne parfaitement: je reçois la notification, et [sharedLocalNotificationsInstance processNotification:notification] fonctionne parfaitement.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

//Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];

//Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}
Salut laarsk. J'ai exactement le même problème. j'ai pu reprendre des notifications lorsque l'application est au premier plan ou en arrière-plan, mais de drôles de choses se produire lorsque l'application est fermée. je clique sur la notification, l'application s'ouvre, reste à l'écran pendant 1 seconde, puis l'écran devient noir. je vois application en arrière-plan, mais aucun signe de réception de la notification. Je ne suis pas vraiment sûr de savoir si UILocalNotification* notification = (UILocalNotification*)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationkey]; fonctionne ou pas. Avez-vous trouvé une solution solide pour cela ?

OriginalL'auteur laarsk | 2012-09-05