0

たとえば、毎日午前 7 時と午後 6 時のようにローカル通知を毎日 2 回起動したいので、どうすればそれを行うことができるか教えてください。

ローカル通知を起動するカスタム時間を設定することはできませんが、ローカル通知を 1 日に 2 回起動するのに役立つものは何もありませんでした。

前もって感謝します :)

これは私がローカル通知を使用しているコードの平和ですが、まったく起動していません:(

- (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ように指示します。UILocalNotificationZo 日付は、システム タイム ゾーンにオフセットされます。

のをに設定するだけtimeZoneです:UILocalNotificationGMT

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

次に、UILocalNotificationはシステムの正しいタイム ゾーンに設定され、オフセットは使用されません。

もう 1 つのオプションは、日付文字列にタイム ゾーンを含めずNSDateFormatterに、システムのタイム ゾーンを含めるように設定することです。 ただし、上記のコードを使用すると、タイムゾーンが変更されても起動するはずです。

を に設定して、毎日repeatInterval繰り返す ようにします。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 に答える