2013-06-25 32 views
0

在我的Background方法中,我安排瞭如下兩個通知。在IOS SDK中處理多個本地通知

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h 
    [localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer 
    [localNotification setAlertAction:@"Launch"]; //The button's text that launches the application and is shown in the alert 
    [localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text 
    [localNotification setHasAction: YES]; //Set that pushing the button will launch the application 
    [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system 

// * *通知2 * * *

localNotification2 = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h 
     [localNotification2 setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer 
     [localNotification2 setAlertAction:@"Launch"]; //The button's text that launches the application and is shown in the alert 
     [localNotification2 setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text 
     [localNotification2 setHasAction: YES]; //Set that pushing the button will launch the application 
     [localNotification2 setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; //Schedule the notification with the system 

} 

它工作正常的來的通知。

問題

我如何能夠檢測通知進來didreceivenotification method? 因爲我想根據通知來做不同的任務。

回答

1

您可以設置userInfo Dictionary

localNotification1 = [[UILocalNotification alloc] init]; 
localNotification1.userInfo = @{ "type" : @1 }; 
... 
localNotification2 = [[UILocalNotification alloc] init]; 
localNotification2.userInfo = @{ "type" : @2 }; 
... 
... 
0

你可以設置一個UILocalNotification

一個NSDictionaryuserInfo屬性,以便你能做到這樣...

localNotification1.userInfo = [NSDictionary dictionaryWithObject:@"1" forKey:@"NO"]; 
localNotification1.userInfo = [NSDictionary dictionaryWithObject:@"2" forKey:@"NO"]; 

和比較didReceiveNotification中的關鍵「NO」。