Skip to content

Commit d38ff61

Browse files
authored
fix for missing callkit icon when app terminated (#841)
1 parent 9d55b4d commit d38ff61

File tree

2 files changed

+80
-54
lines changed

2 files changed

+80
-54
lines changed
Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,128 @@
1+
import Flutter
12
import Foundation
23
import PushKit
3-
import Flutter
44
import flutter_callkit_incoming
55

6-
public class StreamVideoPKDelegateManager: NSObject, PKPushRegistryDelegate, UNUserNotificationCenterDelegate {
6+
public class StreamVideoPKDelegateManager: NSObject, PKPushRegistryDelegate,
7+
UNUserNotificationCenterDelegate
8+
{
79
public static let shared = StreamVideoPKDelegateManager()
8-
10+
911
private var pushRegistry: PKPushRegistry?
1012
private var defaultData: [String: Any]?
1113
private var mainChannel: FlutterMethodChannel?
12-
14+
1315
private override init() {
1416
super.init()
1517
}
16-
18+
1719
@objc public func registerForPushNotifications() {
1820
pushRegistry = PKPushRegistry(queue: DispatchQueue.main)
1921
pushRegistry?.delegate = self
2022
pushRegistry?.desiredPushTypes = [.voIP]
2123
}
22-
24+
2325
public func initChannel(mainChannel: FlutterMethodChannel) {
2426
self.mainChannel = mainChannel
2527
}
26-
28+
2729
public func initData(data: [String: Any]) {
2830
defaultData = data
2931
}
30-
32+
3133
// 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+
) {
3338
let deviceToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
3439
return StreamVideoPushNotificationPlugin.setDevicePushTokenVoIP(deviceToken: deviceToken)
3540
}
36-
37-
@objc public func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
41+
42+
@objc public func pushRegistry(
43+
_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType
44+
) {
3845
return StreamVideoPushNotificationPlugin.setDevicePushTokenVoIP(deviceToken: "")
3946
}
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+
) {
4252
// Return if type is not voIP.
4353
guard type == .voIP else {
4454
return completion()
4555
}
46-
56+
4757
let defaults = UserDefaults.standard
4858
let callbackHandle = defaults.object(forKey: "callback_handle") as? Int64
49-
59+
5060
var streamDict = payload.dictionaryPayload["stream"] as? [String: Any]
51-
61+
5262
let state = UIApplication.shared.applicationState
5363
if state == .background || state == .inactive {
5464
if state == .inactive, callbackHandle != nil {
5565
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!)
5870
let entrypoint = callbackInfo?.callbackName
5971
let uri = callbackInfo?.callbackLibraryPath
60-
72+
6173
let isRunning = engine.run(withEntrypoint: entrypoint, libraryURI: uri)
6274
}
63-
}
64-
65-
75+
}
76+
6677
handleIncomingCall(streamDict: streamDict, state: state, completion: completion)
6778
} else if state == .active {
6879
mainChannel?.invokeMethod("customizeCaller", arguments: streamDict) { (response) in
6980
if let customData = response as? [String: Any] {
7081
streamDict?["created_by_display_name"] = customData["name"] as? String
7182
streamDict?["created_by_id"] = customData["handle"] as? String
7283
}
73-
74-
self.handleIncomingCall(streamDict: streamDict, state: state, completion: completion)
84+
85+
self.handleIncomingCall(
86+
streamDict: streamDict, state: state, completion: completion)
7587
}
7688
}
7789
}
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+
) {
8094
let defaultCallText = "Unknown Caller"
81-
95+
8296
let callCid = streamDict?["call_cid"] as? String ?? ""
8397
let createdByName = streamDict?["created_by_display_name"] as? String
8498
let createdById = streamDict?["created_by_id"] as? String
8599
let videoIncluded = streamDict?["video"] as? String
86100
let videoData = videoIncluded == "false" ? 0 : 1
87101

88-
var callUUID = UUID().uuidString;
102+
var callUUID = UUID().uuidString
89103

90104
let data: StreamVideoPushParams
91105
if let jsonData = self.defaultData {
92106
data = StreamVideoPushParams(args: jsonData)
93107
} else {
94108
data = StreamVideoPushParams(args: [String: Any]())
95109
}
96-
110+
97111
data.callKitData.uuid = callUUID
98112
data.callKitData.nameCaller = createdByName ?? defaultCallText
99113
data.callKitData.handle = createdById ?? defaultCallText
100114
data.callKitData.type = videoData
101115
data.callKitData.extra = ["callCid": callCid]
102-
116+
data.callKitData.iconName =
117+
UserDefaults.standard.string(forKey: "callKit_iconName") ?? data.callKitData.iconName
118+
103119
// Show call incoming notification.
104120
StreamVideoPushNotificationPlugin.showIncomingCall(
105121
data: data.callKitData,
106122
fromPushKit: true
107123
)
108-
124+
109125
completion()
110126
}
111-
127+
112128
}

packages/stream_video_push_notification/ios/Classes/StreamVideoPushNotificationPlugin.swift

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,61 @@ import flutter_callkit_incoming
44

55
public class StreamVideoPushNotificationPlugin: NSObject, FlutterPlugin {
66
let persistentState: UserDefaults = UserDefaults.standard
7-
7+
88
public static func register(with registrar: FlutterPluginRegistrar) {
9-
let mainChannel = FlutterMethodChannel(name: "stream_video_push_notification", binaryMessenger: registrar.messenger())
9+
let mainChannel = FlutterMethodChannel(
10+
name: "stream_video_push_notification", binaryMessenger: registrar.messenger())
1011
let instance = StreamVideoPushNotificationPlugin()
11-
12+
1213
registrar.addMethodCallDelegate(instance, channel: mainChannel)
1314
StreamVideoPKDelegateManager.shared.initChannel(mainChannel: mainChannel)
1415
}
15-
16+
1617
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
1718
switch call.method {
18-
case "initData":
19-
if let arguments = call.arguments as? [String: Any] {
20-
let handle = arguments["callbackHandler"] as? Int64
21-
persistentState.set(handle, forKey: "callback_handle")
22-
23-
StreamVideoPKDelegateManager.shared.initData(data: arguments)
24-
result(nil)
25-
} else {
26-
result(FlutterError(code: "INVALID_ARGUMENT", message: "Invalid argument", details: nil))
19+
case "initData":
20+
if let arguments = call.arguments as? [String: Any] {
21+
let handle = arguments["callbackHandler"] as? Int64
22+
persistentState.set(handle, forKey: "callback_handle")
23+
24+
if let iosData = arguments["ios"] as? [String: Any],
25+
let iconName = iosData["iconName"] as? String
26+
{
27+
persistentState.set(iconName, forKey: "callKit_iconName")
2728
}
28-
default:
29-
result(FlutterMethodNotImplemented)
29+
30+
StreamVideoPKDelegateManager.shared.initData(data: arguments)
31+
result(nil)
32+
} else {
33+
result(
34+
FlutterError(
35+
code: "INVALID_ARGUMENT", message: "Invalid argument", details: nil))
36+
}
37+
default:
38+
result(FlutterMethodNotImplemented)
3039
}
31-
}
32-
40+
}
41+
3342
@objc public static func setDevicePushTokenVoIP(deviceToken: String) {
3443
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.setDevicePushTokenVoIP(deviceToken)
3544
}
36-
45+
3746
@objc public static func startOutgoingCall(
3847
data: flutter_callkit_incoming.Data,
3948
fromPushKit: Bool
4049
) {
4150
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.startCall(data, fromPushKit: fromPushKit)
4251
}
43-
52+
4453
@objc public static func showIncomingCall(
4554
data: flutter_callkit_incoming.Data,
4655
fromPushKit: Bool
4756
) {
48-
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.showCallkitIncoming(data, fromPushKit: fromPushKit)
57+
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.showCallkitIncoming(
58+
data, fromPushKit: fromPushKit)
4959
}
50-
60+
5161
@objc public static func activeCalls() -> [[String: Any]]? {
52-
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.activeCalls();
62+
SwiftFlutterCallkitIncomingPlugin.sharedInstance?.activeCalls()
5363
}
5464
}

0 commit comments

Comments
 (0)