OneSignal VoIP notifications in the iOS app

Maksym Bilan
3 min readFeb 24, 2018

A lot of people know and use multi-platform push notification service OneSignal. And this post describes how to receive the VoIP notification from the service. 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.

The main issue is that OneSignal SDK registers a token inside the SDK only for usual remote notifications via calling the next:

OneSignal.initWithLaunchOptions(launchOptions,
appId: "YOUR_APP_ID",
handleNotificationAction: nil,
settings: onesignalInitSettings)

As we know for using VoIP notifications the developer needs to implement a token registration using PushKit.

// Link to the PushKit framework
import PushKit
// Trigger VoIP registration on launch
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.voipRegistration()
return true
}
// Register for VoIP notifications
func voipRegistration {
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
let voipRegistry: PKPushRegistry = PKPushRegistry(mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
}
// Handle updated push credentials
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
}

Unfortunately OneSignal SDK doesn’t support PushKit registration. But they have a method in the REST API. This method is used to register a new device with OneSignal. If a device is already registered with the specified identifier, then this will update the existing device record instead of creating a new one. So, the problem is resolved, we need to implement POST request manually.

Example via curl:

curl --include \
--request POST \
--header "Content-Type: application/json" \
--data-binary "{\"app_id\" : \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\",
\"identifier\":\"ce777617da7f548fe7a9ab6febb56cf39fba6d382000c0395666288d961ee566\",
\"language\":\"en\",
\"timezone\":-28800,
\"game_version\":\"1.0\",
\"device_os\":\"7.0.4\",
\"device_type\":0,
\"device_model\":\"iPhone 8,2\",
\"tags\":{\"a\":\"1\",\"foo\":\"bar\"}}" \
https://onesignal.com/api/v1/players

I hope it will be easy to implement this method in Swift or Obj C, so I will not post the code. Let me know if you need it.

Remains there is one question, what to do if the app has to support both ways of the notifications. I mean VoIP, for instance, calls, and messages as usual notifications. The solution is simple, it’s implementation of receiving messages only via PushKit, but the app has to detect what is the call and what is the message and depending on it should decide. If it’s a message, just the app has to send a local notification, if a call, the app does next steps with CallKit SDK.

The main idea was described, if you have questions, please feel free to ask me.

--

--

Maksym Bilan

Software Engineer at @MySwimPro |  swift/objc/c++ | 🇺🇦 | family 👨‍👩‍👧 | swimmer 🏊‍♂️ | optimist ⚡️ | superhero for my 👧