Reference code snippet - How to initiate or accept PSTN Call
Sample PSTN call implementation.
//AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//SDK Initialization
Rtc555Sdk.initializeLibrary()
return true
}
}
//DashboardViewController.swift
class DashboardViewController {
//After successful login
func configureSDK {
//SDK Config should be called before accessing other SDK APIs
let config:Rtc555Config = Rtc555Sdk.sharedInstance.getConfig()
config.phoneNumber = "2149230284" // Source Telephone Number i.e. phone number for the account you are logged in with.
config.routingId = "a2685b26-5ef0-11ea-83a9-fa163e547f24@uc-prod.comcast.com" // Unique id for the user logged in 555 platform. (Identity manager response)
config.url = "https://evm.iris.comcast.net" // Event manager URL
config.token = "<555 JWT token>" // JWT token for accessing the 555 SDK/backend APIs. Token can be access from the 555 login response.
config.domain = "uciris12.comcast.net" // Unique domain name
config.notificationManagerUrl = "https://ntm.iris.comcast.net/" //(Optional) For mobile notifications from 555 Platform
Rtc555Sdk.setConfig(config: config)
}
}
//PSTNCallViewController.swift
class PSTNCallViewController {
func initiatePSTNCall {
//SDK Dial
Rtc555Voice.dial( number: "1234567890", notificationPayload:buildNotificationPayload(), rtcVoiceDelegate: self){ result in
switch result {
case .success(let callId):
print("Dial was success and callid is = \(callId)")
case .failure(let error):
print(error)
}
}
}
//build notification payload
private func buildNotificationPayload() -> String{
let data = ["cname" : "User Name"
"cid" : "userName123"]
let notificationPayload = ["type" : "pstn",
"topic": "federation/pstn"]
let userData = ["data": data, "notification" : notificationPayload]
do{
let jsonData = try JSONSerialization.data(withJSONObject: userData, options: JSONSerialization.WritingOptions.prettyPrinted)
return String(data: jsonData, encoding: String.Encoding.utf8)!
}catch {
print("Error creating notification payload")
}
return " "
}
}
extension PSTNCallViewController: Rtc555SdkDelegate {
//SDK onNotification callback
func onNotification(notification notificationData: [AnyHashable : Any]) {
let pushType = NotificationType(rawValue: ((notificationData["type"] as? String) ?? ""))!
switch(pushType){
case .notify:
print("xmpp notify")
//After user accepts incoming call invite, call SDK accept API with received payload
self.acceptPSTNCall(userInfo:notificationData)
case .cancel:
print("xmpp cancel")
//If user rejects incoming call invite
self.rejectPSTNCall(userInfo:notificationData)
case .chat:
print("xmpp chat")
default:
print(" Failed to determine notification type")
}
}
}
func acceptPSTNCall(userInfo:[AnyHashable : Any]) {
//SDK accept
Rtc555Voice.accept(notificationData: buildNotificationData(userInfo:userInfo),rtcVoiceDelegate: self) { result in
switch result {
case .success(let callId):
print("callId :: \(callId)")
case .failure(let error):
print(error)
}
}
}
func rejectPSTNCall() {
//SDK reject
Rtc555Voice.reject(notificationData: buildNotificationData(userInfo:userInfo)) { result in
switch result {
case .success(let callId):
print("Reject was success for call = \(callId)")
case .failure(let error):
print(error)
}
}
}
private func buildNotificationData(userInfo:[AnyHashable : Any]) -> Dictionary {
let notificationdata = ["trace_id" : (userInfo["trace_id"] as? String) ?? "",
"room_id" : (userInfo["room_id"] as? String) ?? "",
"rtc_server" : n(userInfo["rtc_server"] as? String) ?? "",
"to_routing_id" : (userInfo["routing_id"] as? String) ?? "",
"room_token" : (userInfo["room_token"] as? String) ?? "",
"room_token_expiry_time" : (userInfo["room_token_expiry_time"] as? Int64) ?? 0
] as [String : Any]
return notificationdata
}
extension PSTNCallViewController: Rtc555VoiceDelegate {
//SDK voice callbacks
func onStatus(status callStatus: CallStatus, id callId: String) {
switch (callStatus){
case .initializing:
//Initializing
case .connecting:
//Connecting
case .connected:
//Call connected
case .reconnecting:
//Reconnecting
case .disconnected:
//Call ended
case .hold:
//Call is on hold
}
}
func onError(error errorInfo: Error, id callId: String) {
print("Error: \(String(describing: errorInfo)) for callId \(callId)")
}
func onCallStats(callQuality: Int, bitrate: String, id callId: String) {
print("Rtc555SdkTests :: callQuality: \(callQuality)")
}
}
//PSTNCallViewController.swift
class PSTNCallViewController {
// User tapped on mute call button
@IBAction func buttonMuteCall(_ sender: Any) {
//SDK mute call
Rtc555Voice.mute(callId: currentCallId)
}
}
//PSTNCallViewController.swift
class PSTNCallViewController {
// User tapped on unmute call button
@IBAction func buttonUnMuteCall(_ sender: Any) {
//SDK mute call
Rtc555Voice.unmute(callId: currentCallId)
}
}
//PSTNCallViewController.swift
class PSTNCallViewController {
// User tapped on end call button
@IBAction func buttonEndCall(_ sender: Any) {
//SDK end call
Rtc555Voice.hangup(callId: currentCallId)
}
}
//AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidEnterBackground(_ application: UIApplication) {
//No active PSTN call then
if !hasActivePstnCall {
//SDK cleanup
Rtc555Sdk.cleanup()
}
}
}