【问题标题】:UILocalNotification Twice every dayUILocalNotification 每天两次
【发布时间】:2013-09-21 15:25:48
【问题描述】:

我想每天两次触发本地通知,例如:- 每天早上 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];
    }
}

【问题讨论】:

    标签: iphone uilocalnotification


    【解决方案1】:

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

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

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

    只需将UILocalNotificationtimeZone 设置为GMT

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

    那么UILocalNotification会被设置为系统正确的时区,不会使用偏移量。

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

    UILocalNotificationrepeatInterval 设置为NSDayCalendarUnit,使其每天重复。

    notif.repeatInterval = NSDayCalendarUnit;
    

    【讨论】:

    • 我在 GMT 5:30,所以在 05:54:00 使用 11:24 发出通知
    • 但请记住,世界各地的人都可以使用您的应用程序,并且偏移量与您的不同。所以尽量让你的代码在任何时区都能工作。
    • 好的,我同意,但是我如何设置任何修复时间来触发通知?如果有任何工作代码,那将有很大帮助,但有固定时间,因为我想设置每天重复的固定时间通知
    • 我的例子可以工作,只需创建一个带有 GMT 的日期,然后将 timeZone 设置为 GMT。
    • 与格林威治标准时间的日期?我怎样才能创建它?
    【解决方案2】:

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

    【讨论】:

    • 我同意,但为什么这段代码不起作用?如果您有任何代码可以在某个特定时间触发通知,那么这将是一个很大的帮助
    【解决方案3】:

    终于找到了解决方案,如何将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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多