15
15
16
16
#import < AVFoundation/AVAudioSession.h>
17
17
18
- NSString *const RNCallKitHandleStartCallNotification = @" RNCallKitHandleStartCallNotification" ;
19
- NSString *const RNCallKitDidReceiveStartCallAction = @" RNCallKitDidReceiveStartCallAction" ;
20
- NSString *const RNCallKitPerformAnswerCallAction = @" RNCallKitPerformAnswerCallAction" ;
21
- NSString *const RNCallKitPerformEndCallAction = @" RNCallKitPerformEndCallAction" ;
22
- NSString *const RNCallKitDidActivateAudioSession = @" RNCallKitDidActivateAudioSession" ;
18
+ static int const DelayInSeconds = 2 ;
19
+
20
+ static NSString *const RNCallKitHandleStartCallNotification = @" RNCallKitHandleStartCallNotification" ;
21
+ static NSString *const RNCallKitDidReceiveStartCallAction = @" RNCallKitDidReceiveStartCallAction" ;
22
+ static NSString *const RNCallKitPerformAnswerCallAction = @" RNCallKitPerformAnswerCallAction" ;
23
+ static NSString *const RNCallKitPerformEndCallAction = @" RNCallKitPerformEndCallAction" ;
24
+ static NSString *const RNCallKitDidActivateAudioSession = @" RNCallKitDidActivateAudioSession" ;
23
25
24
26
@implementation RNCallKit
25
27
{
26
28
NSMutableDictionary *_settings;
27
29
NSOperatingSystemVersion _version;
30
+ BOOL _isStartCallActionEventListenerAdded;
28
31
}
29
32
30
- RCT_EXPORT_MODULE ()
33
+ // should initialise in AppDelegate.m
34
+ // RCT_EXPORT_MODULE()
31
35
32
- // Override method of RCTEventEmitter
33
- - (void )startObserving
36
+ - (instancetype )init
34
37
{
35
38
#ifdef DEBUG
36
- NSLog (@" [RNCallKit][startObserving ]" );
39
+ NSLog (@" [RNCallKit][init ]" );
37
40
#endif
38
- [[NSNotificationCenter defaultCenter ] addObserver: self
39
- selector: @selector (handleStartCallNotification: )
40
- name: RNCallKitHandleStartCallNotification
41
- object: nil ];
41
+ if (self = [super init ]) {
42
+ [[NSNotificationCenter defaultCenter ] addObserver: self
43
+ selector: @selector (handleStartCallNotification: )
44
+ name: RNCallKitHandleStartCallNotification
45
+ object: nil ];
46
+ _isStartCallActionEventListenerAdded = NO ;
47
+ }
48
+ return self;
42
49
}
43
50
44
- // Override method of RCTEventEmitter
45
- - (void )stopObserving
51
+ - (void )dealloc
46
52
{
47
53
#ifdef DEBUG
48
- NSLog (@" [RNCallKit][stopObserving ]" );
54
+ NSLog (@" [RNCallKit][dealloc ]" );
49
55
#endif
50
56
[[NSNotificationCenter defaultCenter ] removeObserver: self ];
51
57
}
@@ -149,6 +155,11 @@ - (void)stopObserving
149
155
[self requestTransaction: transaction];
150
156
}
151
157
158
+ RCT_EXPORT_METHOD (_startCallActionEventListenerAdded)
159
+ {
160
+ _isStartCallActionEventListenerAdded = YES ;
161
+ }
162
+
152
163
- (void )requestTransaction : (CXTransaction *)transaction
153
164
{
154
165
#ifdef DEBUG
@@ -272,7 +283,7 @@ + (BOOL)application:(UIApplication *)application
272
283
#ifdef DEBUG
273
284
NSLog (@" [RNCallKit][application:continueUserActivity]" );
274
285
#endif
275
- INInteraction *interaction = userActivity.interaction ;
286
+ INInteraction *interaction = userActivity.interaction ;
276
287
INPerson *contact;
277
288
NSString *handle;
278
289
@@ -290,9 +301,11 @@ + (BOOL)application:(UIApplication *)application
290
301
@" handle" : handle,
291
302
@" video" : @NO
292
303
};
304
+
293
305
[[NSNotificationCenter defaultCenter ] postNotificationName: RNCallKitHandleStartCallNotification
294
306
object: self
295
307
userInfo: userInfo];
308
+
296
309
return YES ;
297
310
}
298
311
return NO ;
@@ -303,7 +316,17 @@ - (void)handleStartCallNotification:(NSNotification *)notification
303
316
#ifdef DEBUG
304
317
NSLog (@" [RNCallKit][handleStartCallNotification] userInfo = %@ " , notification.userInfo );
305
318
#endif
306
- [self sendEventWithName: RNCallKitDidReceiveStartCallAction body: notification.userInfo];
319
+ int delayInSeconds;
320
+ if (!_isStartCallActionEventListenerAdded) {
321
+ // Workaround for when app is just launched and JS side hasn't registered to the event properly
322
+ delayInSeconds = DelayInSeconds;
323
+ } else {
324
+ delayInSeconds = 0 ;
325
+ }
326
+ dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, (int64_t )(delayInSeconds * NSEC_PER_SEC));
327
+ dispatch_after (popTime, dispatch_get_main_queue (), ^{
328
+ [self sendEventWithName: RNCallKitDidReceiveStartCallAction body: notification.userInfo];
329
+ });
307
330
}
308
331
309
332
#pragma mark - CXProviderDelegate
0 commit comments