2012-11-01 14 views
10

Sto utilizzando NSUserNotification per visualizzare le notifiche. Funziona bene Il problema è che quando si fa clic su una notifica:NSUserNotification - Come aprire l'app quando si fa clic su

  1. Le notifiche di app non vengono rimosse dal centro di notifica.
  2. L'app (quando ridotta a icona) non si apre.

Qualcuno ha familiarità con NSNserNotification che può offrire alcuni suggerimenti?

notice.m

#import "Notice.h" 

@implementation Notice 

- (void) notify:(NSDictionary *)message { 

    NSLog(@"Notification - Show it"); 

    NSUserNotification *notification = [[NSUserNotification alloc] init]; 
    [notification setTitle:[message valueForKey:@"title"]]; 
    [notification setInformativeText:[message valueForKey:@"content"]]; 
    [notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]]; 
    [notification setSoundName:NSUserNotificationDefaultSoundName]; 
    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter]; 
    [center scheduleNotification:notification]; 
} 

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 
{ 

    NSLog(@"Notification - Clicked"); 

    notification=nil; 
    [center removeDeliveredNotification: notification]; 
} 







#pragma mark WebScripting Protocol 

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector 
{ 
    if (selector == @selector(notify:)) 
     return NO; 

    return YES; 
} 

+ (NSString*) webScriptNameForSelector:(SEL)selector 
{ 
    id result = nil; 

    if (selector == @selector(notify:)) { 
     result = @"notify"; 
    } 

    return result; 
} 

// right now exclude all properties (eg keys) 
+ (BOOL) isKeyExcludedFromWebScript:(const char*)name 
{ 
    return YES; 
} 

@end 

Grazie

+0

[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self]; –

risposta

11

Basta implementare l'NSUserNotificationCenterDelegate e definire questo metodo:

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 

Esempio:

Questo è quello che ho fatto in un "notifie" r "applicazione.

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 
{ 
    NSRunAlertPanel([notification title], [notification informativeText], @"Ok", nil, nil); 
} 

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification 
{ 
    notifications=nil; 
    [tableView reloadData]; 
    [center removeDeliveredNotification: notification]; 
} 

Quando la notifica è attivata (clic da parte dell'utente) Ho appena informare l'utente con un pannello (ho potuto utilizzare una finestra HUD) .In questo caso posso rimuovere immediatamente la notifica consegnata, ma questo non è ciò che succede di solito. La notifica potrebbe rimanere lì un po 'di tempo e essere rimossa dopo 1/2 ore (dipende dall'applicazione che stai sviluppando).

+5

Non dimenticare di impostarti come delegato. Dichiarare a nessuno in particolare che si può essere delegati non ottiene nulla se in realtà non si diventa mai delegati. –

+0

Ti dispiacerebbe aggiornare quanto sopra così ho potuto imparare? Sono nuovo e sto provando ad aumentare. Lo apprezzo davvero. – AnApprentice

+0

Inoltre, è possibile rimuovere le notifiche o aprire l'app da una vista nascosta ridotta a icona? Suppongo di aver fatto due domande :) – AnApprentice

Problemi correlati