Playing an audio clip even when the app is not running - ios

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.

Related

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.

PushKit: Can we use push kit(VoiP Push) in Chat Application without using VoiP

I want to sync my local DB with server when Push notification came, but I am not able to do it with Silent Notification because application:didReceiveRemoteNotification:fetchCompletionHandler not called when app killed by user. So, I did not know how to do it, then I read about push kit but don't know is it possible to use push kit without VoiP feature. Please tell me is it possible or not??
If not, suggest me any other idea , how will I run background task when app killed by user??
In my experience as well as others on SO - you get rejected for trying to leverage VOIP as a workaround. See below for actual response from apple dealing with a similar attempt.
2.16: Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc.
2.16 We found that your app uses a background mode but does not include functionality that requires that mode to run persistently. This behavior is not in compliance with the App Store Review Guidelines.
We noticed your app declares support for VoIP in the UIBackgroundModes key in your Info.plist but does not provide any Voice over IP services.
We recognize that VoIP can provide "keep alive" functionality that many app features would like to use. However, using VoIP in this manner is not the intended purpose of VoIP, which, as indicated in the iOS Application Programming Guide, is that: "A Voice over Internet Protocol (VoIP) application allows the user to make phone calls using an Internet connection instead of the device's cellular service."
Seems that the only way to restart the app when it was killed by user is to use PushKit.
It is possible to use PK framework in your app without Voip functionality, but I can't guarantee you, that your app will not be banned while posting to AppStore.

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 - How to add ability for using an alarm with app running in background?

I am building an app for a client that requires the ability for users to set an alarm time in the app, then be able to run the app in the background, then when the alarm time is hit display a notification.
I know Apple has some limitations on how long apps can run in the background, so what is the proper method for going about this?
I already know how to use local notifications, so showing a notification when the alarm is actually hit is not an issue. I just want to make sure I am able to enable the app to 'wake up' when the alarm time is hit and call the code for showing the local notification.
I don't believe that your app can wake itself up upon a notification, users wake your app up by interacting with the said notification.
However, you can keep your app running in the background by various methods so you don't have to worry about waking it up. One of these methods is to let the app pretend to be a music player and constantly play some sound clips(just make them silent to not disrupt the users), this way the OS won't terminate your app and will allow it to run in the background.
Doing so of course against Apple's guideline, is highly likely that your app will be rejected from the App Store unless you have very good reasons to convince the reviewers.
My company is right now developing an app that uses similar approach to keep on running, since its an in-house application we don't need Apple to review it.

Way to update application every 24 hours

I want to wake up my application every 24 hours and update something. Is there a way to do it without using silent push notification, location service and voip. For example the app "My data summary" gives the data usage on daily basis and it does not involve push notification service since i was not asked that permission. So how can i achieve this. Any help would be greatly appreciated.
If you remove the silent push, location services and VoIP, all you have left is Background Audio or Background fetch.
With background audio, you could play a silent sound to keep your application running, but the downside is that it might be killed when another application plays the sound.
With background fetch, you cannot reliably start the application, the OS decides when it will fire up the app to fetch data. How ofter it happens depends on how much is user using your application.
There are no other "legal" ways to achieve this kind of functionality on iOS. There are also a lot of resources available online on this topic.
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
http://www.raywenderlich.com/29948/backgrounding-for-ios
iOS Background Mode
And many more, you need to research background modes more.

Resources