当应用程序处于后台时处理本地通知

Handle local notification when app is in background

基本上,我正在安排一个本地通知,以便在特定时间响起我的闹钟,比如 8:00 上午。现在我想执行一个特定的任务,比如当应用程序在后台时播放警报声,但不点击我在通知列表中收到的通知横幅。

我正在使用以下代码,但它仅在我点击通知横幅并进入应用程序时有效。

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: 
 UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
 {
   print("notification recived in background state")
 }

请帮助我解决无需点击横幅即可处理本地通知的棘手解决方案。

感谢提前。

设置放置在项目包中的声音文件

let arrayOfSoundFile = ["water.caf","hello.mp3"]
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Title..",  arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Text Here...", arguments: nil)   

//Custom sound file as par user requriment select file UIPickerView add and select file 

content.sound = UNNotificationSound(named: arrayOfSoundFile[0])
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 50, repeats: false)
let identifier = "WakeUp"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if error != nil {
    print("notification created successfully.")
}
})

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    let userInfo = notification.request.content.userInfo
    let dictAps = userInfo["aps"] as! NSDictionary
    print(dictAps)
    completionHandler([.alert, .badge, .sound])
}