Interest in implementing SIP push notification

I don’t see pjsip being an issue. I based the method I used from this example:
https://www.zoiper.com/en/tutorials/push-notifications


extensions.conf entry:

[inbound]
exten => _1000,1,Set(ph=<your_phone_extension>)
exten => _1000,n,Answer()
exten => _1000,n,Set(tok=${DB(SIP/Registry/${ph})})
exten => _1000,n,AGI(/opt/etc/asterisk/push.php,SIP/Registry/${ph}, ${tok})
exten => _1000,n,Wait(4)
exten => _1000,n,Dial(SIP/${ph},30,rt)


push.php script:

#!/opt/bin/php-cli -q
<?php

$db2=new SQLite3('/opt/etc/asterisk/tokens.sqlite3');
$API_ACCESS_KEY='<YOUR_FIREBASE_API_SERVER_KEY>';
$f=fopen("/tmp/log.txt","w");
$key="";
$k=$argv[1];
if (isset($argv[2])) {
        $v=$argv[2];
        if (preg_match('/pn-tok=([^;]+)/i', $v,$token)) {
                $v=$token[1];
                $db2->query("delete from keys where key='$k'");
                $db2->query("insert into keys values( '$k','$v')");
                fwrite($f,"got token $v\r\n");

        }
        fwrite($f,"k=$k, v=$v\r\n");
}
//we read here in case we didnt get a new token so use the stored value
$results=$db2->query("SELECT value FROM keys where key = '$k'");
if ($row=$results->fetchArray())
        $key=$row["value"];
if ($key != "") {

fwrite($f,"key=$key\r\n");

$registrationIds =  array($key) ;

$msg = array
(
    'title'     => 'Notification',
    'body'        =>'Wake Up'

);

$fields = array
(
    'registration_ids'    => $registrationIds,
    'data'                => $msg,
    'priority' =>10
);

$headers = array
(
    'Authorization: key=' . $API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'
);
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );

curl_close( $ch );

fwrite($f,"$result\r\n");
} // if key
fclose($f);
?>
1 Like