2013-05-01 60 views
0

我想要两个本地通知,两者有不同的时间,比如说我的第一个通知会在1分钟后发出警报,第二个会在2分钟后发出警报。是否有可能在应用程序中有两个本地通知

我试过它在appDelegate中创建两个,但只有第一个给我的通知,而不是第二个。

我该如何做到这一点?

回答

2

是有可能设置两个LocalNotification在任何的iOS应用

请参见下面的方法,通过它可以设定多个LocalNotifications

你只需要所需的参数传递给该方法。

- (void)setAlarmFor:(NSArray*)datesArray forTime:(NSString*)atTime notificationName:(NSString*)name 

{ 

    for(int dayIndex=0;dayIndex <[datesArray count];dayIndex++) 
{ 
    Class cls = NSClassFromString(@"UILocalNotification");// 
    if (cls != nil) { 
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; 
     NSString* dateStr=[datesArray objectAtIndex:dayIndex]; 
     [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 

     NSDate *tempDate = [dateFormatter dateFromString:dateStr]; 
     NSString *tempString = [dateFormatter stringFromDate:tempDate]; 
     tempString = [NSString stringWithFormat:@"%@ %@",tempString,atTime]; 
     [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"]; 

     NSDate *firetAtThisDate = [dateFormatter dateFromString:tempString]; 

     UILocalNotification *localNotif = [[cls alloc] init]; 
     localNotif.fireDate =firetAtThisDate;//here set the Date at which mnotification fire; 
     NSLog(@"Notification date is:%@",firetAtThisDate); 

     localNotif.alertBody =name; 
     localNotif.alertAction = @"Your'Alert message"; 
     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 1; 

     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

     NSDictionary *userDict = [NSDictionary dictionaryWithObject:tempString 
                  forKey:tempString];//by using this we can further cancel the Notification 
     localNotif.userInfo = userDict; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
     [localNotif release]; 
     [dateFormatter release]; 
    } 
    } 


} 

并在AppDelegate类准备动作你想要的通知消防什么

//This Below Line will goes to the Appdelegate DidFinishLaunching Method 

Class cls = NSClassFromString(@"UILocalNotification"); 
if (cls) 
{ 
    UILocalNotification *notification = [launchOptions objectForKey: 
             UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (notification) 
    { 

     //do what you want 
    } 
} 

application.applicationIconBadgeNumber = 0; 

//End of Appdelegate DidFinishLaunching Method. 


    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

application.applicationIconBadgeNumber = 0; 
    //do what you want 

    } 
+0

+1,它可以设置多个报警。 – Ishu 2013-05-01 06:09:49

0

是的,你可以使用你的应用程序中的多个本地通知。

Check this link.

希望它有帮助你

0

肯定的是,你在你的应用程序中使用一个或多个本地通知。试试这个代码在您的项目

-(void) setLocalNotification 
{ 

    NSTimeInterval todayTimeIntervel=[[NSDate date]timeIntervalSince1970]; 
    NSTimeInterval nextOneMinTimeIntervel; 

    nextOneMinTimeIntervel = todayTimeIntervel + 60 ; 

    NSTimeInterval nexttwoMinTimeIntervel; 

    nexttwoMinTimeIntervel = todayTimeIntervel + 60*3; 

    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:nextOneMinTimeIntervel]; 

    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:nexttwoMinTimeIntervel]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 

    [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm a"]; 

    NSString *strDate1 = [dateFormat stringFromDate:date1]; 

    NSString *strDate2 = [dateFormat stringFromDate:date2]; 

    NSArray *arr = [NSArray arrayWithObjects:strDate1,strDate2, nil]; 

    NSArray *titleArr = [NSArray arrayWithObjects:@"First LocalNotification",@"Second LocalNotification", nil]; 

    for (int i =0; i < 2; i++) 
    { 
     NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[arr objectAtIndex:i],@"dateStr",[titleArr objectAtIndex:i],@"title", nil]; 

     [self scheduleLocalNotification:dic]; 
    } 



} 


-(void) scheduleLocalNotification:(NSMutableDictionary*) dic 
{ 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 

    [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm a"]; 

    NSLog(@"%@", [dateFormat dateFromString:[dic objectForKey:@"dateStr"]]); 

    localNotif.fireDate = [dateFormat dateFromString:[dic objectForKey:@"dateStr"]]; 

    localNotif.timeZone = [NSTimeZone systemTimeZone]; 

    localNotif.alertAction = @"View"; 



    localNotif.alertBody = [dic objectForKey:@"title"]; 

     localNotif.userInfo = dic; 

     NSLog(@"value of infoDic %@",dic); 

     localNotif.repeatInterval = NSDayCalendarUnit; 
    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 
0
// get app instance 
    UIApplication *app = [UIApplication sharedApplication]; 
    // create local notif 
    UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 
    if (notification) { 
     NSDate *oneMinuteFromNow = [[NSDate date] dateByAddingTimeInterval:60]; 
     notification.fireDate = oneMinuteFromNow; 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 

     NSString *notificationMessage = @"First"; 
     notification.alertBody = notificationMessage; 
     notification.soundName = UILocalNotificationDefaultSoundName; 

     // schedule notification 
     [app scheduleLocalNotification:notification]; 
     // fire notification right away 
     [app presentLocalNotificationNow:notification]; 
    } 

     UILocalNotification *notification1 = [[[UILocalNotification alloc] init] autorelease]; 
     if (notification1) { 
      NSDate *oneMinuteFromNow1 = [[NSDate date] dateByAddingTimeInterval:120]; 
      notification1.fireDate = oneMinuteFromNow1; 
      notification1.timeZone = [NSTimeZone defaultTimeZone]; 

      NSString *notificationMessage1 = @"Second"; 
      notification1.alertBody = notificationMessage1; 
      notification1.soundName = UILocalNotificationDefaultSoundName; 

      // schedule notification 
      [app scheduleLocalNotification:notification1]; 
      // fire notification right away 
      [app presentLocalNotificationNow:notification1]; 
    } 

通过写这个,你会在2分钟后1min后和第二一赠一个通知。在“didReceiveLocalNotification”方法中,您可以检查通知类型并可以显示警报消息。

希望这会帮助你。

相关问题