Search code examples
iosaudiovoiphig

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


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?


Solution

  • 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.