1

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.

Amrit Trivedi
  • 1,240
  • 1
  • 9
  • 24
  • There is no way to wakeup applications without pressing on notification, as for the location services case is different, but i suppose you are working on the Alarm app in which you can use local notification to play sound in the background when you trigger the notification but remember custom sound should not be more than 30 seconds. – Harjot Singh Apr 06 '19 at 09:12
  • 1
    You're technically right. But how come Alarmy app is doing? It's approved on app store. There must be technical way to do it. Otherwise apple is not going to approve it. – Amrit Trivedi Apr 06 '19 at 09:25
  • I am not used to Alarmy app still if you are saying,i will try it today, let you know what they are doing. – Harjot Singh Apr 06 '19 at 09:35
  • So, you are saying that when the alarm time comes, the app is automatically coming from background state to foreground state? – Harjot Singh Apr 06 '19 at 09:40
  • yes, even if you will power off iPhone and restarts it, Alarmy will immidietly throwing n number of notifications till you open the app and dismiss it. So, please try that app. – Amrit Trivedi Apr 06 '19 at 10:18
  • @AmritTrivedi What is telling you that Alarmy is launched in the background when alarm time comes? I guess the app just schedules many local notifications. At least that is what I see when I setup an alarm and kill the app afterwards. The app is not launched until you tap the notification (in case it wasn't just still running). – sofacoder Apr 06 '19 at 10:23
  • I did try to keep it on till 4 hours. For continues 4 hours it fired local notifications. It's not posssible by scheduling it. It's only possible if app wake up in background and triggering it rhgough timer. I am manage to do it while my app is in backgorund. – Amrit Trivedi Apr 06 '19 at 10:24
  • 1
    Well I did not try it, but the app could just schedule an `UNCalendarNotificationTrigger` for every second of the next X hours. No need for being launched. The App even asks you to run it to work properly when you kill it. – sofacoder Apr 06 '19 at 10:33
  • What about turning iPhone off and turning it on after 2 or 3 hours? In that case your solution will not work. That app even wakes it up and starting throwing notification on start of the iphone. – Amrit Trivedi Apr 06 '19 at 10:35
  • @AmritTrivedi to correct you that app is not waking up, it just throwing the local notifications which you can do even the app is terminated that is called services, i went through the app, it is working just i thought it would, it tells you to put your app in background so that you can receive sound with notification. So, for the alarm sound it is just throwing custom sound with local notification and when your app is not in background it throwing you local notifications to keep it in background. – Harjot Singh Apr 06 '19 at 11:44
  • In the case of iPhone turned off you have to create two or three notifications with periodic time intervals so that even it turned off for two hours, after turning on of three hours it should be fired. – Harjot Singh Apr 06 '19 at 11:56
  • https://stackoverflow.com/a/22842112/8810022 check this answer for throwing notifications even when the iPhone is turned on or off – Harjot Singh Apr 06 '19 at 12:00
  • @HarjotSingh Do you guys know how to wake up the app at specific clock time? – Nimesh Chandramaniya Jun 11 '19 at 11:10
  • @Nimesh Do you mean to send local notification at specific time?, there is no way to wake up the app without user interaction on the notification. – Harjot Singh Jun 11 '19 at 13:28
  • @HarjotSingh I heard about the silent remote notifications. Are there other ways to wake the app at specific time? – Nimesh Chandramaniya Jun 12 '19 at 04:19
  • 1
    @Nimesh there is no way to wakeup the app with user interaction, as for silent notification, you can send silently some updated data through background modes which will be receivable in your app delegate. – Harjot Singh Jun 12 '19 at 05:49
  • @HarjotSingh I have some interesting observations to share with you. **Alarmy** app is able to trigger the alarm even if the device's Wi-Fi is off. If I disable the notification permissions for the app, then the alarm is not triggered. Is this app using remote notifications or local notifications? – Nimesh Chandramaniya Jun 12 '19 at 05:59
  • They are using local notifications. They are using deprecated code of UILocalnotification to repeat notification for every 1 second interval till you remote notifications. – Amrit Trivedi Jun 12 '19 at 06:01
  • @AmritTrivedi Thank you for the reply. Your observation is correct but those local notifications are triggered when you force quit or kill the app. But if you put the app in the background by pressing the device's HOME button, then there is only one notification when alarm is triggered. Hence, I am still confused about how this app is working? – Nimesh Chandramaniya Jun 12 '19 at 06:08
  • @AmritTrivedi did u get the solution. Currently I'm facing issue to play notification sound in background or terminated while device is silent and when I locked the screen notification sound is overlapped. – pramod Oct 26 '20 at 12:08

1 Answers1

1

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.

Spidy
  • 1,137
  • 3
  • 28
  • 48
  • Why do they set local notifications when going to the background state? If in the background they can play sound? Is this a type and you meant Suspended state? Additionally, I have noticed, Alarmy can work even when the phone is in the do not disturb mode, do you have any ideas how it's done? Thanks a lot!!! – Yahor Sinkevich Dec 14 '21 at 13:26