目录

Incoming Call

Use Cases

The app receives a voice or video call through the network.

The display effect of an incoming call is as follows.

Incoming Voice Call Incoming Video Call Incoming Video Call (Voice Mode Unsupported)
Ongoing Call Incoming Voice Call on a Lock Screen Incoming Video Call on a Lock Screen

Constraints

The incoming call capability is supported on phones and tablets. Support for wearables was added in API version 6.0.0 (20), and support for PCs/2-in-1 devices was added in API version 6.1.1 (24).

Service Process

[h2]Answering an Incoming Call

[h2]Rejecting an Incoming Call

API Description

APIs used for incoming calls are provided by voipCall. For more information, please refer to the API Reference.

API Description
on(type: 'voipCallUiEvent', callback: Callback<VoipCallUiEventInfo>): void Subscribes to voipCallUiEvent.
off(type: 'voipCallUiEvent', callback?: Callback<VoipCallUiEventInfo>): void Unsubscribes from voipCallUiEvent.
reportIncomingCall(voipCallAttribute: VoipCallAttribute): Promise<ErrorReason> Reports the incoming call.
reportCallAudioEventChange(callId: string, callAudioEvent: CallAudioEvent): Promise<void> Reports the audio event.
reportCallStateChange(callId: string, callState: VoipCallState): Promise<void> Reports the call status change.
reportCallStateChange(callId: string, callState: VoipCallState, callType: VoipCallType): Promise<void> Reports the call status change and specifies the call type.

Development Procedure

  1. Import dependencies.

    import { voipCall } from '@kit.CallServiceKit';
    import { image } from '@kit.ImageKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Subscribe to voipCallUiEvent to detect the user's answering, hanging up, muting, and unmuting operations on the banner. It is recommended to complete the step before reporting the incoming call.

    The sample code is as follows:

    // Subscribe to voipCallUiEvent.
    voipCall.on('voipCallUiEvent', callback => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in registering voipCallUiEvent');
    });
  3. After a call connection is established within the app, the incoming call must be reported to Call Service Kit along with call details. For more information, please refer to  VoipCallAttribute.

    If the app is running in the background, the system will display a banner for the incoming call.

    The sample code is as follows:

    // Construct parameters for reporting the incoming call.
    let voipCallAttribute: voipCall.VoipCallAttribute = {
      callId: 'callId123',
      voipCallType: voipCall.VoipCallType.VOIP_CALL_VOICE,
      userName: 'Callman',
      userProfile: image.createPixelMapSync(new ArrayBuffer(100), { size: { width: 90, height: 90 } }),
      abilityName: 'VoipCallAbility',
      voipCallState: voipCall.VoipCallState.VOIP_CALL_STATE_RINGING,
      showBannerForIncomingCall: true
    };
    
    // Report the incoming call.
    voipCall.reportIncomingCall(voipCallAttribute).then(errorReason => {
      if (errorReason == voipCall.ErrorReason.ERROR_NONE) {
        hilog.info(0x0000, 'CallDemo', 'Succeeded in reporting the incoming call');
      } else {
        hilog.error(0x0000, 'CallDemo', 'Failed to report the incoming call: %{public}d', errorReason);
      }
    });

    For a video call, the isVoiceAnswerSupported parameter can be used to specify whether voice-only answering is allowed. The sample code is as follows:

    // Construct parameters for reporting the incoming call.
    let voipCallAttribute: voipCall.VoipCallAttribute = {
      callId: 'callId123',
      voipCallType: voipCall.VoipCallType.VOIP_CALL_VIDEO,
      userName: 'Jack',
      userProfile: image.createPixelMapSync(new ArrayBuffer(100), { size: { width: 90, height: 90 } }),
      abilityName: 'VoipCallAbility',
      voipCallState: voipCall.VoipCallState.VOIP_CALL_STATE_RINGING,
      showBannerForIncomingCall: true,
      isVoiceAnswerSupported: false  // Voice answering is not supported for video calls.
    };
  4. The user can choose to answer or reject the call.

    Answering methods: reporting the status twice and reporting the status once

    • (Recommended) Reporting the status twice

      For example, for a voice call, once receiving the VOIP_CALL_EVENT_VOICE_ANSWER callback, the app immediately reports the VOIP_CALL_STATE_ANSWERED state to Call Service Kit and implements call answering simultaneously.

      After call answering is completed, the app reports the VOIP_CALL_STATE_ACTIVE state to Call Service Kit. The system will then update the notification banner.

      The sample code is as follows:

      voipCall.on('voipCallUiEvent', callback => {
        if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_VOICE_ANSWER) {
          // Immediately report the answered state to Call Service Kit.
          voipCall.reportCallStateChange(callback.callId, voipCall.VoipCallState.VOIP_CALL_STATE_ANSWERED);
      
          // Implement call answering in the app.
      
          // After the call is answered in the app, report the active state to Call Service Kit.
          voipCall.reportCallStateChange(callback.callId, voipCall.VoipCallState.VOIP_CALL_STATE_ACTIVE);
        }
      });

      The display effect of an ongoing call is as follows.

      Connecting Connected

      Benefits of reporting the status twice:

      Due to network issues, a noticeable delay (example: around 1 second) may occur between the user's tap on the answer button and the call being answered. User experience may be compromised if the banner continues to notify of the ringing state during the delay.

      Reporting the status twice updates the screen for the user when the call is answered.

    • Reporting the status once

    For example, for a voice call, once receiving the VOIP_CALL_EVENT_VOICE_ANSWER callback, the app implements call answering.

    After call answering is completed, the app reports the VOIP_CALL_STATE_ACTIVE state to Call Service Kit.

    The sample code is as follows:

    voipCall.on('voipCallUiEvent', callback => {
      if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_VOICE_ANSWER) {
        // Implement call answering in the app.
    
        // After the call is answered in the app, report the call status to Call Service Kit.
        voipCall.reportCallStateChange(callback.callId, voipCall.VoipCallState.VOIP_CALL_STATE_ACTIVE);
      }
    });

    If the user taps the button for rejecting the call on the notification banner, the app will complete the rejection upon receiving the VOIP_CALL_EVENT_REJECT callback, then report the VOIP_CALL_STATE_DISCONNECTED state to Call Service Kit. The system will dismiss the notification banner accordingly.

    After the call is rejected, skip steps 5 and 6 and go directly to step 7.

    The sample code is as follows:

    voipCall.on('voipCallUiEvent', callback => {
      if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_REJECT) {
        // Implement call rejection in the app.
    
        // Report the call status to Call Service Kit.
        voipCall.reportCallStateChange(callback.callId, voipCall.VoipCallState.VOIP_CALL_STATE_DISCONNECTED);
      }
    });
  5. During the call, the user can mute or unmute the call as needed.

    For example, when the user taps the mute button on the notification banner, Call Service Kit will trigger the VOIP_CALL_EVENT_MUTED callback and report the event to the app.

    After the app completes the mute operation, it will then report the AUDIO_EVENT_MUTED state to Call Service Kit.

    The sample code is as follows:

    voipCall.on('voipCallUiEvent', callback => {
      if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_MUTED) {
        // Report the muted state to Call Service Kit.
        voipCall.reportCallAudioEventChange(callback.callId, voipCall.CallAudioEvent.AUDIO_EVENT_MUTED);
      } else if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_UNMUTED) {
        // Report the unmuted state to Call Service Kit.
        voipCall.reportCallAudioEventChange(callback.callId, voipCall.CallAudioEvent.AUDIO_EVENT_UNMUTED);
      }
    });

    The display effect of the banner notification for muting and unmuting is as follows.

    Muting Unmuting
  6. When the user taps the button for hanging up, Call Service Kit will trigger the VOIP_CALL_EVENT_HANGUP callback and report the event to the app.

    The app then ends the call after receiving the event and reports the VOIP_CALL_STATE_DISCONNECTED state to Call Service Kit. The system will then cancel the banner notification.

    The sample code is as follows:

    voipCall.on('voipCallUiEvent', callback => {
      if (callback?.voipCallUiEvent == voipCall.VoipCallUiEvent.VOIP_CALL_EVENT_HANGUP) {
        // Implement call hang-up in the app.
    
        // Report the call status to Call Service Kit.
        voipCall.reportCallStateChange(callback.callId, voipCall.VoipCallState.VOIP_CALL_STATE_DISCONNECTED);
      }
    });
  7. Unsubscribe from voipCallUiEvent if necessary, as the app no longer needs to detect the user's operations on the banner after the call ends.

    The sample code is as follows:

    // Unsubscribe from voipCallUiEvent.
    voipCall.off('voipCallUiEvent', callback => {
      hilog.info(0x0000, 'CallDemo', `Succeeded in unRegistering voipCallUiEvent, callId: ${callback.callId}`);
    });