I had done an iOS App like this, Searched a lot at last i used local notification. Delayed notification showing time . try this and let me know the results
`func scheduleNotification(date:String , time: String , subject:String) {
var dateString = date+" "+time
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let convertedDate = dateFormatter.date(from: dateString)!
let subtractTime = Calendar.current.date(byAdding: .minute, value: -10, to: convertedDate)
dateString = dateFormatter.string(from: subtractTime!)
var localTimeZoneName: String { return TimeZone.current.identifier }
var secondsFromGMT: Int { return TimeZone.current.secondsFromGMT() }
dateFormatter.timeZone = TimeZone(secondsFromGMT: secondsFromGMT)
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let dateObj:Date = dateFormatter.date(from: dateString)!
print("alaram time : \(dateObj)")
let triggerDaily = Calendar.current.dateComponents([.day,.month,.year,.hour,.minute,], from: dateObj)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let alarmId = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = "your title"
content.body = subject
content.sound = UNNotificationSound.init(named: "your sound filename.mp3")
content.categoryIdentifier = alarmId
let request = UNNotificationRequest(identifier: alarmIdentifier, content: content, trigger: trigger)
print("alarm identi : \(alarmIdentifier)")
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! i had an error: \(error)")
}
}
}`
the above func
is to set notification
func playSound(_ soundName: String) {
//vibrate phone first
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
//set vibrate callback
AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate),nil,
nil,
{ (_:SystemSoundID, _:UnsafeMutableRawPointer?) -> Void in
print("callback", terminator: "") //todo
},
nil)
let url = URL(
fileURLWithPath: Bundle.main.path(forResource: soundName, ofType: "mp3")!)
var error: NSError?
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
} catch let error1 as NSError {
error = error1
audioPlayer = nil
}
if let err = error {
print("audioPlayer error \(err.localizedDescription)")
} else {
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
}
//negative number means loop infinity
audioPlayer!.numberOfLoops = -1
audioPlayer!.play()
}
to play sound pass your mp3 file name also declare audioPlayer as AVAudioPlayer
playSound("Your sound file name")
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
actionSheet.view.tintColor = UIColor.black
actionSheet.addAction(UIAlertAction(title: "Stop Alert", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
self.audioPlayer?.stop()
}))
window?.rootViewController!.present(actionSheet, animated: true, completion: nil)
add these code in UNUserNotificationCenterDelegate
methods also want to import AudioToolBox
and AVFoundation