Search code examples
twiliotwilio-programmable-voice

How to notify mobile app for Twilio inbound call? Already test Twilio demo android app with node server but it not working


public function mobileCallTwimlResponse(Request $request): VoiceResponse
    {
        $from = $request->From ?? config('services.twilio.number');
        $to = $request->To;
        $dial = $this->twimlResponse->dial(null, ['callerId' => $from]);

        if (!$to) {
            $this->twimlResponse->say('  "Congratulations! You have made your first call! Good bye."');
        } elseif (is_string($to)) {
            $dial->number($to);
        } else {
            $dial->client($from);
        }

        return $this->twimlResponse;
    }

    public function receiveCall(Request $request)
    {

        $account_sid = config('services.twilio.accountSid');
        $auth_token = config('services.twilio.authToken');
        $twilio_client = new Client($account_sid, $auth_token);
        $from = $request->From ?? config('services.twilio.number');
        $to = $request->To;
        $call = null;
        $url = route('mobile_receive_call');
        if (!$to) {
            $call = $twilio_client->calls->create("client:customer", $from,
                array(
                    "url" => $url,
                ));
        } elseif (is_numeric($to)) {
            $call = $twilio_client->calls->create($to, $from,
                array(
                    "url" => $url,
                ));
        } else {
            $to = "client:" . $to;
            $call = $twilio_client->calls->create($to, $from,
                array(
                    "url" => $url,
                ));
        }

        return $call->sid;
    }

    public function incomingCall()
    {
        return $this->twimlResponse->say('incoming call responding');
    }

I have above code to get incoming call to my mobile app but i can't figure out how twilio send incoming call notify to my mobile app and through which mobile app user respond to call. I had set push credentials in token creation API

 private $accessToken;

    public function __construct(AccessToken $accessToken)
    {
        $this->accessToken = $accessToken;
    }

    public function token($applicationSid, $identity = 'customer', $page_url = null): string
    {
        $forPage = $page_url;
        /*if ($forPage === route('dashboard', [], false)) {
            $this->accessToken->setIdentity('support_agent');
        } else {
            $this->accessToken->setIdentity('customer');
        }*/

        $this->accessToken->setIdentity($identity);

        // Create Voice grant
        $voiceGrant = new VoiceGrant();
        $voiceGrant->setOutgoingApplicationSid($applicationSid);

        // Optional: add to allow incoming calls
        $voiceGrant->setIncomingAllow(true);
        $voiceGrant->setPushCredentialSid(config('services.twilio.push_credential_sid'));
        // Add grant to token
        $this->accessToken->addGrant($voiceGrant);

        // render token to string
        return $this->accessToken->toJWT();
    } 

but how Twilio trigger notify by FCM? it will deal it by itself or their is any need to write code in server side where incoming call web hook is invoked and through that code the incoming call notify send to mobile app?


Solution

  • Token For android/Ios app:

    public function __construct(VoiceCallTokenService $callTokenService)
        {
            $this->callTokenService = $callTokenService;
        }
    
     public function tokenForMobile(): JsonResponse
        {
            $user = auth()->user();
            $identity = TwilioHelper::substrPhoneNumber($user->twilio_number);
            $this->callTokenService->setPushCredentialSid(config('services.twilio.ios_push_credential_sid'));
            $token = $this->callTokenService->token(config('services.twilio.applicationSidForMobile'), $identity);
            return response()->json(['voice_call_token' => $token]);
        }
    

    Helper SubStrPhoneNumber method:

    public static function substrPhoneNumber($phone)
        {
            if ($phone[0] === "+") {
                $phone = substr($phone, 1);
            }
            return $phone;
        }
    
    

    In was using $to as identity for calling:

    $to = $request->To; //+923222333322

    But this number contain + sign, By excluding +sign it's starts to work for me. i.e

    $to = "923222333322"