Question

J'essaie d'envoyer une notification locale, elle ressemble à ceci :

NSUserNotification *notification = [[NSUserNotification alloc] init];
//set title, subtitle, and sound
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

Mais la notification n'apparaît pas.Je sais que parfois les notifications ne sont pas présentées si l'application est au premier plan, mais ce n'est pas le cas au moment de la livraison.Dans les préférences système, je me suis assuré que les notifications de mon application étaient autorisées et j'ai même remplacé le userNotificationCenter:shouldPresentNotification :méthode pour toujours renvoyer OUI mais elle ne présente toujours pas la notification.

Ce qui est le plus déroutant, c'est que tout fonctionnait bien jusqu'à ce que je mette à jour vers Mavericks.Je suppose que quelque chose a changé dans la mise à jour mais je n'arrive pas à comprendre quoi.

Merci pour l'aide.

Était-ce utile?

La solution

je suppose qu'il y a quelque chose nil.assurez-vous que vous attribuez un (valide et non nul) title, ou informativeText.

J'imagine qu'avoir des valeurs invalides pour d'autres propriétés telles que otherButtonTitle pourrait également empêcher l’affichage de la notification.

Y a-t-il des messages d'erreur dans votre console ?

Utilisez le débogueur ou NSLog() déclarations pour évaluer les valeurs attribuées à la notification.Vous pourriez également constater ce problème si le NSUserNotification le pointeur est nul (pas le cas d'après le code que vous avez publié, mais mérite d'être mentionné).

Voici un test minimal pour un délégué d'application qui fonctionne sur Mac OS X 10.9 (Mavericks) :

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
    unc.delegate = self;

    NSUserNotification* notice = [[NSUserNotification alloc] init];
    notice.title = @"title"; // notifications need a title, or informativeText at minimum to appear on screen
    //notice.informativeText = @"informativeText";

    NSLog(@"notification: %@, notification center:%@", notice, unc);
    [unc deliverNotification:notice];
}

// The notifications will always dispaly even if we are in the foreground
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
    return YES;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top