I’m at the moment studying IOS growth and am creating an alarm app. I’ve seen prior posts on right here explaining how Apps reminiscent of Alarmy use AVAudiosessions and AVAudioplayer to implement their alarm performance, however I’m not seeing how that is doable.
The prior posts I communicate of don’t deal with the right scheduling of the AVAudioplayer in order that it really works as an alarm.
I’ve a present implementation that simply makes use of person notifications, however this isn’t very highly effective because it doesn’t work if the cellphone is on silent mode, and they’re simply quick notifications that don’t make alarm.
`import UIKit
import UserNotifications
import AVFoundation
class ViewController: UIViewController,UNUserNotificationCenterDelegate {
var alarms = [Date]()
let audioSession = AVAudioSession.sharedInstance()
@IBOutlet weak var alarmTimePicker: UIDatePicker!
@IBAction func setAlarm(_ sender: Any) {
print("hit button")
let selectedTime = alarmTimePicker.date
let content material = UNMutableNotificationContent()
content material.title = "ALERT"
content material.physique = "It's your scheduled time."
content material.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: "alarm.wav"))
let parts = Calendar.present.dateComponents([.year,.month,.day,.hour,.minute],from: selectedTime)
let set off = UNCalendarNotificationTrigger(dateMatching: parts, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content material:content material, set off: set off)
UNUserNotificationCenter.present().add(request) {error in
if let error = error {
print("Error")
}else{
DispatchQueue.essential.async{
self.alarms.append(selectedTime)
self.createAlert(withTitle: "Alarm Set for (selectedTime)", andDescription: "Your alarm is about for (self.alarms)")
}
}
}
}
func createAlert(withTitle title:String,andDescription description: String){
let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "Okay" , model: .default) {
_ in print("You set an alert")
}
alert.addAction(okAction)
current(alert,animated: true, completion: nil)
}
func userNotificationCenter(_ middle: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping(UNNotificationPresentationOptions)-> Void){
completionHandler([.alert,.sound])
}
override func viewDidLoad() {
tremendous.viewDidLoad()
UNUserNotificationCenter.present().delegate = self
// Do any extra setup after loading the view.
}
}`
I’ve tried establishing the alarm utilizing AVAudiosessions, nevertheless it doesn’t appear doable to have it work with the Consumer notifications