Wake Up Application in Background Using Audiosession Like Alarmy iOS App

Swift - Is there a way to wake up my app at a certain time?

You probably need a silent notification.
You can find a good description here.

Local notification sound not playing while app in background - Swift

So I've discovered something.

As the question is stated, it is not currently possible to play sound in a local notification when the phone is on silent mode.

However, great news!

There is actually a different way to achieve the same result; and it's how apps like Alarmy do it.

Note: I (FINALLY) discovered this solution from this wonderful SO answer, but I'll summarize it here for reference.

In short, the local notification will not be playing the sound, but instead, the app will play it (while in the background).

STEPS

  1. You must enable the app to play sound in the background. To do this, navigate to your .plist file and add the String value App plays audio or streams audio/video using AirPlay to the array key Required background modes. (This can also be achieved in your app's Capabilities - it does the same thing).

  2. In your App Delegate, set your AVAudioSession's category to .playBack so sound will still play even when the phone is locked or in the background.

do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error.localizedDescription)
}

  1. Start your AVAudioPlayer at the time you would like it to play (in my case, at the time of my local notification).
let timeInterval = 60.0 // 60.0 would represent 1 minute from now
let timeOffset = audioPlayer.deviceCurrentTime + timeInverval
audioPlayer.play(atTime: timeOffset) // This is the magic function

// Note: the `timeInterval` must be added to the audio player's
// `.deviceCurrentTime` to calculate a correct `timeOffset` value.

In conclusion, as the SO answer I linked to above so aptly summarizes:

This does not play silence in the background, which violates Apple's rules. It actually starts the player, but the audio will only start at the right time. I think this is probably how Alarmy implemented their alarm, given that it's not a remote notification that triggers the audio nor is the audio played by a local notification (as its not limited to 30 seconds or silenced by the ringer switch).

iOS - show consistent alert at the top of the UI when backgrounding the app (like personal hotspot does)

There are a handful of built in 'background modes' that change the status bar's appearance depending on what functionality an app provides whilst it's in the background. The one you've identified (a red status bar) is triggered when an app records audio whilst in the background. I presume Sleep Cycle must be acting as though it records audio just for this purpose. Other background modes include VoIP (which I think uses a blue status bar). Check out Apple's documentation on supporting these various background modes

In your case, you'd want to add audio to the UIBackgroundModes property in your Info.plist file.

But note that it wouldn't be unreasonable for Apple to reject an app during review if it pretends to perform one of these background tasks but doesn't. For example, there have been apps in the past that tried playing a silent audio clip continuously in order to stay awake in the background - needless to say Apple got wise to this and the app in question had to change its behaviour.



Related Topics



Leave a reply



Submit