Skip to content

Commit 1fe42bf

Browse files
manuquentinr0b0t3d
authored andcommitted
Refactor reportNewIncomingCall and add some doc
1 parent 44bbe82 commit 1fe42bf

File tree

3 files changed

+60
-46
lines changed

3 files changed

+60
-46
lines changed

README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ RNCallKeep.addEventListener('didActivateAudioSession', () => {
410410
Callback for `RNCallKeep.displayIncomingCall`
411411

412412
```js
413-
RNCallKeep.addEventListener('didDisplayIncomingCall', ({ error }) => {
413+
RNCallKeep.addEventListener('didDisplayIncomingCall', ({ error, uuid, handle, localizedCallerName, fromPushKit }) => {
414414
// you might want to do following things when receiving this event:
415415
// - Start playing ringback if it is an outgoing call
416416
});
@@ -567,7 +567,7 @@ class RNCallKeepExample extends React.Component {
567567
onDTMFAction = (data) => {
568568
let { digits, callUUID } = data;
569569
// Called when the system or user performs a DTMF action
570-
}
570+
};
571571

572572
audioSessionActivated = (data) => {
573573
// you might want to do following things when receiving this event:
@@ -587,6 +587,33 @@ class RNCallKeepExample extends React.Component {
587587
}
588588
```
589589

590+
## Receiving a call when the application is not reachable.
591+
592+
In some case your application can be unreachable :
593+
- when the user kill the application
594+
- when it's in background since a long time (eg: after ~5mn the os will kill all connections).
595+
596+
To be able to wake up your application to display the incoming call, you can use [https://github.com/ianlin/react-native-voip-push-notification](react-native-voip-push-notification) on iOS or BackgroundMessaging from [react-native-firebase](https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4)-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background).
597+
598+
You have to send a push to your application, like with Firebase for Android and with a library supporting PushKit pushes for iOS.
599+
600+
### PushKit
601+
602+
Since iOS 13, you'll have to report the incoming calls that wakes up your application, like in your `AppDelegate.m` :
603+
604+
```objective-c
605+
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion {
606+
// Process the received push
607+
[RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type];
608+
609+
// Retrieve information like handle and callerName here
610+
611+
[RNCallKeep reportNewIncomingCall:uuid handle:handle handleType:@"generic" hasVideo:false localizedCallerName:callerName fromPushKit: YES];
612+
613+
completion();
614+
}
615+
```
616+
590617
## Debug
591618

592619
### Android

ios/RNCallKeep/RNCallKeep.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ continueUserActivity:(NSUserActivity *)userActivity
3131
handle:(NSString *)handle
3232
handleType:(NSString *)handleType
3333
hasVideo:(BOOL)hasVideo
34-
localizedCallerName:(NSString * _Nullable)localizedCallerName;
34+
localizedCallerName:(NSString * _Nullable)localizedCallerName
35+
fromPushKit:(BOOL)fromPushKit;
3536
@end

ios/RNCallKeep/RNCallKeep.m

+29-43
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ - (void)dealloc
7272
- (NSArray<NSString *> *)supportedEvents
7373
{
7474
return @[
75-
RNCallKeepDidReceiveStartCallAction,
76-
RNCallKeepPerformAnswerCallAction,
77-
RNCallKeepPerformEndCallAction,
78-
RNCallKeepDidActivateAudioSession,
79-
RNCallKeepDidDeactivateAudioSession,
80-
RNCallKeepDidDisplayIncomingCall,
81-
RNCallKeepDidPerformSetMutedCallAction,
82-
RNCallKeepPerformPlayDTMFCallAction,
83-
RNCallKeepDidToggleHoldAction,
84-
RNCallKeepProviderReset
85-
];
75+
RNCallKeepDidReceiveStartCallAction,
76+
RNCallKeepPerformAnswerCallAction,
77+
RNCallKeepPerformEndCallAction,
78+
RNCallKeepDidActivateAudioSession,
79+
RNCallKeepDidDeactivateAudioSession,
80+
RNCallKeepDidDisplayIncomingCall,
81+
RNCallKeepDidPerformSetMutedCallAction,
82+
RNCallKeepPerformPlayDTMFCallAction,
83+
RNCallKeepDidToggleHoldAction,
84+
RNCallKeepProviderReset
85+
];
8686
}
8787

8888
+ (void)initCallKitProvider {
@@ -105,7 +105,7 @@ + (void)initCallKitProvider {
105105
[[NSUserDefaults standardUserDefaults] synchronize];
106106

107107
[RNCallKeep initCallKitProvider];
108-
108+
109109
self.callKeepProvider = sharedProvider;
110110
[self.callKeepProvider setDelegate:self queue:nil];
111111
}
@@ -140,29 +140,7 @@ + (void)initCallKitProvider {
140140
hasVideo:(BOOL)hasVideo
141141
localizedCallerName:(NSString * _Nullable)localizedCallerName)
142142
{
143-
#ifdef DEBUG
144-
NSLog(@"[RNCallKeep][displayIncomingCall] uuidString = %@", uuidString);
145-
#endif
146-
int _handleType = [RNCallKeep getHandleType:handleType];
147-
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
148-
CXCallUpdate *callUpdate = [[CXCallUpdate alloc] init];
149-
callUpdate.remoteHandle = [[CXHandle alloc] initWithType:_handleType value:handle];
150-
callUpdate.supportsDTMF = YES;
151-
callUpdate.supportsHolding = YES;
152-
callUpdate.supportsGrouping = YES;
153-
callUpdate.supportsUngrouping = YES;
154-
callUpdate.hasVideo = hasVideo;
155-
callUpdate.localizedCallerName = localizedCallerName;
156-
157-
[self.callKeepProvider reportNewIncomingCallWithUUID:uuid update:callUpdate completion:^(NSError * _Nullable error) {
158-
[self sendEventWithName:RNCallKeepDidDisplayIncomingCall body:@{ @"error": error ? error.localizedDescription : @"" }];
159-
if (error == nil) {
160-
// Workaround per https://forums.developer.apple.com/message/169511
161-
if ([self lessThanIos10_2]) {
162-
[self configureAudioSession];
163-
}
164-
}
165-
}];
143+
[RNCallKeep reportNewIncomingCall: uuidString handle:handle handleType:handleType hasVideo:hasVideo localizedCallerName:localizedCallerName fromPushKit: NO];
166144
}
167145

168146
RCT_EXPORT_METHOD(startCall:(NSString *)uuidString
@@ -336,6 +314,7 @@ + (void)reportNewIncomingCall:(NSString *)uuidString
336314
handleType:(NSString *)handleType
337315
hasVideo:(BOOL)hasVideo
338316
localizedCallerName:(NSString * _Nullable)localizedCallerName
317+
fromPushKit:(BOOL)fromPushKit
339318
{
340319
#ifdef DEBUG
341320
NSLog(@"[RNCallKeep][reportNewIncomingCall] uuidString = %@", uuidString);
@@ -353,7 +332,14 @@ + (void)reportNewIncomingCall:(NSString *)uuidString
353332

354333
[RNCallKeep initCallKitProvider];
355334
[sharedProvider reportNewIncomingCallWithUUID:uuid update:callUpdate completion:^(NSError * _Nullable error) {
356-
if (error == nil) {}
335+
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];
336+
[callKeep sendEventWithName:RNCallKeepDidDisplayIncomingCall body:@{ @"error": error ? error.localizedDescription : @"", @"callUUID": uuidString, @"handle": handle, @"localizedCallerName": localizedCallerName, @"fromPushKit": fromPushKit }];
337+
if (error == nil) {
338+
// Workaround per https://forums.developer.apple.com/message/169511
339+
if ([callKeep lessThanIos10_2]) {
340+
[callKeep configureAudioSession];
341+
}
342+
}
357343
}];
358344
}
359345

@@ -481,9 +467,9 @@ + (BOOL)application:(UIApplication *)application
481467

482468
if (handle != nil && handle.length > 0 ){
483469
NSDictionary *userInfo = @{
484-
@"handle": handle,
485-
@"video": @(isVideoCall)
486-
};
470+
@"handle": handle,
471+
@"video": @(isVideoCall)
472+
};
487473

488474
[[NSNotificationCenter defaultCenter] postNotificationName:RNCallKeepHandleStartCallNotification
489475
object:self
@@ -615,10 +601,10 @@ - (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession
615601
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:didActivateAudioSession]");
616602
#endif
617603
NSDictionary *userInfo
618-
= @{
619-
AVAudioSessionInterruptionTypeKey: [NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded],
620-
AVAudioSessionInterruptionOptionKey: [NSNumber numberWithInt:AVAudioSessionInterruptionOptionShouldResume]
621-
};
604+
= @{
605+
AVAudioSessionInterruptionTypeKey: [NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded],
606+
AVAudioSessionInterruptionOptionKey: [NSNumber numberWithInt:AVAudioSessionInterruptionOptionShouldResume]
607+
};
622608
[[NSNotificationCenter defaultCenter] postNotificationName:AVAudioSessionInterruptionNotification object:nil userInfo:userInfo];
623609

624610
[self configureAudioSession];

0 commit comments

Comments
 (0)