Home > DeveloperSection > Forums > set multiple UILocalNotifications in ios
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    October-28-2014 3:00 AM

 iPhone iPhone  Ios  Ios 7 
Ratings:


 1 Reply(s)
 726  View(s)
Rate this:
In my app I am using UILocalNotifications. I want to set notifications for different days of the week.For that, I have different dates in an array. But I am getting wrong results. Is there anything wrong with my code.
My code is:
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);
        }


Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    October-28-2014 3:02 AM

We have done something similiar to your question but i dont know it will help you or not..just try this.

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];
        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 setAlertBody:[messagesArray objectAtIndex:index]];
            [notification setFireDate:dp.date];
            [notification setTimeZone:[NSTimeZone defaultTimeZone]];
            [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
            return YES;
        }
    }
}

Modified On Mar-17-2016 04:13:09 AM

Don't want to miss updates? Please click the below button!

Follow MindStick