0

我想每天两次触发本地通知,例如:- 每天早上 7 点和晚上 6 点,所以任何人都可以帮助我,我该怎么做?

无法设置触发本地通知的自定义时间已经到处查看,但如果有任何帮助将不胜感激,但对于一天两次触发本地通知没有任何帮助

提前致谢 :)

这是我使用本地通知的代码的和平,但它根本没有触发:(

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

3 回答 3

2

问题在于您创建日期的方式:

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

在这里您创建一个日期,11:24 UTC/GMT但您告诉UILocalNotification使用系统时区。Zo 日期偏移到系统时区。

只需将 的设置timeZone为:UILocalNotificationGMT

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

然后UILocalNotification将设置为系统的正确时区并且不会使用偏移量。

另一种选择是不在日期字符串中包含时区,并将其设置NSDateFormatter为包含系统时区。 但是使用上面的代码,即使时区发生变化,它也应该触发。

repeatIntervalof 设置UILocalNotificationNSDayCalendarUnit使其每天重复。

notif.repeatInterval = NSDayCalendarUnit;
于 2013-09-20T12:38:38.023 回答
1

终于找到了解决方案,如何设置UILocalNotification早上 7 点和下午 6 点,并每天重复一遍,希望对正在寻找相同解决方案的人有所帮助

-(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];
    }
}
于 2013-09-21T07:18:01.720 回答
1

您需要设置两个通知,一个早上,一个晚上;两者都将repeatInteval属性设置为NSDayCalendarUnit.

于 2013-09-20T12:31:28.640 回答