How to make an iOS VoIP app obey Do Not Disturb when ringing?
Asked Answered
B

1

7

One would think it would be essential for a VoIP app to obey the same rules as the stock phone app but it turns out to be almost impossible to implement ringing correctly. Several things I tried:

  1. Local push notifications with ring sound.

    Good: obeys both Silent and DND modes.

    Bad: the sound can be no longer than 30 seconds, and it only vibrates once when the notification appears. So to achieve the ringing effect the notification has to be re-pushed e.g. every 6 seconds, effectively spamming the notification center. Also push notifications do not sound/vibrate if the app is active so the app has to detect that and ring differently.

  2. AudioServicesPlayAlertSound().

    Good: proper API seemingly designed specifically for this task. Obeys silent mode.

    Bad: completely ignores Do Not Disturb mode, the sound and vibration come right through.

  3. Use AVFoundation to play the ring sound.

    Good the sound plays.

    Bad: does not support vibration, does not support silent/DND modes. Essentially not usable as a ringer.

Is there a better way? Or did Apple completely miss this use case?

Bawdry answered 20/2, 2015 at 14:53 Comment(0)
C
2

As you say in your 3 options, only a UILocalNotification actually obeys silent/DND mode.

The problems with it can be solved.

Spamming the notification center: I think that works quite well. You can cancel your previous notification immediately before you fire off a new one, so there will always be only 1 outstanding notification.

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Vibration problem: You should be able to call this: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); in the same place where you call your local notification over and over again with a timer until the call ends or the users acknowledges the call. With the VOIP background setting on it should work in the background.

As you stated in option 2 the vibrate will not follow DND mode, but it's just vibration. If you spam the notification center that will vibrate once every time the notification comes in so you may not need to explicitly start vibrating if that's enough for you.

Good luck.

Cure answered 29/4, 2015 at 20:58 Comment(2)
I'm accepting your answer because this is exactly how we ended up implementing it. We let the notification vibrate, too, so that it does not even vibrate in DND. I wish there was a better answer though. This is clearly a hack.Bawdry
We implemented the solution as described by teradyl. This works great when the app is in the background. However, if the app is in the foreground the notification will not vibrate or play the sound. Therefore, we capture the notification in application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) and play the sound with AudioServicesPlayAlertSound(). This works, however, when the user answers the sound will continue to play through the earpiece until the wav file is complete. I can't find a way to stop this playback.Clite

© 2022 - 2024 — McMap. All rights reserved.