首页 > 解决方案 > 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)
        }
    }
}

标签: iosswiftaudiouilocalnotificationalarm

解决方案



推荐阅读