简体   繁体   中英

Wake up application in background using AudioSession like Alarmy iOS app

I am trying to wake application in background. I have seen one app which is waking app in background when local notification arrives. App name is Alarmy - https://itunes.apple.com/us/app/alarmy-alarm-clock/id1163786766?mt=8

I have checked apple documentation for background modes - https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

I found one hint in that to launch app in background - "For audio apps, the audio framework needs the app to process some data. (Audio apps include those that play audio or use the microphone.)" but i didn't find any technical assistance on that.

Regarding Alarmy app I found 2 or 3 questions here and all of them saying that they are playing silent sound with AVAudioSession category playback. I did try that but it's not helping me to awake app when local notification comes.

Till now i did infinite loop of silent audio and also set up audio session but it's not working to wake the app.

let audioSession = AVAudioSession.sharedInstance()
    try? audioSession.setCategory(.playback, mode: .default, options: .mixWithOthers)
    try? audioSession.setActive(true)

audioPlayer = try! AVAudioPlayer.init(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Birthday_6", ofType: "mp3")!))
    audioPlayer?.numberOfLoops = -1
    audioPlayer?.play()

If anyone have idea on AVAudioSession to wake the app like location services then please let me know.

From my observation, Alarmy app is setting up x number of local notification for the specified time in which the alarm is set.

They are setting this x number of local notifications, when the app goes into background. And invalidates all pending notifications when the app comes into foreground. This is a loop, which will keep on repeating when the app comes to foreground and goes back into background.

On termination, they trigger one notification for that moment which asks the user to keep the app open in background along with the x number of notifications for the specified time as mentioned earlier.

When the app is not in foreground, the x number of notification gets fired on the alarm time, with a specific time interval in between each notifications, which is the length of the sound that is being played, this gives a continuity to the music or sound of the notifications that are being triggered.

When the alarm fires up, if the app is running, it will ring like a regular alarm until it is dismissed. If you click on anything other than dismiss, or force closes the app during this time, the earlier mentioned cycle will be triggered and will keep on making the sound using the local notifications, until the dismiss button in the app is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM