start AVAudioSession & AVAudioPlayer in Background using didReceiveRemoteNotification:fetchCompletionHandler: - ios

Can I play AVAudioPlayer when in the app is in background using silent push notification ?
My current implementation is like this :
Send silent push notification
didReceiveRemoteNotification:fetchCompletionHandler: is triggered
and create the session then play mp3 from there.
The issue is that the session seems to be taken away when your app is not active for quite a time (like one night) and AVAudioSession is not given if you request it from background mode, so the AVAudioPlayer won't play the mp3.
I'm developing an alarm app for emergency things, so the app need to play an audible sound even if the phone is muted and locked.
Anyone has been encountered any similar implementation?

The solution in your case would be to use "audio" setting from the UIBackgroundModes key, which effectively tells the system that the app's main purpose is to play music in the background: "The app plays audible content to the user or records audio while in the background". See this for details: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
However, it seems that Apple will reject "alarm apps" that use this approach as alarms do not play constantly and do not qualify as "audio apps". See this answer: Why did my app get rejected because "Multitasking apps may only use background"?.
You can try to "play" silence most of the time, but they will reject it either.
To summarize, you can implement this Alarm App technically, by using silent push notifications and UIBackgroundModes key, but this method is against Apple review policies.

Related

Playing an audio clip even when the app is not running

In my app, I used VoIP notification to play a long alarm on app not running state as per suggestion Silent Push, But app got rejected in the following points
Your app is not in compliance with Guideline 4.2.1 using the VoIP API in a manner that it is not intended.
2.5.4 Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc. If your app uses location background mode, include a reminder that doing so may dramatically decrease battery life.
Is there any way to rectify these issues or would be good if we get any alternative to play a long alarm on push receive in app not running state?
I beleive, UNNotificationContentExtension would help to show our custom notification on push receives but how to play automatic audio on push receive,
Can we handle in push message sound key?
or
Can we do this by default UNNotificationContentExtension methods in
mediaPlay() mediaPlayPauseButtonFrame()
But there is no way to play automatic audio here. please correct me if am wrong.
My requirement will be, I need to play a long audio in any app state and audio should stop on user click of stop button or notification(it usually launch our app).
You can't use background location/ VOIP for other purposes, so if you are using them just to keep your app active in the background, Apple will reject it.
Use them in your app only when it is directly relevant to the features and services provided by the app. Location-based APIs/VOIP shouldn’t be used to provide emergency services or alarm systems, etc. Ensure that you notify and obtain consent before collecting, transmitting, or using location data. If your app uses background services, be sure to explain the purpose in your app. Refer to the Human Interface Guidelines for best practices on doing so.
You can use sound key in your push but audio can only be played for maximum 30 seconds. There's no way like audio should stop when user taps on notification.

How can I mute my local notifications while my iOS app is in foreground?

I am using this package: https://github.com/zo0r/react-native-push-notification to schedule local notifications in my react-native app.
I can schedule local notifications that will display when the app is killed or in the background. If the app is in the foreground the visual notification is suppressed but the notification chime sound plays. I don't want the sound to play - as it can only serve to confuse or annoy the user.
According to Apple docs "The system does not display any alerts, badge the app’s icon, or play any sounds when the app is already frontmost." I've been racking my brain and bingeing on docs but I can't figure out what I could have done to circumvent this iOS policy.
Any ideas where I should look in my code?
This lib has a property that can handle it for you. Just set playSound: false.
You can mix it with AppState to toggle the value according to the state value.
More info in docs below:
https://github.com/zo0r/react-native-push-notification#local-notifications
https://facebook.github.io/react-native/docs/appstate.html
Hope it helps

How to schedule an alert which wakes up iOS app without user intervention

My goal: implement sleep function in my music audio player. If user chooses say 30mins sleep time then the app should wake up and stop the currently played music.
Problem: [self performSelector:#selector (onTimer_sleep) withObject:nil afterDelay:sleepIntervalInSeconds]; does not work once the phone is locked;
My try: use UILocalNotification. This however works only if the app is foremost and visible when the system delivers the notification.
What would you recommend for my use case? Should I implement background mode? Which bg mode would you suggest, "Background fetch"?
Short answer: You can't. Apple severely limits the apps that are allowed to run in the background.
If your app is a music playing app that supports playing from the background then you are all set. That is one of the small list of apps that are allowed to run in the background for more than a couple of minutes. While music is playing in your app you should still get background time.
As you've discovered, local notifications only get delivered if the user taps on them, or if the app is running in the foreground.
You could set up a server to send silent push notifications, but that would require a network connection, and push notifications can't be timed very precisely.

Play sound in background without obeying iPhone ring / silent switch

My app must be able to play alarm sound no matter what upon receiving push notification.
Is there a way to configure audio session to play sound:
and NOT obey silent switch (route may be respected)
upon arrival of push message (can be silent) and if app is in the background or even if killed by the iOS in the meantime ?
Sound is played even if user uses any other audio app (unconditional mix).
I do not need to publish this app to AppStore (only for my private usage), so private API is an option.
For those who may tempt to propose UILocalNotification: those are not suitable in my case since they must obey silent / ring switch.
If you don't want to use UILocalNotification, there is the option described here:
http://oleb.net/blog/2014/02/alarm-clock-apps-ios/
In brief, if you opt-out of iOS's multi-tasking, and you lock your phone with the app running, you can continue to run custom code:
If you do not want your app to run in the background at all, you can explicitly opt out of background by adding the UIApplicationExitsOnSuspend key (with the value YES) to your app’s Info.plist file. When an app opts out, it cycles between the not-running, inactive, and active states and never enters the background or suspended states.
However, if that isn't appropriate for your use-case, you might be able to use UIBackgroundModes with remote-notification to wake up your app when it receives a push notification, but then you'd have to have your own server infrastructure to keep track of your alarms. Even then, I'm not sure if you could start playing a sound while your app is in the background...

iOS receive Media Player Notifications when app is in background

Probably a simple question - can an iOS app continue to receive media player notifications [NSNotifications] (i.e. MPMusicPlayerControllerNowPlayingItemDidChangeNotification) once it moves into background?
I'm specifically using an iPodMusicPlayer to play songs over my main app functionality, and id like to play about with implementing code under this notification after the app falls into background mode.
Not asking for code; just a point in the right direction re: background media notifications ;)
I have made some checks since i need the same.
Unfortunately I have seen that your app does not receive notification whilst in background.
However, I realized that it does receive the entire notification queue once resumed.
The problem is that notification are received all together giving you no information about the time and date they were fired up.
Of course, since you receive them all at once, even if you try to save the nowPlayingItem after the MPMusicPlayerControllerNowPlayingItemDidChangeNotification, you will only see the current item when your app becomes active again, and nothing more.
Unfortunately, I think we cannot do anything more, since Apple doc also says
The notification has no userInfo dictionary
meaning that you cannot get any info out of the notification.

Resources