UILocalNotification Twice every day

2020-08-06 03:19发布

问题:

I want to fire local notification twice everyday like for ex:- morning 7am and evening 6pm everyday so can anyone please help me how can i do that?

unable to set custom time to fire the local notification have looked every where but nothing helpful for fire local notification twice in a day if any help will appreciate graetly

Thanks in advance :)

this is the peace of code i am using local notification but it's not firing at all :(

- (void)scheduleNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];    
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {       
        NSString *end = @"2013-09-20 11:24:00 +0000";
        NSDate *endDate = [self convertStringToDate:end];
        NSLog(@"end date :%@", endDate);

        UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = endDate;
    notif.timeZone = [NSTimeZone defaultTimeZone];

    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];
    }
}

回答1:

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;


回答2:

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



回答3:

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];
    }
}