iPhone - UILocalNotification as alarm

老子叫甜甜 提交于 2020-01-21 06:08:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!