首页 文章

当应用程序处于后台时,安排音频通知

提问于
浏览
1

我正在开发类似navi的app .

我必须在预定时间播放声音通知 . 我启用了音频后台模式,但显然我不能使用 NSTimer .

我可以使用什么来在指定时间安排音频通知,因此即使应用程序进入后台也会触发?

2 回答

  • 6

    你需要探索UILocalNotification,使用它可以在特定的时间间隔设置通知,也可以为你的应用程序设置自定义声音,文件see this example for custom sound .

    喜欢

    UILocalNotification *notif = [[UILocalNotification alloc] init];
    NSDateComponents *dateComp = [[NSDateComponents alloc] init];
    [dateComp setDay:YourDay];
    [dateComp setHour:YourHour];
    [dateComp setMinute:YourMinute];
    
    NSLog(@"Year: %i, Month: %i, Day: %i, Time:%i:%i\n",[dateComp year], [dateComp month], 
                        [dateComp day], [dateComp hour], [dateComp minute]);
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    notif.fireDate = [gregorian dateFromComponents:dateComp];
    notif.timeZone = [NSTimeZone defaultTimeZone];
    
    notif.alertBody = YourAlertBody;
    notif.soundName = @"fireburn.caf";
    

    注意:以上代码信用额为this question

  • 5

    您可以使用 UILocalNotification 执行此任务 .

相关问题