如何通知移动应用程序进行 Twilio 入站呼叫?已经使用节点服务器测试了 Twilio 演示 android 应用程序,但它不起作用

问题描述

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,));
        } else {
            $to = "client:" . $to;
            $call = $twilio_client->calls->create($to,));
        }

        return $call->sid;
    }

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

我有上面的代码来接听我的移动应用程序的来电,但我无法弄清楚 twilio 如何将来电通知发送到我的移动应用程序以及用户通过哪个移动应用程序响应呼叫。我在令牌创建 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();
    } 

但是 FCM 如何触发 Twilio 通知?它将自行处理,或者他们是否需要在调用传入呼叫网络挂钩的服务器端编写代码,并通过该代码将传入呼叫通知发送到移动应用程序?

解决方法

Android/Ios 应用的令牌:

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 方法:

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

In 使用 $to 作为调用标识:

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

但是这个数字包含+号,通过排除+号它开始对我有用。即

$to = "923222333322"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...