|
| 1 | +import Flutter |
1 | 2 | import Foundation |
2 | 3 | import PushKit |
3 | | -import Flutter |
4 | 4 | import flutter_callkit_incoming |
5 | 5 |
|
6 | | -public class StreamVideoPKDelegateManager: NSObject, PKPushRegistryDelegate, UNUserNotificationCenterDelegate { |
| 6 | +public class StreamVideoPKDelegateManager: NSObject, PKPushRegistryDelegate, |
| 7 | + UNUserNotificationCenterDelegate |
| 8 | +{ |
7 | 9 | public static let shared = StreamVideoPKDelegateManager() |
8 | | - |
| 10 | + |
9 | 11 | private var pushRegistry: PKPushRegistry? |
10 | 12 | private var defaultData: [String: Any]? |
11 | 13 | private var mainChannel: FlutterMethodChannel? |
12 | | - |
| 14 | + |
13 | 15 | private override init() { |
14 | 16 | super.init() |
15 | 17 | } |
16 | | - |
| 18 | + |
17 | 19 | @objc public func registerForPushNotifications() { |
18 | 20 | pushRegistry = PKPushRegistry(queue: DispatchQueue.main) |
19 | 21 | pushRegistry?.delegate = self |
20 | 22 | pushRegistry?.desiredPushTypes = [.voIP] |
21 | 23 | } |
22 | | - |
| 24 | + |
23 | 25 | public func initChannel(mainChannel: FlutterMethodChannel) { |
24 | 26 | self.mainChannel = mainChannel |
25 | 27 | } |
26 | | - |
| 28 | + |
27 | 29 | public func initData(data: [String: Any]) { |
28 | 30 | defaultData = data |
29 | 31 | } |
30 | | - |
| 32 | + |
31 | 33 | // MARK: - PKPushRegistryDelegate |
32 | | - @objc public func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) { |
| 34 | + @objc public func pushRegistry( |
| 35 | + _ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, |
| 36 | + for type: PKPushType |
| 37 | + ) { |
33 | 38 | let deviceToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined() |
34 | 39 | return StreamVideoPushNotificationPlugin.setDevicePushTokenVoIP(deviceToken: deviceToken) |
35 | 40 | } |
36 | | - |
37 | | - @objc public func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) { |
| 41 | + |
| 42 | + @objc public func pushRegistry( |
| 43 | + _ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType |
| 44 | + ) { |
38 | 45 | return StreamVideoPushNotificationPlugin.setDevicePushTokenVoIP(deviceToken: "") |
39 | 46 | } |
40 | | - |
41 | | - @objc public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) { |
| 47 | + |
| 48 | + @objc public func pushRegistry( |
| 49 | + _ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, |
| 50 | + for type: PKPushType, completion: @escaping () -> Void |
| 51 | + ) { |
42 | 52 | // Return if type is not voIP. |
43 | 53 | guard type == .voIP else { |
44 | 54 | return completion() |
45 | 55 | } |
46 | | - |
| 56 | + |
47 | 57 | let defaults = UserDefaults.standard |
48 | 58 | let callbackHandle = defaults.object(forKey: "callback_handle") as? Int64 |
49 | | - |
| 59 | + |
50 | 60 | var streamDict = payload.dictionaryPayload["stream"] as? [String: Any] |
51 | | - |
| 61 | + |
52 | 62 | let state = UIApplication.shared.applicationState |
53 | 63 | if state == .background || state == .inactive { |
54 | 64 | if state == .inactive, callbackHandle != nil { |
55 | 65 | DispatchQueue.main.async { |
56 | | - let engine = FlutterEngine(name: "StreamVideoIsolate", project: nil, allowHeadlessExecution: true) |
57 | | - let callbackInfo = FlutterCallbackCache.lookupCallbackInformation(callbackHandle!) |
| 66 | + let engine = FlutterEngine( |
| 67 | + name: "StreamVideoIsolate", project: nil, allowHeadlessExecution: true) |
| 68 | + let callbackInfo = FlutterCallbackCache.lookupCallbackInformation( |
| 69 | + callbackHandle!) |
58 | 70 | let entrypoint = callbackInfo?.callbackName |
59 | 71 | let uri = callbackInfo?.callbackLibraryPath |
60 | | - |
| 72 | + |
61 | 73 | let isRunning = engine.run(withEntrypoint: entrypoint, libraryURI: uri) |
62 | 74 | } |
63 | | - } |
64 | | - |
65 | | - |
| 75 | + } |
| 76 | + |
66 | 77 | handleIncomingCall(streamDict: streamDict, state: state, completion: completion) |
67 | 78 | } else if state == .active { |
68 | 79 | mainChannel?.invokeMethod("customizeCaller", arguments: streamDict) { (response) in |
69 | 80 | if let customData = response as? [String: Any] { |
70 | 81 | streamDict?["created_by_display_name"] = customData["name"] as? String |
71 | 82 | streamDict?["created_by_id"] = customData["handle"] as? String |
72 | 83 | } |
73 | | - |
74 | | - self.handleIncomingCall(streamDict: streamDict, state: state, completion: completion) |
| 84 | + |
| 85 | + self.handleIncomingCall( |
| 86 | + streamDict: streamDict, state: state, completion: completion) |
75 | 87 | } |
76 | 88 | } |
77 | 89 | } |
78 | | - |
79 | | - func handleIncomingCall(streamDict: [String: Any]?, state: UIApplication.State, completion: @escaping () -> Void) { |
| 90 | + |
| 91 | + func handleIncomingCall( |
| 92 | + streamDict: [String: Any]?, state: UIApplication.State, completion: @escaping () -> Void |
| 93 | + ) { |
80 | 94 | let defaultCallText = "Unknown Caller" |
81 | | - |
| 95 | + |
82 | 96 | let callCid = streamDict?["call_cid"] as? String ?? "" |
83 | 97 | let createdByName = streamDict?["created_by_display_name"] as? String |
84 | 98 | let createdById = streamDict?["created_by_id"] as? String |
85 | 99 | let videoIncluded = streamDict?["video"] as? String |
86 | 100 | let videoData = videoIncluded == "false" ? 0 : 1 |
87 | 101 |
|
88 | | - var callUUID = UUID().uuidString; |
| 102 | + var callUUID = UUID().uuidString |
89 | 103 |
|
90 | 104 | let data: StreamVideoPushParams |
91 | 105 | if let jsonData = self.defaultData { |
92 | 106 | data = StreamVideoPushParams(args: jsonData) |
93 | 107 | } else { |
94 | 108 | data = StreamVideoPushParams(args: [String: Any]()) |
95 | 109 | } |
96 | | - |
| 110 | + |
97 | 111 | data.callKitData.uuid = callUUID |
98 | 112 | data.callKitData.nameCaller = createdByName ?? defaultCallText |
99 | 113 | data.callKitData.handle = createdById ?? defaultCallText |
100 | 114 | data.callKitData.type = videoData |
101 | 115 | data.callKitData.extra = ["callCid": callCid] |
102 | | - |
| 116 | + data.callKitData.iconName = |
| 117 | + UserDefaults.standard.string(forKey: "callKit_iconName") ?? data.callKitData.iconName |
| 118 | + |
103 | 119 | // Show call incoming notification. |
104 | 120 | StreamVideoPushNotificationPlugin.showIncomingCall( |
105 | 121 | data: data.callKitData, |
106 | 122 | fromPushKit: true |
107 | 123 | ) |
108 | | - |
| 124 | + |
109 | 125 | completion() |
110 | 126 | } |
111 | | - |
| 127 | + |
112 | 128 | } |
0 commit comments