Skip to content

Add onEventHandler block handler to handle all fired events.#382

Closed
Romick2005 wants to merge 3 commits into
react-native-webrtc:masterfrom
Romick2005:master
Closed

Add onEventHandler block handler to handle all fired events.#382
Romick2005 wants to merge 3 commits into
react-native-webrtc:masterfrom
Romick2005:master

Conversation

@Romick2005

Copy link
Copy Markdown

Init CallKeep on native site, not on JS.
Pass a function that will called at any call reported event. And make them public via native interface like: RNCallKeepPerformEndCallAction, RNCallKeepDidDisplayIncomingCall, ... etc

@sboily

sboily commented Mar 25, 2021

Copy link
Copy Markdown
Member

Hello, can you explain a little bit this PR please? Thank you!

@sboily sboily added the waiting for feedback Waiting for feedback label Mar 25, 2021
@Romick2005

Romick2005 commented Mar 25, 2021

Copy link
Copy Markdown
Author

All of that was done due to the issue that on slow devices the event "RNCallKeepDidLoadWithEvents" was not filled with all fired events. So in case when person A calling to Person B (app on Person B device is closed/killed) and Person B rejects the call there was no way to inform person A that call was rejected, but on native part I saw RNCallKeepPerformEndCallAction, but not on js side event not in RNCallKeepDidLoadWithEvents. So I decided to send http request directly from native code and that is why I need access the the event name and separate method to setup CallKeep.
So in my case I need more exclusive access to the library. That is why I add additional callback withEventHandler for withEventHandler:

  [callKeep initCallKitProvider: settings
    // withEventHandler: nil
    withEventHandler: ^(NSString *eventName, id data) {

      #ifdef DEBUG
        NSLog (@"[RNCallKeep][setup] event eventName = %@, data = %@", eventName, data);
      #endif

      if (eventName == RNCallKeepHandleStartCallNotification) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepHandleStartCallNotification");
      } else if (eventName == RNCallKeepDidReceiveStartCallAction) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidReceiveStartCallAction");
      } else if (eventName == RNCallKeepPerformAnswerCallAction) {
        startTimeInSeconds = (long) [[NSDate date] timeIntervalSince1970];
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepPerformAnswerCallAction");
      } else if (eventName == RNCallKeepPerformEndCallAction) {

        // Prepare data for http request
        NSString *callUUID = data[@"callUUID"];
        NSDictionary *payload = callUUIDCallDataMap[callUUID][@"payload"];

        NSDictionary *parameters = @{
          @"callUUID": callUUID,
          @"reasonId": @6
        };

        // Send http delete request to reject call
        NSData *callEndData = [NSJSONSerialization dataWithJSONObject: parameters options: NSJSONWritingPrettyPrinted error: nil];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL: [NSURL URLWithString: cancelUrl]];
        [request setHTTPMethod: @"DELETE"];
        [request addValue: @"application/json" forHTTPHeaderField: @"Content-Type"];
        NSString *postLength = [NSString stringWithFormat: @"%lu", [callEndData length]];
        [request setValue: postLength forHTTPHeaderField: @"Content-Length"];
        [request setHTTPBody: callEndData];
        [[[NSURLSession sharedSession] dataTaskWithRequest: request] resume];
        [callUUIDCallDataMap removeObjectForKey: callUUID];

        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepPerformEndCallAction");

        // [NSThread sleepForTimeInterval:2.0f];
      } else if (eventName == RNCallKeepDidActivateAudioSession) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidActivateAudioSession");
      } else if (eventName == RNCallKeepDidDeactivateAudioSession) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidDeactivateAudioSession");
      } else if (eventName == RNCallKeepDidDisplayIncomingCall) {
        NSString *callUUID = data[@"callUUID"];
        [callUUIDCallDataMap setObject: data forKey: callUUID];

        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidDisplayIncomingCall");
      } else if (eventName == RNCallKeepDidPerformSetMutedCallAction) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidPerformSetMutedCallAction");
      } else if (eventName == RNCallKeepPerformPlayDTMFCallAction) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepPerformPlayDTMFCallAction");
      } else if (eventName == RNCallKeepDidToggleHoldAction) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidToggleHoldAction");
      } else if (eventName == RNCallKeepProviderReset) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepProviderReset");
      } else if (eventName == RNCallKeepCheckReachability) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepCheckReachability");
      } else if (eventName == RNCallKeepDidLoadWithEvents) {
        NSLog (@"[RNCallKeep][withEventHandler] RNCallKeepDidLoadWithEvents");
      } else {
        NSLog (@"[RNCallKeep][withEventHandler] Event %@not found.", eventName);
      }
    }
  ];

  return YES;
}

Also I moved CallKeep setup to the native part to have everything under control and to not set up it twice:

 // Callkeep setup
  // We have to set appName as settings for CallKeep in standardUserDefaults, because we may not call RNCallKeep.setup(options) on JS side
  NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"RNCallKeepSettings"];

  NSBundle *bundle = [NSBundle mainBundle];
  NSDictionary *info = [bundle infoDictionary];

  NSMutableDictionary<NSString *, NSMutableDictionary *> *callUUIDCallDataMap =  [[NSMutableDictionary alloc] init];

  settings = @{
    @"appName": [info objectForKey:@"CFBundleDisplayName"]
  };

  RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];

I also expose all callKeep events to the publicity:

extern NSString * const RNCallKeepHandleStartCallNotification;
extern NSString * const RNCallKeepDidReceiveStartCallAction;
extern NSString * const RNCallKeepPerformAnswerCallAction;
extern NSString * const RNCallKeepPerformEndCallAction;
extern NSString * const RNCallKeepDidActivateAudioSession;
extern NSString * const RNCallKeepDidDeactivateAudioSession;
extern NSString * const RNCallKeepDidDisplayIncomingCall;
extern NSString * const RNCallKeepDidPerformSetMutedCallAction;
extern NSString * const RNCallKeepPerformPlayDTMFCallAction;
extern NSString * const RNCallKeepDidToggleHoldAction;
extern NSString * const RNCallKeepProviderReset;
extern NSString * const RNCallKeepCheckReachability;
extern NSString * const RNCallKeepDidLoadWithEvents;

@sboily

sboily commented Apr 15, 2021

Copy link
Copy Markdown
Member

Probably superseded by #394

@manuquentin

Copy link
Copy Markdown
Contributor

Thanks @Romick2005 I've merged #394 instead.
Feel free to open another PR if you think there is something missing.

@manuquentin manuquentin closed this May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for feedback Waiting for feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants