2012-07-03 15 views
9

que estoy tratando de poner en práctica la notificación local deiPhone: notificaciones locales diarias

Esto es lo que yo he puesto

// Current date 
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time 
    NSDate *dateToFire = [date dateByAddingTimeInterval:20]; 

    // Set the fire date/time 
    [localNotification setFireDate:dateToFire]; 
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]]; 

En lugar de 20, quiero poner un tiempo fijo (diario) para iniciar Pulsar.

Por ejemplo: deseo enviar notificaciones emergentes cada 6:00 AM.

¿Cómo puedo hacer eso?

Gracias

Respuesta

27

sólo tiene que crear un objeto adecuadamente NSDate para ser su fecha de fuego (tiempo). En lugar de utilizar [NSDate dateByAddingTimeInterval: 20], usar algo como esto:

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
[components setDay: 3]; 
[components setMonth: 7]; 
[components setYear: 2012]; 
[components setHour: 6]; 
[components setMinute: 0]; 
[components setSecond: 0]; 
[calendar setTimeZone: [NSTimeZone defaultTimeZone]]; 
NSDate *dateToFire = [calendar dateFromComponents:components]; 

Here are the Apple NSDateComponents API docs

Y luego, cuando se agrega la fecha de la notificación, establecer el intervalo de repetición de un día:

[localNotification setFireDate: dateToFire]; 
[localNotification setTimeZone: [NSTimeZone defaultTimeZone]]; 
[localNotification setRepeatInterval: kCFCalendarUnitDay]; 

Al igual que con todos los códigos relacionados con la fecha, asegúrese de probar cómo funciona esto durante el cambio al horario de verano, si su zona horaria utiliza el horario de verano.

+0

en la consola Probé para encontrar la fecha del incendio, que es "2012-07-03 00:30:00 +0000" No estoy seguro, ¿este fuego todos los días? – iscavengers

+0

@iscavengers, Se disparará todos los días si establece el intervalo de repetición en 'kCFCalendarUnitDay', como mostré en el código anterior – Nate

+0

cómo se traducirá a SWIFT? –

3

Supongo que lo que necesita es NSDayCalendarUnit.

Puede consultar this answer. Y here es otro tutorial que vale la pena leer.

1
NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10]; 
    UIApplication* app = [UIApplication sharedApplication]; 

    UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease]; 
    if (notifyAlarm) 
    { 
     notifyAlarm.fireDate = alertTime; 
     notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; 
     notifyAlarm.repeatInterval = 0; 
     notifyAlarm.soundName = @"Glass.aiff"; 
     notifyAlarm.alertBody = @"Staff meeting in 30 minutes"; 

     [app scheduleLocalNotification:notifyAlarm]; 
    } 
Cuestiones relacionadas