0

I am making a music-streaming alarm clock. Music streams for the duration selected by the user, including in the background, and at the end of the duration a custom alarm sound is played in addition to a local notification.

In short (1) set timer (2) local notification fires off (3) streamed music stops (4) alarm plays

I am able to do (1) and (2) ok, but am unable to figure out how to trigger (3) or (4) when the app is in the background.

Here is the code for (1) and (2)

let center = UNUserNotificationCenter.current()
var trigger : UNNotificationTrigger!
//...
trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.hour, .minute], from: date), repeats: false)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)

Your help much appreciated.

Edit 1: Timer trial. Only was able to get it to work when app is in foreground.

if let interval = AppDataManager.shared.wakeupDate?.timeIntervalSince(Date.init()) {
      print("interval ", interval)
      self.wakeUpTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { (timer) in
        print("Timer Invoked")
        self.stopPlay()
      }
    } else {
      print("interval NOT AVAILABLE")
    }

Edit 2:

print ("Wakeup date set is \(AppDataManager.shared.wakeupDate)")
        print ("Date now is \(Date())")

    let calendar = Calendar.current
    let unitFlags = Set<Calendar.Component>([ .second])
    let datecomponents = calendar.dateComponents(unitFlags, from: Date(), to: AppDataManager.shared.wakeupDate!)
    let seconds = datecomponents.second

    print(String(describing: seconds))
    let secondsInDouble: Double = Double(seconds!)

      let appDelegate = UIApplication.shared.delegate as! AppDelegate
        if appDelegate.globalTimer == nil {
            print ("Timer started")
            appDelegate.globalTimer = Timer.scheduledTimer(withTimeInterval: secondsInDouble, repeats: false, block: {_ in
                NSLog("Right now the music should stop! ")
                //Function for music stop. This only works in Simulator, or when the app is active in the device
            })
        }
daspianist
  • 5,336
  • 8
  • 50
  • 94
  • https://github.com/mattneub/timerRunsInBackground Download and run. The timer does run in the background. – matt Mar 28 '20 at 17:03
  • 1
    FWIW, that “no background task exists with identifier” message you see is likely the well known [red herring](https://stackoverflow.com/a/60762314/1271826). – Rob Apr 05 '20 at 19:50
  • 1
    No, I assure you that what you are saying is not true. I changed the time interval to 60 and it made no difference; the sound repeats forever, including while the app is in the background and/or the phone is locked, and that's all that matters, because as long as the sound repeats, the timer is able to continue firing. — Naturally if the sound _does_ stop, the timer will also stop, because the background sound is the only thing that keeps the timer going. So now you need to figure out what's stopping the background sound — could be some _other_ app that you have permitted to grab the sound. – matt Apr 05 '20 at 20:28
  • It's been five minutes and the darned thing is still droning on! I wish I'd picked a different sound. :)))) – matt Apr 05 '20 at 20:29
  • I told you that, several times already. – matt Apr 05 '20 at 23:02

0 Answers0