VoIP notification with OneSignal Complete setup in iOS

Monyneath Mol
MonyneathMOL
Published in
3 min readJan 18, 2020

Infact there are many artical on the internet talking about how to setup OneSignal VoIP push but all of them, no one tell us about a complete setup.

So in this artical I am going to talk about how to Setup OneSignal VoIP notification to make a PhoneCall App with CallKit . Let stop talking. Start Implementing.
if you are new to callKit

1. Register VoIP Service certificate at Apple Website

2. Create App in OneSignal Dashboard

One signal app registration

You need to create 2 app in OneSignal to enable notification and handling VoIP notification. In this case I register App “VideoCallApp” with my normal Push notification Certificate. And “VoIPService” app with the VoIP Certificate that we create above.

3. Register Device.

To start using VoIP sevice with oneSignal, You needed to register device with oneSignal Account first.

3.1 Enable background Mode

3.2 Adding this into your App delegate did finished launching with option function.

if you don’t have OneSignal Setup yet. please find it in the Offical website.

import PushKit
import OneSignal
...func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {voipRegistration()func voipRegistration(){
let voipRegistery : PKPushRegistry = PKPushRegistry(queue:DispatchQueue.main)
voipRegistery.delegate = self
voipRegistery.desiredPushTypes = [PKPushType.voIP]
}
func setupOneSignal(launchOptions: [UIApplication.LaunchOptionsKey: Any]?){//START OneSignal initialization codelet onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]// Replace 'YOUR_MAIN_APP_ID' with your OneSignalApp ID.OneSignal.initWithLaunchOptions(launchOptions,appId: YOUR_MAIN_APP_ID,handleNotificationAction: nil,settings: onesignalInitSettings)OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;// Recommend moving the below line to prompt for push after informing the user about// how your app will use them.OneSignal.promptForPushNotifications(userResponse: { accepted inprint("User accepted notifications: \(accepted)")})}

in this case “YOUR_MAIN_APP_ID” here is the App ID of VideoCallApp’s App ID that we registered above.

//Add these AppDelegate Extensionextension AppDelegate  : PKPushRegistryDelegate{ func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {//        // Register VoIP push tokenlet parts = pushCredentials.token.map { String(format: "%02.2hhx", $0) }let VoIPToken = parts.joined()print("did update push credentials with token: \(VoIPToken)")}func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {//process the recieved pushprint("didReceiveIncomingPushWith payload \(payload)")self.receiveIncomingPush(payload: payload, for: type)}func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {print("Incomming Push \(payload.dictionaryPayload)")self.receiveIncomingPush(payload: payload, for: type)}func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {}func receiveIncomingPush(payload: PKPushPayload, for type: PKPushType) {print("did receiving  incoming Payload")guard type == .voIP else {return}// payload.dictionaryPayload may have caller info// call reportNewIncomingCall() here.if let customData = payload.dictionaryPayload["custom"] as? [String:Any]{//Handle your incomming VoIP notification here}}}}

After app load, you will get the VoIP notification from Pushkit didUpdate pushCredentials function. After you have that token. Call a request to register your device with OneSignal request.

curl --location --request POST 'https://onesignal.com/api/v1/players' \
--header 'Content-Type: application/json' \
--data-raw '{"app_id" : "YOUR_MAIN_APP_ID",
"identifier":"YOUR_VOIP_TOKEN",
"language":"en",
"timezone":-28800,
"game_version":"1.0",
"device_os":"13.0.1",
"device_type":0,
"device_model":"DeviceModelNAME",
"external_id":"MUST_HAVE",
"tags":{"a":"1","foo":"bar"}}'

This is the tricky part of it. I needed to add external_ID to identify our user for the further use. YOUR_MAIN_APP_ID here also refer the the appID of VideocallApp’s App ID too.

4. Sart Testing

After you done setting up. let do a testing to make sure our setup is correct. You can testing with the Request Below.

curl --location --request POST 'https://onesignal.com/api/v1/notifications' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YOUR_MAIN_APP_KEY' \
--data-raw '{
"app_id": "YOUR_MAIN_APP_APP_ID",
"apns_push_type_override": "voip",
"contents": {
"en": "English Message"
},
"data":{
...
},
"content_available": true,
"priority": 5,
"ttl": 30 ,
"include_external_user_ids": [
"YOUR_EXTERNAL_USER_ID"
]
}'

After this request being called, It will trigger this function

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) { print("Incomming Push \(payload.dictionaryPayload)") self.receiveIncomingPush(payload: payload, for: type)
}

--

--