Pergunta

Estou tentando entregar uma notificação local, é mais ou menos assim:

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

Mas a notificação não aparece.Eu sei que às vezes as notificações não são apresentadas se o aplicativo estiver na frente, mas no momento da entrega não é.Nas preferências do sistema, certifiquei-me de que as notificações do meu aplicativo fossem permitidas e até substituí o userNotificationCenter:shouldPresentNotification:método para sempre retornar SIM, mas ainda não apresenta a notificação.

O que é mais confuso é que tudo estava funcionando bem até eu atualizar para o Mavericks.Suponho que algo mudou na atualização, mas não consigo descobrir o quê.

Obrigado pela ajuda.

Foi útil?

Solução

meu palpite é que algo está nil.certifique-se de atribuir um (válido e não nulo) title, ou informativeText.

Imagino que ter valores inválidos para outras propriedades como otherButtonTitle pode impedir que a notificação também seja exibida.

Há alguma mensagem de erro em seu console?

Use o depurador ou NSLog() declarações para avaliar os valores atribuídos à notificação.Você também poderá ver esse problema se o NSUserNotification ponteiro é nulo (não é o caso do código que você postou, mas vale a pena mencionar).

Aqui está um teste mínimo para um delegado de aplicativo que funciona no 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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top