2013-11-21 5 views
9

Ich möchte eine UILocalNotification jeden Sonntag um 20 Uhr auslösen, aber ich habe ein Feuer Datum jeden Tag um 20 Uhr.Feuern Sie eine Benachrichtigung an einem bestimmten Tag und Uhrzeit jede Woche

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDate *now = [NSDate date]; 
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now]; 
    [componentsForFireDate setWeekday: 1] ; //for fixing Sunday 
    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
    [componentsForFireDate setMinute:0] ; 
    [componentsForFireDate setSecond:0] ; 

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; 
    UILocalNotification *notification = [[UILocalNotification alloc] init] ; 
    notification.fireDate = fireDateOfNotification ; 
    notification.timeZone = [NSTimeZone localTimeZone] ; 
    notification.alertBody = [NSString stringWithFormat: @"New updates!"] ; 
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"]; 
    notification.repeatInterval= NSDayCalendarUnit ; 
    notification.soundName=UILocalNotificationDefaultSoundName; 

    NSLog(@"notification: %@",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday. 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 

Vielen Dank.

+0

versuchen Sie diese notification.repeatInterval = NSWeekCalendarUnit; – chancyWu

+0

@Chany: Es springt auf die nächste Woche (nächsten Dienstag), aber nicht am Sonntag. Hier ist, was ich mit 'NSWeekCalendarUnit' habe:' next fire date = Dienstag, 26. November 2013 um 20:00 Uhr ' – androniennn

Antwort

12

Sie haben die NSWeekCalendarUnit in der NSDateComponents Init-Funktion verpasst. Fügen Sie die NSWeekCalendarUnit darauf und stellen Sie den repeatInterval-NSWeekCalendarUnit, dann Ausgang ist

next fire date = Sunday, November 24, 2013 at 8:00:00 PM 

Der Code ist hier:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDate *now = [NSDate date]; 
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now]; 
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday 
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
[componentsForFireDate setMinute:0] ; 
[componentsForFireDate setSecond:0] ; 

    //... 
    notification.repeatInterval = NSWeekCalendarUnit; 
    //... 
+0

Vielen Dank. Ich werde niemals ohne dich darüber nachdenken. Vielen Dank! – androniennn

+0

Hallo @chancyWu, ich muss noch einen Tag in der Woche hinzufügen. Für ex müssen Sie Montag und Dienstag jede Woche eine lokale Benachrichtigung erhalten. Was soll ich machen? Vielen Dank im Voraus – TamilKing

4

Ich habe auch darüber gesucht. Unterhalb der Code-Arbeit gut für mich. Übergeben Sie den Wochentag Wert 1 bis 7 Sonntag bis Samstag und Benachrichtigung Körper mit Aktion, die Sie auslösen möchten und geben Sie Ihr Datum, dann Benachrichtigung wird an diesem bestimmten Tag kommen. Die Uhrzeit wird automatisch eingestellt, wenn Sie das Datum einstellen.

Ich hoffe, dies hilft Ihnen.

-(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate]; 
    [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday 
// [componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
// [componentsForFireDate setMinute:0] ; 
// [componentsForFireDate setSecond:0] ; 
    notification.repeatInterval = NSWeekCalendarUnit; 
    notification.fireDate=[calendar dateFromComponents:componentsForFireDate]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 
+0

Was ist, wenn ich [componentsForFireDate setDate: -1]; Minus eins? – kiran

Verwandte Themen