ios - Xcode Swift:当用户在后台收到本地通知时如何播放音频
问题描述
您好我正在制作一个警报应用程序,并且我正在尝试在用户收到本地通知时播放音频。我将所有本地通知功能和管理保存在一个名为 NotificationPublisher 的快速文件中。当用户将应用程序放在前台时它可以工作,因为 willPresent() 函数被调用,但是我试图让它在后台工作/电话关闭。
当本地通知出现在后台而不仅仅是前台时,是否有一个函数会被调用?
如果不是我处理这个功能是错误的。
这是我的 sendNotification 函数,我在其中接收警报并安排本地通知。(在我的 NotificationPublisher.swift 中)
func sendNotification(alarm : Alarm, badge: Int?) {
let notificationContent = UNMutableNotificationContent()
notificationContent.title = alarm.alarmName
notificationContent.subtitle = alarm.alarmTime + " " + alarm.alarmPeriod
notificationContent.body = "Click here to open the app and click the solve button!"
if let badge = badge {
var currentBadgeCount = UIApplication.shared.applicationIconBadgeNumber
currentBadgeCount += badge
notificationContent.badge = NSNumber(integerLiteral: currentBadgeCount)
}
notificationContent.sound = UNNotificationSound.default
UNUserNotificationCenter.current().delegate = self
var hour = ""
if(alarm.alarmTime.count == 4){
hour = String(alarm.alarmTime.prefix(1))
} else {
hour = String(alarm.alarmTime.prefix(2))
}
let minute = String(alarm.alarmTime.suffix(2))
var intHour = Int(hour)!
if(alarm.alarmPeriod == "PM" && intHour != 12){
intHour += 12
} else if(alarm.alarmPeriod == "AM" && intHour == 12) {
intHour = 0
}
var dateComponents = DateComponents()
if(alarm.alarmDays.filter{$0}.count == 0) {
dateComponents.hour = intHour
dateComponents.minute = Int(minute)!
dateComponents.timeZone = .current
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) // Repeating Alarm
let request = UNNotificationRequest(identifier: alarm.alarmKey, content: notificationContent, trigger: trigger) // replace trigger with delaytimetrigger and vice versa for exact time
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print(error.localizedDescription)
}
}
} else {
dateComponents.hour = intHour
dateComponents.minute = Int(minute)!
dateComponents.timeZone = .current
let days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
for i in alarm.alarmDays.enumerated() {
if(i.element){
dateComponents.weekday = i.offset + 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: alarm.alarmKey + days[i.offset], content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print(error.localizedDescription)
}
}
}
}
}
}
这是我从 willPresent() 函数调用的 ViewController.swift 中的音频函数。
func alarmSound() {
print("alarm sound was called")
if let player = player, player.isPlaying {
player.stop()
} else {
let urlString = Bundle.main.path(forResource: "buzzer", ofType: "mp3")
do {
try AVAudioSession.sharedInstance().setMode(.default)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
guard let urlString = urlString else { return }
player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: urlString))
guard let player = player else { return }
player.numberOfLoops = -1
player.play()
} catch {
print(error)
}
}
}
解决方案
推荐阅读
- javascript - Javascript 10 天 - 第 3 天:数组 - HackerRank
- c++ - 如何从文件夹视图中删除组
- python - 根据 Keras 中的图像和子文件夹名称设置 X 和 y 用于训练 CNN
- c++ - QSqlDatabasePrivate::addDatabase: 重复连接名称 'qt_sql_default_connection'
- r - 如何消除“as.Date.OtherDate(death) 中的错误:外部函数调用中的 NAs (arg 4)”
- python - 第一个项目,pi 上的 Python 3,似乎一开始就无法正常运行
- php - 根据选择的支付网关的邮政编码拒绝 WooCommerce 结帐
- loops - 无法使用字符串循环打开和读取 Fortran 中的文件夹和文件
- r - 我可以将历史市值从网站导入 R 吗?
- python - 在烧瓶项目中导入模块的位置