不管是安卓还是ios现在多数的app都无法长时间在后台运行(特殊权限以及应用除外),如果要想在app没有激活或者被冻结的情况下接收到来电,那么就需要先推送一条通知。
搜了一下有这么个插件:https://github.com/sem32/freeswitch-PushNotificator 尝试了一下发现编译起来比较麻烦,后来发现了这篇文章:https://www.zoiper.com/en/tutorials/push-notifications参考里面的关键代码,包含一个push.sh,代码如下:
#!/bin/bash printResult() { echo "<root>" echo -e "\t<code>"$1"</code>" echo -e "\t<X_NotificationStatus>"$2"</X_NotificationStatus>" echo -e "\t<X_DeviceConnectionStatus>"$3"</X_DeviceConnectionStatus>" echo -e "\t<X_SubscriptionStatus>"$4"</X_SubscriptionStatus>" echo "</root>" } CURL=`which curl` if [ -z $CURL ]; then echo "Curl not found" exit 1 fi if [ -z $1 ]; then echo "Url is not set" exit 1 fi URL=$1 i=0 max_i=3 while [ 1 ] do if [ $i -ge $max_i ]; then exit 0; fi let i++ headers=`$CURL -s --data ' <?xml version="1.0" encoding="utf-8"?> <root> <Value1>Zoiper</Value1> <Value2>Incoming</Value2> <Value3>Call</Value3> </root> ' -D - $URL --header "Content-Type: text/xml" --header "X-NotificationClass: 4" | tr -d '\r' ` code=`echo "$headers" | head -n 1` X_NotificationStatus=`echo "$headers" | grep "X-NotificationStatus" | sed 's#X-NotificationStatus: ##' | sed -e 's/^\s*//g' -e 's/\s*$//g' ` X_DeviceConnectionStatus=`echo "$headers" | grep "X-DeviceConnectionStatus" | sed 's#X-DeviceConnectionStatus: ##' | sed -e 's/^\s*//g' -e 's/\s*$//g' ` X_SubscriptionStatus=`echo "$headers" | grep "X-SubscriptionStatus" | sed 's#X-SubscriptionStatus: ##' | sed -e 's/^\s*//g' -e 's/\s*$//g' ` if [ "$code" != "HTTP/1.1 200 OK" ]; then break; fi if [ "$X_NotificationStatus" != "Suppressed" ]; then break; fi if [ "$X_DeviceConnectionStatus" != "Connected" ]; then break; fi if [ "$X_SubscriptionStatus" != "Active" ]; then break; fi done printResult "$code" "$X_NotificationStatus" "$X_DeviceConnectionStatus" "$X_SubscriptionStatus"
除此之外freeswitch还需要配置dialplan逻辑:
<action application="set" data="result=${sofia_contact(1001)}"/> <action application="set" data="push=${regex(${result}|^(?:.*)X-PUSH-URI=(.*)|%1)}"/>" <action application="set" data="result=${system /usr/bin/push.sh ${push}}"/> <action application="sleep" data="2000"/>
所以整体的一个逻辑就是修改拨号逻辑,在接收到呼叫的时候首先发送通知,2s之后开始正式呼叫。但是单纯基于上面的逻辑是无法进行push的,需要第三方推送服务支持,可以使用个推等服务。如果要通知呼叫号码,目标号码等,可以使用下面的代码:
<action application="set" data="result=${system /root/sh/push_notice.sh ${caller_id_number} ${destination_number}}"/> <action application="sleep" data="2000"/>