Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序处于后台时处理本地通知_Ios_Swift_Background_Uilocalnotification - Fatal编程技术网

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

Ios 在应用程序处于后台时处理本地通知,ios,swift,background,uilocalnotification,Ios,Swift,Background,Uilocalnotification,基本上,我正在安排一个本地通知,以便在特定时间(比如上午8:00)敲响闹钟。现在我想执行一个特定的任务,比如在应用程序处于后台时播放警报声,但不点击通知列表中收到的通知横幅 我正在使用下面的代码,但它只在我点击通知横幅并进入应用程序时起作用 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHan

基本上,我正在安排一个本地通知,以便在特定时间(比如上午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])
}

每次通知呼叫特定声音文件更改??或使用自定义声音文件??请告诉我,您只能预先准备最长30秒的声音文件,如果系统使您的应用程序不活动,则可能无法处理/唤醒应用程序。我认为您应该使用通知服务扩展在后台处理本地通知。如果您只想设置自定义通知声音,那么您可以通过“content.sound=UNNotificationSound(名为:“out.caf”)”@himanshutatel用户可以根据自己的选择更改声音。您可以参考此链接[用于通知服务扩展集成。感谢您的回答,但这部分工作正常。当应用程序位于后台时,我需要播放警报声,但不需要点击通知的通知横幅。@SahilDhiman reference like()