调用已连接后无法从 RNTwilioPhone.calls 检索 UUID

Cannot retrive UUID from RNTwilioPhone.calls after CallConnected was fired

我正在开发具有外呼功能的 React Native Android 移动应用程序, 在当前状态下,它可以向PSTN 呼出但不能挂机。 我试图根据以下引用在 CallConnected 之后找到 UUID,用于 RNCallKeep.endCall(uuid) ,但仍然没有运气。

Hi, you should see it in RNTwilioPhone.calls when CallConnected event is triggered.

Originally posted by @MrHertal in https://github.com/MrHertal/react-native-twilio-phone/issues/54#issuecomment-902481675

事件侦听器

twilioPhoneEmitter.addListener(EventType.CallConnected, ({callSid}) => {
  console.log('CallConnected');
  console.log('callSid');
  console.log(callSid);
  console.log('RNTwilioPhone.calls');
  console.log(RNTwilioPhone.calls);
}),

Console.log

 LOG  CallConnected
 LOG  callSid
 LOG  CAfcd9ea923d640be2bf37b24ade18f642
 LOG  RNTwilioPhone.calls
 LOG  []

代码片段

const HandleHangUp = () => {
    RNCallKeep.endAllCalls(); // Not working
    navigate.goBack();
};

const callKeepOptions = {
    ios: {
        appName: 'TwilioPhone Example',
        supportsVideo: false,
    },
    android: {
        alertTitle: 'Permissions required',
        alertDescription: 'This application needs to access your phone accounts',
        cancelButton: 'Cancel',
        okButton: 'OK',
        additionalPermissions: [],
        selfManaged: true,
        foregroundService: {
            channelId: 'io.wazo.callkeep.VoiceConnectionService',
            channelName: 'Foreground service for my app',
            notificationTitle: 'My app is running on background',
        },
    },
};

useEffect(() => {
    const unsubscribeRNTTwilio = RNTwilioPhone.initializeCallKeep(callKeepOptions, fetchAccessToken);
    const unsubscribeTwilioPhone = listenTwilioPhone();
    RNTwilioPhone.startCall(userPhone);
    return () => {
        unsubscribeRNTTwilio();
        unsubscribeTwilioPhone();
    };
}, []);

const listenTwilioPhone = () => {
    const subscriptions = [
        twilioPhoneEmitter.addListener(EventType.CallConnected, ({callSid}) => {
            console.log('CallConnected');
            console.log('callSid');
            console.log(callSid);
            console.log('RNTwilioPhone.calls');
            console.log(RNTwilioPhone.calls);
        }),
        twilioPhoneEmitter.addListener(EventType.CallDisconnected, () => {
            console.log('CallDisconnected');
            setCallInProgress(RNTwilioPhone.calls.length > 0);
            navigate.goBack();
        }),
        twilioPhoneEmitter.addListener(EventType.CallDisconnectedError, (error) => {
            console.log('CallDisconnectedError');
            console.log(error);
            setCallInProgress(RNTwilioPhone.calls.length > 0);
            navigate.goBack();
        }),
    ];

    return () => {
        subscriptions.map((subscription) => {
            subscription.remove();
        });
    };
};

这里是 Twilio 开发人员布道者。

虽然您在呼叫 RNTwilioPhone.calls 时似乎确实遇到了当前连接的呼叫的问题,但这可能不会立即停止为您工作。

据我在源代码中看到的,TwilioPhone.endCall methodcallSid 作为参数。所以,你应该可以保持活跃状态​​callSid,然后用它来挂断。

所以,你应该保持 callSid 状态:

const [callSid, setCallSid] = useState(null);

来电接通后,设置状态callSid

twilioPhoneEmitter.addListener(EventType.CallConnected, ({callSid}) => {
  setCallSid(callSid);  
});

然后当你想挂断时,使用callSid结束通话并将状态设置回null:

const HandleHangUp = () => {
  TwilioPhone.endCall(callSid);
  setCallSid(null);
  navigate.goBack();
};