Voice Over IP (VoIP) Best Practices

A Voice over Internet Protocol (VoIP) app lets the user make and receive phone calls using an Internet connection instead of the device’s cellular service. Because a VoIP app relies heavily on the network, it’s no surprise that making calls results in high energy use. When not in active use, however, a VoIP app should be completely idle to conserve energy.

Use VoIP Push Notifications to Avoid Persistent Connections

In the past, a VoIP app had to maintain a persistent network connection with a server to receive incoming calls and other data. This meant writing complex code that sent periodic messages back and forth between the app and the server to keep a connection alive, even when the app wasn’t in use. This technique resulted in frequent device wakes that wasted energy. It also meant that if a user quit the VoIP app, calls from the server could no longer be received.

Instead of persistent connections, developers should use the PushKit framework—APIs that allows an app to receive pushes (notifications when data is available) from a remote server. Whenever a push is received, the app is called to action. For example, a VoIP app could display an alert when a call is received, and provide an option to accept or reject the call. It could even begin taking precursory steps to initiate the call, in the event the user decides to accept.

There are many advantages to using PushKit to receive VoIP pushes:

The device is woken only when VoIP pushes occur, saving energy.

Unlike standard push notifications, which the user must respond to before your app can perform an action, VoIP pushes go straight to your app for processing.

VoIP pushes are considered high-priority notifications and are delivered without delay.

VoIP pushes can include more data than what is provided with standard push notifications.

Your app is automatically relaunched if it’s not running when a VoIP push is received.

Your app is given runtime to process a push, even if your app is operating in the background.

Note

PushKit VoIP support is available in iOS 8 and later.

Prepare to Receive VoIP Push Notifications

Like all apps that support background operation, your VoIP app must have background mode enabled in the Xcode Project > Capabilities pane. Select the checkbox for Voice over IP, as shown in Figure 11-1.

Figure 11-1Enabling the VoIP background mode in an app

You must also create a certificate for your VoIP app. Each VoIP app requires its own individual VoIP Services certificate, mapped to a unique App ID. This certificate allows your notification server to connect to the VoIP service. Visit the Apple Developer Member Center and create a new VoIP Services Certificate. See Figure 11-2. Download the certificate and import it into the Keychain Access app.

Figure 11-2Creating a VoIP certificate in the Apple Developer Member Center

Configure VoIP Push Notifications

To configure your app to receive VoIP push notifications, link to the PushKit framework in your app delegate (or some other location in your app). Then, create a PKPushRegistry object, set its delegate to self, and register to receive VoIP pushes. See Listing 11-1.

Next, implement a delegate method to handle updated push credentials. If your app receives both standard push notifications and VoIP pushes, then your app will receive two separate push tokens. Both tokens must be passed to the server in order to receive notifications. See Listing 11-2.