如何设置多个UILocalNotification

时间:2014-09-25 10:40:28

标签: ios objective-c iphone uilocalnotification

在我的应用中,我正在使用UILocalNotifications。我想为一周中的不同日期设置通知。为此,我在数组中有不同的日期。但我得到了错误的结果。我的代码有什么问题吗?我的代码是

for(int counter=0 ;counter<[daysArray count]; counter++)
        {
            int day = [[daysArray objectAtIndex:counter] intValue];
            NSDate *specificDate = [self getDateOfSpecificDay:day];

            UILocalNotification *localNotification = [[UILocalNotification alloc]init];
            localNotification.fireDate = specificDate;
            localNotification.repeatInterval = NSWeekdayCalendarUnit;
            localNotification.soundName = sound;
            localNotification.alertBody = [NSString stringWithFormat:@"%@",specificDate];
            localNotification.alertAction = @"Show me the item";
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            NSLog(@"%@",localNotification);
        }

2 个答案:

答案 0 :(得分:0)

我想你在这里滥用了行localNotification.repeatInterval = NSWeekdayCalendarUnit;

它会让您的通知在一周中的每一天重复,这会在您的案例中造成问题。请删除此行,然后重试它会有效。

答案 1 :(得分:0)

我们做了类似于你的问题的事情,但我不知道它会对你有所帮助。只是试试这个** NSWeekdayCalendarUnit **我建议你只是参考我的答案。

NSCalendar * gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit|NSWeekCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

NSArray *dobArray = [NSArray arrayWithObjects:@"02-08-2014", @"10-08-2014", @"14-08-2014", @"15-08-2014", @"16-08-2014", @"17-08-2014", @"18-08-2014", @"22-08-2014", @"28-08-2014", @"29-08-2014",nil];
NSArray *messagesArray = [NSArray arrayWithObjects:@"Literacy And Numeracy workshop for parents and children", @"raksha Bandhan", @"Tri Colour Dat(School celebration)", @"Independence day (School Holiday)", @"PTM/Book Exhibition", @"Janmastami", @"Parsi New Year- School Holiday", @"Clown Day", @"Eco Friendly Ganesha-School Holiday", @"Ganesh Chaturthi-School Holiday",nil];
for (NSString *dobStr in dobArray)
{
    NSArray *components = [dobStr componentsSeparatedByString:@"-"];
    if(components.count>2) {
        NSInteger aDay = [[components objectAtIndex:0] integerValue];
        NSInteger aMonth = [[components objectAtIndex:1] integerValue];
        // NSInteger aYear = [[components objectAtIndex:2] integerValue];

        if(aDay == [dateComponent day] && aMonth == [dateComponent month]) { // dob is here
            [dateComponent setDay:aDay];
            [dateComponent setMonth:aMonth];
            [dateComponent setYear:[dateComponent year]];
            [dateComponent setHour:16];
            [dateComponent setMinute:54];

            UIDatePicker *dp = [[UIDatePicker alloc] init];
            [dp setDate:[gregCalendar dateFromComponents: dateComponent]];

            UILocalNotification *notification = [[UILocalNotification alloc] init];
            NSInteger index = [dobArray indexOfObject:dobStr];

            // [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
            [notification setAlertBody:[messagesArray objectAtIndex:index]];

            [notification setFireDate:dp.date];
            [notification setTimeZone:[NSTimeZone defaultTimeZone]];
            [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
            return YES;
        }
    }
}