UILocalNotification Twice every day

穿精又带淫゛_ 提交于 2019-12-04 21:01:09

The problem is with the way you create the date:

NSString *end = @"2013-09-20 11:24:00 +0000";
NSDate *endDate = [self convertStringToDate:end];
NSLog(@"end date :%@", endDate);

Here you create a date at 11:24 UTC/GMT but you tell the UILocalNotification to use the system time zone. Zo the date gets offset to the system time zone.

Just set the timeZone of the UILocalNotification to GMT:

notif.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

Then the UILocalNotification will be set to the correct time zone of the system and will not use the offset.

Another option is to not include the time zone in the date string and set the NSDateFormatter to include the system time zone. But with the code above it should fire even if the timezone changes.

Set the repeatInterval of UILocalNotification to NSDayCalendarUnit to make it repeat every day.

notif.repeatInterval = NSDayCalendarUnit;

You need to set two notifications, one for the morning, one for evening; both would have the repeatInteval property set to NSDayCalendarUnit.

Finally Got the solution how to set UILocalNotification for 7am and 6pm and repeat it daily hope it help who are looking fore same solutions on notification

-(void)scheduleNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {      
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        [components setHour:7];
        [components setMinute:0];
        NSDate *today7am = [calendar dateFromComponents:components];

        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = today7am;
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.repeatCalendar = [NSCalendar currentCalendar];
        notif.alertBody = @"Did you forget something?";
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;       
        notif.repeatInterval = NSDayCalendarUnit;

        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];


        NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        [components2 setHour:18];
        [components2 setMinute:0];
        NSDate *today6pm = [calendar2 dateFromComponents:components2];

        UILocalNotification *notif2 = [[cls alloc] init];
        notif2.fireDate = today6pm;
        notif2.timeZone = [NSTimeZone defaultTimeZone];
        notif2.repeatCalendar = [NSCalendar currentCalendar];
        notif2.alertBody = @"Did you forget something2?";
        notif2.alertAction = @"Show me2";
        notif2.soundName = UILocalNotificationDefaultSoundName;
        notif2.applicationIconBadgeNumber = 1;
        notif2.repeatInterval = NSDayCalendarUnit;

        [[UIApplication sharedApplication] scheduleLocalNotification:notif2];
        [notif2 release];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!