ios-如何在应用程序处于活动和非活动状态时使用自定义声音发出警报和小睡

ios-how to make alarm with custom sound and snooze when app is active and inactive both

我已经搜索过,我找到了 UILocalNotification,但它仅在应用程序未处于活动状态且我无法实现自定义声音和小睡时有效。这是我的代码

UILocalNotification* alarm = [[UILocalNotification alloc] init];
    alarm.fireDate = [myPicker date];
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.hasAction = YES;
    alarm.alertAction = @"SNOOZE";
    alarm.repeatInterval = 0;
    alarm.soundName =  @"Notification.aif";
    alarm.alertBody = @"Alarm";
    //[app scheduleLocalNotification:alarm];
     [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

感谢帮助。

您的方法完全正确。但我以为你忘记了一些关于 UILocalNotification 的事情。

阅读documents

UILocalNotification 对象指定应用可以安排在特定日期和时间显示的通知。操作系统负责在预定时间发送本地通知; 应用程序不必是 运行 才能发生这种情况

谢谢,我在 didReceiveLocalNotification

中将 UILocalNotification 与我自己的声音名称一起使用,在应用程序处于活动状态时以另一种方式播放它
 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
      NSLog(@"Received notification");
        // Play a sound and show an alert only if the application is active, to avoid doubly notifiying the user.
        if ([application applicationState] == UIApplicationStateActive) {
            NSLog(@"active");

          NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alarm2" ofType:@"wav"];
            NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_mySound);
            AudioServicesPlaySystemSound(self.mySound);
        }

    }