问题
Even when my iPhone application is in background, How can I use UILocalNotification to show my alram every day at 8.00 PM?
回答1:
Set the fireDate
to 8.00 PM and set the repeatInterval
to NSDayCalendarUnit
and schedule the alert with [[UIApplication sharedApplication] scheduleLocalNotification: myNotification];
回答2:
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.
回答3:
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...!
来源:https://stackoverflow.com/questions/6696346/iphone-uilocalnotification-as-alarm