I created an app that will send text messages through an API similar to Twilio, and I have an NSTimer that fires a function every set amount of time. The problem is that if you press the home button, it will totally stop the timing, which defeats the purpose.
I have read that it is possible to have your phone play a mute sound until closed to make sure it still runs in the background, but I can't find a resource on how to do this. Here is an example: To run NSTimer in background
If anyone has any information on how to do this, I'm sure it is very simple, and I very much appreciate the help.
Thank you.
If you set up your app as a background sound playing app then yes, it is technically possible to play a "silent sound" in order to keep it active in the background, but your app will be rejected by Apple if you do this.
Apple expressly forbids what you are trying to do.
You should look at using scheduled local notifications. Those will alert the user, who can bring your app to the foreground and let it perform the desired task.
Related
I've been googling for a long time about how to run code in the background for IOS, and for what I have come up with the short answer is that you can't. And the long answer that you can but is limited. I've seen similar apps to the one I am creating so I feel like there should be a way to make it.
So on to the actual app I want to create. I want to create an application that will continuously play a random sound from a list in between a set interval, let's say for this example 30-60 seconds. So randomly every 30<t<60 seconds a sound should play. You can think of it as a positive affirmations app that should play positive affirmations throughout the day. I've seen such apps exist for IOS, I haven't tried them but I assume they should work like that? And when the app is firing the code to play the sound, Ideally I also want it to send information to a server.
I am using "Flutter Background Service" and "AudioPlayer" packages and have gotten a very very early demo version to work on android where it plays the sound after a random amount of time. And now I want it to work for Iphones also. Since it's not a playlist, I can't use the inbuilt functions for IOS that allows media players to run in the background. For what I can understand is that no code can run for longer then 30 seconds on IOS from when the app goes to the background, or the screen locks. And you can't run background tasks more often then every 15 minutes, or 3 minutes for small work. But that is way to long for the use case I want. I'd want to be able to run the function every 10 seconds to maybe 1 minute at the latest.
So I am wondering, is there a way to get such an application to work for IOS? It feels like it should be possible somehow. I don't really care if you have to "play dirty" to achieve it, and I don't know if it matters or not but I am not planning on publishing the app to the app store. It will only be a private app for me and a few friends.
One way I've been thinking about is maybe use push notifications from a server to trigger it. But I'd rather not have to resort to this if I can help it, if it even is possible to have a push notification to run code/trigger functions in the background?
I'd like to play a sound periodically while the app is in the background but after doing some research it's not certain if this is possible. Before I give up or waste too much time trying I want to double check with the community if this is really possible or not.
I figured out how to enable playing sounds while the app is in the background but the app is still eventually killed and the timer stops firing (there's a 10 minute period it runs in suspended mode if I use beginBackgroundTask: but that's not enough). Other ideas I had:
1) There appears to be a "background fetch" mode which could maybe be used like a timer but this is a hack and may be rejected by Apple.
2) Loop blank audio in the background or some other looping part of audio API's to keep the app alive? Again Apple may reject that even it's possible.
3) Enable the location API to keep the app alive and play audio. Hack again, bad for battery and Apple 99% will reject this.
4) I read somewhere that push notifications could work for this but that requires a complicated server backend and if it's possible that last resort.
Are any of those methods viable or are there others?
You can use scheduled Local Notifications with sounds.
A lot of todo-list apps such as Wunderlist offer some kind of reminder feature which usually just fires off a notification.
But I have never encountered an app that offer a similar notification view as the built in preinstalled alarm clock. You know, that locks the phone and offers a snooze option.
This makes me wonder if the alarm clock functionality that is built into the preinstalled iOs app from Apple including setting time or add a snooze button etc is only available for that particular app?
Since I'm a designer without any coding skills I only want to know if it possible or not.
Unfortunately this is unavailable for developers. You can check all of alarm clock apps in AppStore, all of them are using the standard local notifications. Which will fire once and that's all. You can of course set your custom sound and change couple of options, but it will never work like one build in Clock app inside iPhone.
You can read more about local notifications in Apple documentation: https://developer.apple.com/reference/usernotifications/unnotificationrequest
I figured Alarmy is constantly playing (when there is an alarm set up) a silent sound in the background. Like Spotify, but silent. This keeps the app active all the time. I came to this conclusion by logging the result AVAudioSession.sharedInstance().isOtherAudioPlaying (Swift 4) in my own application. Whenever Alarmy is terminated it returns false, whenever Alarmy is running in the background it returns true. Same thing happens for the app linked by Ernesto Elsäßer.
I used the same technique and I also used this pod to trigger a function when the time comes and start the real music.
I did this for testing and am not going to make an app out of it because playing silent sounds in the background is against Apple's rules. I don't know how Alarmy (or other apps that use this same method) got away with it.
Some related questions:
How do you constantly run in background?
App “Alarmy” is able to play iTunes song from background state… How?
Wake up application in background using AudioSession like Alarmy iOS app
Also you can check this article out.
This app seems to do exactly this, but I have no clue how ...
I tested it, and it managed to start playing music on a locked device at an arbitrarily chosen time, and for way longer than the 30s timeframe allowed for notification sounds. Further, the music is streamed from an online radio station, meaning they found a way to ...
wake up the app from a scheduled notification without user interaction
create a socket connection to a radio station in the background
start playing sound in the background for an indefinite amount of time
I also tried out the alarm in Airplane Mode, and it still played a locally stored backup song, so it probably doesn't abuse the VoiP background mode or push notifications, but really is triggered by a local notification ...
So although I can't tell how, it looks like there is an App Store Review-proof way to create a real alarm app.
(I am not affiliated with the developers of this app, just did some research on what's possible.)
I was wondering if it was possible to play a continues sound in my Skype like application when a user is called an the other user has the app installed but the app is in the background in the moment.
It would be really awesome if it could could show an "accept / decline" on the lock screen. But that might not be possible is it?
I have looked around on the internet, but I could not find it. (I think I saw something related to this on a keynote once, but that might just be my memory hoping it exists.)
That is possible to show an "accept / decline" on lock screen or notification itself. The only thing that is not possible to do is to make input field for quick response (as it's done in Messages app)
There is nice guide to interactive notifications for iOS8.
To play sound i suggest you to use possibilities provided by UILocalNotification instead of implementing custom sound/vibration.
Also, if your app is in background, it must use push notifications, so for VoIP apps i suggest you to take a look into PuskKit framework and special type of pushes - VoIP pushes
I want do code an alarm-clock.
Now the alar-clock should play a music-file at a specific time.
But I don't know how to call the method with AVAudioPlayer while the app is in background?
Can you please help me I want do this for the first time.
thanks :)
You should not use a long-running background thread in order to trigger a sound at a certain time. A running thread prevents the device from going to sleep, greatly increasing power requirements.
Further, Apple greatly restricts the amount of processing you are allowed to do from the background. You can request time to complete a long task from the background, but only a short period of time is allowed. (A few minutes at most if memory serves.
You should probably create a local notification instead. The local notification will display a banner message on the lock screen if your app is not running, and optionally play a sound that you specify.
Take a look at the UILocalNotification Class Reference in Xcode for more information.