2012-08-03 64 views
2

我有這樣的一段代碼,調用不同的NSLog聲明,這取決於本地通知已收到:如何判斷收到哪些本地通知? Objective - C的

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 
    if (notification == automaticBackupNotification) 
    { 
     NSLog(@"Backup notification received."); 
    } 
    else 
    { 
     NSLog(@"Did receive notification: %@, set for date:%@ .", notification.alertBody, notification.fireDate); 
    } 
} 

而且我用這個方法來安排在另一大類通知:

- (IBAction)automaticValueChanged { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if (automaticSwitch.isOn){ 
     [defaults setValue:@"1" forKey:@"automatic"]; 
     //schedule notification 
     //Set up the local notification 
     appDelegate.automaticBackupNotification = [[UILocalNotification alloc] init]; 
     if(appDelegate.automaticBackupNotification){ 
      //Repeat the notification according to frequency 
      if ([backupFrequencyLabel.text isEqualToString:@"Daily"]) { 
       appDelegate.automaticBackupNotification.repeatInterval = NSDayCalendarUnit; 
      } 
      if ([backupFrequencyLabel.text isEqualToString:@"Weekly"]) { 
       appDelegate.automaticBackupNotification.repeatInterval = NSWeekCalendarUnit; 
      } 
      else { 
       appDelegate.automaticBackupNotification.repeatInterval = NSMonthCalendarUnit; 
      } 

      //Set fire date to alert time 
      NSCalendar *calendar = appDelegate.automaticBackupNotification.repeatCalendar; 
      if (!calendar) { 
       calendar = [NSCalendar currentCalendar]; 
      } 

      NSDateComponents *components = [[NSDateComponents alloc] init]; 
      components.day = 1; 
      //NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0]; 
      //appDelegate.automaticBackupNotification.fireDate = nextFireDate; 
      appDelegate.automaticBackupNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20.0]; 

      //Set time zone to default 
      appDelegate.automaticBackupNotification.timeZone = [NSTimeZone defaultTimeZone]; 

      // schedule notification 
      UIApplication *app = [UIApplication sharedApplication]; 
      [app scheduleLocalNotification:appDelegate.automaticBackupNotification]; 

      NSLog(@"Backup Fire Date: %@", appDelegate.automaticBackupNotification.fireDate); 
     } 
    } 
    else { 
     [defaults setValue:@"0" forKey:@"automatic"]; 
     if(appDelegate.automaticBackupNotification){ 
      [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.automaticBackupNotification]; 
     } 
    } 

    [defaults synchronize]; 
} 

但是,當應用程序委託收到通知時,它觸發條件的'else'部分。有什麼辦法可以告訴不同的通知?或者我做錯了什麼?

乾杯,

Tysin

回答

6

NSNotification對象具有屬性,它被稱爲用戶信息。它是一個NSDictionary,你可以在創建通知的地方設置一些值,並在收到通知的地方檢查它們。

0

我personnaly使用通知名字就知道這通知推送或已收到

//add local observer 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mathodCalled:) name:[NSSTring StringWithFormat :@"plop"] object:nil]; 

... 

//push an event manually 
[[NSNotificationCenter defaultCenter] postNotificationName:eventName object:[NSSTring StringWithFormat :@"plop"]]; 

... 

- (void)methodCalled :(NSNotification*)aNotification{ 
    if([aNotification.name isEqualToString:@"plop"]){ 
     // do something 
    } 

} 
3

只是嘗試設置你的UILocalNotificationuserInfo財產,這樣,

NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"YOUROBJECT" forKey:@"TESTKEY"]; 
YOURNOTIFICATION.userInfo = userDict; 

當UILocalNotification火災這種方法將被稱爲,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    NSDictionary *dict = [notification userInfo]; 
    id obj = [dict objectForKey:@"TESTKEY"]; 
} 

根據您在設置UILocalNotification時設置的userInfo,您可以找出哪個notification被調用。

相關問題