Skip to content

Commit 7c89f5e

Browse files
committed
Fix outgoing native calls on iOS when app is killed
1 parent d46f174 commit 7c89f5e

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

ios/RNCallKeep/RNCallKeep.m

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
#import <AVFoundation/AVAudioSession.h>
1717

18-
static int const DelayInSeconds = 3;
18+
#ifdef DEBUG
19+
static int const OUTGOING_CALL_WAKEUP_DELAY = 10;
20+
#else
21+
static int const OUTGOING_CALL_WAKEUP_DELAY = 5;
22+
#endif
1923

2024
static NSString *const RNCallKeepHandleStartCallNotification = @"RNCallKeepHandleStartCallNotification";
2125
static NSString *const RNCallKeepDidReceiveStartCallAction = @"RNCallKeepDidReceiveStartCallAction";
@@ -45,15 +49,20 @@ - (instancetype)init
4549
NSLog(@"[RNCallKeep][init]");
4650
#endif
4751
if (self = [super init]) {
48-
[[NSNotificationCenter defaultCenter] addObserver:self
49-
selector:@selector(handleStartCallNotification:)
50-
name:RNCallKeepHandleStartCallNotification
51-
object:nil];
5252
_isStartCallActionEventListenerAdded = NO;
5353
}
5454
return self;
5555
}
5656

57+
+ (id)allocWithZone:(NSZone *)zone {
58+
static RNCallKeep *sharedInstance = nil;
59+
static dispatch_once_t onceToken;
60+
dispatch_once(&onceToken, ^{
61+
sharedInstance = [super allocWithZone:zone];
62+
});
63+
return sharedInstance;
64+
}
65+
5766
- (void)dealloc
5867
{
5968
#ifdef DEBUG
@@ -281,7 +290,7 @@ - (void)dealloc
281290
CXPlayDTMFCallAction *dtmfAction = [[CXPlayDTMFCallAction alloc] initWithCallUUID:uuid digits:key type:CXPlayDTMFCallActionTypeHardPause];
282291
CXTransaction *transaction = [[CXTransaction alloc] init];
283292
[transaction addAction:dtmfAction];
284-
293+
285294
[self requestTransaction:transaction];
286295
}
287296

@@ -444,9 +453,8 @@ + (BOOL)application:(UIApplication *)application
444453
@"video": @(isVideoCall)
445454
};
446455

447-
[[NSNotificationCenter defaultCenter] postNotificationName:RNCallKeepHandleStartCallNotification
448-
object:self
449-
userInfo:userInfo];
456+
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];
457+
[callKeep handleStartCallNotification: userInfo];
450458
return YES;
451459
}
452460
return NO;
@@ -457,21 +465,21 @@ + (BOOL)requiresMainQueueSetup
457465
return YES;
458466
}
459467

460-
- (void)handleStartCallNotification:(NSNotification *)notification
468+
- (void)handleStartCallNotification:(NSDictionary *)userInfo
461469
{
462470
#ifdef DEBUG
463-
NSLog(@"[RNCallKeep][handleStartCallNotification] userInfo = %@", notification.userInfo);
471+
NSLog(@"[RNCallKeep][handleStartCallNotification] userInfo = %@", userInfo);
464472
#endif
465473
int delayInSeconds;
466474
if (!_isStartCallActionEventListenerAdded) {
467475
// Workaround for when app is just launched and JS side hasn't registered to the event properly
468-
delayInSeconds = DelayInSeconds;
476+
delayInSeconds = OUTGOING_CALL_WAKEUP_DELAY;
469477
} else {
470478
delayInSeconds = 0;
471479
}
472480
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
473481
dispatch_after(popTime, dispatch_get_main_queue(), ^{
474-
[self sendEventWithName:RNCallKeepDidReceiveStartCallAction body:notification.userInfo];
482+
[self sendEventWithName:RNCallKeepDidReceiveStartCallAction body:userInfo];
475483
});
476484
}
477485

0 commit comments

Comments
 (0)