-
Notifications
You must be signed in to change notification settings - Fork 178
/
ViewController+SimulateIncomingCall.swift
142 lines (118 loc) · 5.78 KB
/
ViewController+SimulateIncomingCall.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// ViewController+SimulateIncomingCall.swift
// VideoCallKitQuickStart
//
// Copyright © 2016-2019 Twilio, Inc. All rights reserved.
//
import UIKit
import UserNotifications
// MARK:- Simulate Incoming Call
extension ViewController {
func registerForLocalNotifications() {
// Define the custom actions.
let inviteAction = UNNotificationAction(identifier: "INVITE_ACTION",
title: "Simulate VoIP Push",
options: UNNotificationActionOptions(rawValue: 0))
let declineAction = UNNotificationAction(identifier: "DECLINE_ACTION",
title: "Decline",
options: .destructive)
let notificationCenter = UNUserNotificationCenter.current()
// Define the notification type
let meetingInviteCategory = UNNotificationCategory(identifier: "ROOM_INVITATION",
actions: [inviteAction, declineAction],
intentIdentifiers: [],
options: .customDismissAction)
notificationCenter.setNotificationCategories([meetingInviteCategory])
// Register for notification callbacks.
notificationCenter.delegate = self
// Request permission to display alerts and play sounds.
notificationCenter.requestAuthorization(options: [.alert])
{ (granted, error) in
// Enable or disable features based on authorization.
}
}
@IBAction func simulateIncomingCall(sender: AnyObject) {
let alertController = UIAlertController(title: "Schedule Notification", message: nil, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { alert -> Void in
let roomNameTextField = alertController.textFields![0] as UITextField
let delayTextField = alertController.textFields![1] as UITextField
let roomName = roomNameTextField.text
self.roomTextField.text = roomName
var delay = 5.0
if let delayString = delayTextField.text, let delayFromString = Double(delayString) {
delay = delayFromString
}
self.logMessage(messageText: "Schedule local notification for Room: \(String(describing: roomName)) after a \(delay) second delay")
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: delay, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Room Invite"
content.body = "Tap to connect to \(roomName ?? "a Room")."
content.categoryIdentifier = "ROOM_INVITATION"
if let name = roomName {
content.userInfo = [ "roomName" : name ]
}
let identifier = NSUUID.init().uuidString
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let theError = error {
print("Error posting local notification \(theError)")
}
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(action : UIAlertAction!) -> Void in
})
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Room Name"
}
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Delay in seconds (defaults is 5)"
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
extension ViewController : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Will present notification \(notification)")
self.reportIncomingCall(uuid: UUID(), roomName: ViewController.parseNotification(notification: notification)) { _ in
// Always call the completion handler when done.
completionHandler(UNNotificationPresentationOptions())
}
}
static func parseNotification(notification: UNNotification) -> String {
var roomName = ""
if let requestedName = notification.request.content.userInfo["roomName"] as? String {
roomName = requestedName
}
return roomName
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("Received notification response in \(UIApplication.shared.applicationState.rawValue) \(response)")
let roomName = ViewController.parseNotification(notification: response.notification)
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
self.performStartCallAction(uuid: UUID(), roomName: roomName)
completionHandler()
break
case "INVITE_ACTION":
self.reportIncomingCall(uuid: UUID(), roomName: roomName) { _ in
// Always call the completion handler when done.
completionHandler()
}
break
case "DECLINE_ACTION":
completionHandler()
break
case UNNotificationDismissActionIdentifier:
completionHandler()
break
// Handle other actions…
default:
break
}
}
}