使用AlarmSession之类的AudioSession在后台唤醒应用程序 [英] Wake up application in background using AudioSession like Alarmy iOS app

查看:73
本文介绍了使用AlarmSession之类的AudioSession在后台唤醒应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在后台唤醒应用程序.我已经看到一个应用程序,它在本地通知到达时在后台唤醒应用程序.应用名称为警报"- https://itunes.apple.com/us/app/alarmy-alarm-clock/id1163786766?mt = 8

我已经检查了苹果文档的后台模式-

关于Alarmy应用程序,我在这里发现了2个或3个问题,所有这些人都说他们正在通过AVAudioSession类别播放来播放无声声音.我确实尝试过,但是当本地通知到来时,它并没有帮助我唤醒应用程序.

直到现在我都进行了无声音频的无限循环,并且还设置了音频会话,但是它无法唤醒应用程序.

 让audioSession = AVAudioSession.sharedInstance()尝试?audioSession.setCategory(.playback,模式:.default,选项:.mixWithOthers)尝试?audioSession.setActive(true)audioPlayer =试试!AVAudioPlayer.init(contentsOf:URL.init(fileURLWithPath:Bundle.main.path(forResource:"Birthday_6",ofType:"mp3")!))audioPlayer?.numberOfLoops = -1audioPlayer?.play() 

如果有人在AVAudioSession上有想法要像定位服务一样唤醒应用程序,请告诉我.

根据我的观察,Alarmy应用正在为设置警报的指定时间设置本地通知的 x 号./p>

当应用程序进入后台时,他们正在设置此 x 条本地通知.并在应用程序进入前台时使所有待处理的通知无效.这是一个循环,当应用程序进入前台并返回到后台时,它将继续重复执行.

终止时,它们会在那一刻触发一个通知,要求用户在指定时间内将应用保持在后台打开状态,同时还要在指定的时间内保持 x 个通知.

当应用程序不在前台时,会在警报时间触发通知的 x 个通知编号,每个通知之间都有特定的时间间隔,这就是正在发出的声音的长度播放,这将使被触发的通知的音乐或声音具有连续性.

警报启动时,如果应用程序正在运行,它将像常规警报一样响起,直到将其关闭.如果您单击关闭"以外的任何内容,或者在这段时间内强行关闭了应用程序,则会触发前面提到的循环,并会继续使用本地通知发出声音,直到单击应用程序中的关闭"按钮为止.

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.

这篇关于使用AlarmSession之类的AudioSession在后台唤醒应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆