Satyam
Satyam

Reputation: 15894

iPhone - UILocalNotification as alarm

Even when my iPhone application is in background, How can I use UILocalNotification to show my alram every day at 8.00 PM?

Upvotes: 1

Views: 3129

Answers (3)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

UILocalNotification *localNotification =[[UILocalNotification alloc]init];

 NSCalendar *calendar=[NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone defaultTimeZone]];

    unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;

    NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];

    comp.hour=8;
    comp.minute=0;
    comp.second=0;

    NSDate *date=[[calendar dateFromComponents:comp]dateByAddingTimeInterval:0];

    if (localNotification==nil) {
        return;
    }

    localNotification.fireDate=date;
    localNotification.timeZone=[NSTimeZone defaultTimeZone];
    localNotification.repeatCalendar=[NSCalendar currentCalendar];

    localNotification.alertBody=@"Good Morning dude..!";

    localNotification.alertAction=@"Snooze";

    localNotification.repeatInterval=NSDayCalendarUnit;

    localNotification.soundName=@"goodmorning.caf";

[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

I hope this will help you...!

Upvotes: 0

MyCSharpCorner
MyCSharpCorner

Reputation: 1313

dasdom answer is correct. just wanted to add to it that the alarm will not make a sound if your device is in silent mode. It is a restriction from Apple for UILocalNotification.

Upvotes: 2

dasdom
dasdom

Reputation: 14063

Set the fireDate to 8.00 PM and set the repeatInterval to NSDayCalendarUnit and schedule the alert with [[UIApplication sharedApplication] scheduleLocalNotification: myNotification];

Upvotes: 3

Related Questions