0

我想要两个本地通知,两者都有不同的时间,假设我的第一个通知将在 1 分钟后发出警报,第二个将在 2 分钟后发出警报。

我已经尝试在 appDelegate 中创建两个,但只有第一个给我通知,而不是第二个。

我怎样才能做到这一点?

4

4 回答 4

2

是 可以在任何 iOS 应用程序中设置两个 LocalNotification

请参阅下面的方法,您可以通过该方法设置多个 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

  }
于 2013-05-01T05:26:47.987 回答
0

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

检查此链接。

希望对你有帮助

于 2013-05-01T05:27:28.787 回答
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];
}
于 2013-05-01T05:30:04.313 回答
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];
    }

通过写这篇文章,您将在 1 分钟后收到一个通知,在 2 分钟后收到第二个通知。在 " didReceiveLocalNotification" 方法中,您可以检查通知类型并显示警报消息。

希望这会帮助你。

于 2013-05-01T05:34:55.853 回答