在IOS SDK中处理多个本地通知

时间:2013-06-25 10:00:50

标签: iphone ios objective-c uilocalnotification

在我的背景方法中,我按如下方式安排了两个通知。

- (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中发出的通知? 因为我想根据通知做不同的任务。

2 个答案:

答案 0 :(得分:1)

您可以设置userInfo Dictionary

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

答案 1 :(得分:0)

您可以在NSDictionary

userInfo属性中设置UILocalNotification

所以你可以这样做......

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

并比较didReceiveNotification中的关键字“NO”。