AWAITING FEEDBACK Mobile push notifications within AGI?

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Update: Success! See my post further down......

Specifically using an agi script file to craft push notifications to android phones when a call arrives. This is for a mobile project that is using Asterisk to process inbound calls. The dialplan currently uses an agi script which plucks callerid data and sends it to an external DB. I'd like to add the capability of sending push notifications from within the asterisk environment.

Call comes in->agi script inserts the callerid data to a remote DB and kicks off a push notification to android devices. At least that's what I hope can be done, it's that simple.

I have two boxes to use for testing, one running PIAF Green (asterisk 11), and a vanilla asterisk 11 (ubuntu) install. I have cURL installed on the ubuntu machine currently. I can import the variables I need from the dialplan into the script, and also have working php code to handle the push notification piece. The questions I do have surround:

1) two include files handling android push (php)- how do I include them properly within an agi script. where do I put the include files? if they remain in the ../asterisk/agi-bin folder, do I need to rename them as .agi files? Or do I need to incorporate the includes into the agi script.

2) any potential issues using cURL in an asterisk environment? cURL would be used to format and send variables using POST to an external url (https://android.googleapis.com/gcm/send in this case).

Thank you for any tips or advice, I'm not entirely sure how to proceed without filling in some gaps I have from the questions above. cURL is installed on the vanilla ubuntu asterisk machine, and again, I also have a PIAF Green to test with as well. I'd also be happy to post any code if it will help anyone understand exactly what I'm trying to do.
 

phonebuff

Guru
Joined
Feb 7, 2008
Messages
1,117
Reaction score
129
Interesting, It will be mid June before I have any bandwidth, but if you are still stuck I may take a shot at this with you..
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
I hope to heck I have it solved (or ruled out) by mid-June phonebuff. I can't find any guidance on the net or anyone attempting anything like this.
 

bobkoure

Member
Joined
May 22, 2013
Messages
173
Reaction score
20
Are you looking to initiate a VOIP connection (or start a PSTN callback) when a notification gets to the mobile device?
I looked at this a while back - forum chatter was that GCM latency varied wildly. It may have improved.

There's also an alternative notification system: PubNub - supposed to be lower latency and works on both Android and iOS.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Are you looking to initiate a VOIP connection (or start a PSTN callback) when a notification gets to the mobile device?.....


The reverse actually. What I hope to accomplish is to send a push notification in response to an incoming call. Right now, when a call comes into the server, it's routed through a custom dialplan and agi file that parses the CID/RDNIS numbers from the invite packet and fills an external db.

I hope to spend time with this over the weekend and next week. Thanks lgaetz for your help, and I will update the thread as I progress.
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,206
Reaction score
5,227
jehowe: I'm probably missing something, but if all you want to do is notify, keep reading...

We use a combination of Jabber, Twitter, and SMS notifications that are all tied to a "notify" extension. There are two ways to do this. Use billsimon "notify extension" and add it to a ring group with a regular extension. Or modify your inbound trunk which is covered in our Nerd Vittles tutorial. Here's our notify extension code since Bill's tutorial was lost in "The Great Crash." It would obviously work equally well with the SMS code posted last week for VoIP.ms, and Anveo is also easy using their API.

Code:
;# // BEGIN SMS/Jabber Alert for Incoming Calls
exten => 25378,1,Set(GVACCT=mundyxyz) ; your Google Voice account name without @gmail.com
exten => 25378,n,Set(GVPASS=password) ; your Google Voice account password
exten => 25378,n,Set([email protected]) ; Gmail address to receive Jabber alerts
exten => 25378,n,Set(SMS_NOTIFY=8431234567) ; your cellphone number to receive SMS alters
exten => 25378,n,Playtones(ring)
exten => 25378,n,JABBERSend(${GVACCT},${EMAIL_NOTIFY},"Incoming call: ${CALLERID(name)} ${CALLERID(num)}")
exten => 25378,n,System(gvoice -e ${GVACCT}@gmail.com -p ${GVPASS} send_sms ${SMS_NOTIFY} "Incoming call: ${CALLERID(name)} ${CALLERID(num)}")
;exten => 25378,n,AGI(nv-twitter.php,${CALLERID(name)},${CALLERID(num)})
;# // END SMS/Jabber Alert for Incoming Calls
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Thanks Ward. I've used the gvoice module to send SMS replies, but in this case we need to use standard push notifications. The reason behind using push is because, frankly, push is proving to be more reliable than SMS and faster to boot. But the big reason against using SMS for our purposes is that these notifications are triggered as a consequence of the phones (all mobile) being off a carrier network. While SMS is constrained to carrier data for delivery, push has no constraints and can use carrier data or wifi.

Hope that sheds some light on what I'm trying to accomplish and I was traveling all last week, but will be actively working integrating an android push solution this week. I'll be sure to post any findings and appreciate any feedback.
 

tm1000

Schmoozecom INC/FreePBX
Joined
Dec 1, 2009
Messages
1,360
Reaction score
78
Wards post is actually exactly what you need. You just need to look deeper into it. In Wards dialplan he is accessing external scripts to post to twitter, which is what you could do if you want to post to the Android service. Same concept, just remove his SMS line and use the system() application or the agi() application and in this case I dont think you even need to use the AGI application

Thanks Ward. I've used the gvoice module to send SMS replies, but in this case we need to use standard push notifications. The reason behind using push is because, frankly, push is proving to be more reliable than SMS and faster to boot. But the big reason against using SMS for our purposes is that these notifications are triggered as a consequence of the phones (all mobile) being off a carrier network. While SMS is constrained to carrier data for delivery, push has no constraints and can use carrier data or wifi.


Hope that sheds some light on what I'm trying to accomplish and I was traveling all last week, but will be actively working integrating an android push solution this week. I'll be sure to post any findings and appreciate any feedback.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Thanks tm1000, i'll look deeper. One issue is the service we're building isn't for one phone, it's an off-network call mobile notification platform with both android & iOS app support. We're hoping to serve potentially 10's of thousands of unique mobile numbers.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Ok, had a chance to work on this today. I'm currently testing on ubuntu 12.04 with vanilla asterisk 11 installed. cURL and the php5-curl extension is installed, and json encode is installed as it's included in php5. The agi file processing the push notification is called from the dialplan, and is passed two variables. The console confirms the variables are being passed correctly, and the agi file appears to run normally.

Unfortunately, it's not working yet- I may be missing an extension or missing package, but I'm not sure. I have success running the exact file contents (with variables hard coded) in another ubuntu 12.04 LAMP (asterisk-less) server as a php file in the root apache2 directory.

extensions.conf line:
Code:
exten => _X.,n,AGI(/var/lib/asterisk/agi-bin/gcmpush.agi, ${cid}, ${cnum})

And gcmpush.agi file:
Code:
#!/usr/bin/php -q
<?php
 
        $con1=mysqli_connect("external-db-host", "db-user", "db-user-pw", "db-name");
 
        if (mysqli_connect_errno())
          {
          echo  mysqli_connect_error();
          exit();
          }
 
        $callerid = $argv[1];
        $callednum = $argv[2];
 
        $query = "SELECT gcmregid FROM users WHERE num = '$callednum'";
        $query_res = mysqli_query($con1, $query);
        $result = mysqli_fetch_assoc($query_res);
        $gcmregid = $result['gcmregid'];
 
 
define( 'API_ACCESS_KEY', 'GOOGLE-RANDOM-API-KEY' );
 
 
$registrationIds = array( $gcmregid );
 
$msg = array
(
    'message'        => 'Push Message',
    'title'            => 'Notification',
    'subtitle'        => $callerid,
    'vibrate'    => 1,
    'sound'        => 1
);
 
$fields = array
(
    'registration_ids'    => $registrationIds,
    'data'                => $msg
);
 
$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);
 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
 
?>

The script itself is pretty simple really. It takes in two numbers from the dialplan and assigns them as variables, runs a db query to pull out the unique device push registration token, and then uses curl to package up the notification in json and fire it at the google push api url.

Again, I can use the same code (with variables hard coded) in a regular LAMP box and it works. Through the console, I can see the dialplan variables being passed correctly and the agi file appears to run normally. I have another script in front of gcmpush.agi in the dialplan that performs a db insert to the same database, so it seems unlikely that this is a db issue.

I'll continue to work on this, and will post updates.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Update: The Ubuntu 12.04 server with vanilla asterisk that I'm using to test has LAMP installed. I copied the file (push.php) to the apache root directory with hard coded variables and it works from there.

I don't understand the nuances of asterisk's agi environment well enough to know why it isn't working from the agi-bin directory. Permissions aren't the issue.

I'm stumped, but will give it a go tomorrow.
 

james

Guru
Joined
Oct 18, 2007
Messages
374
Reaction score
38
I am guilty of skimming so this response may be out of scope: For push notifications I like the push bullet API. I actually have pushbullet on my tablet, phone and browsers.
 

tm1000

Schmoozecom INC/FreePBX
Joined
Dec 1, 2009
Messages
1,360
Reaction score
78
Alternatively you could attach the debugging tools that are provided by FreePBX into your script which would help you out immensely.

See: http://wiki.freepbx.org/display/DC/Bootstrap

so you can do this:

Code:
<?php
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
      include_once('/etc/asterisk/freepbx.conf');
}
 
dbug('this is my message')

Then from the CLI (not asterisk CLI, terminal CLI). Just run: amportal a dbug

and when you make the call you will see the output. You can also throw arrays into dbug and they will echo out nicely to the screen. Then when everything is working just remove all the freepbx bootstrap stuff.

Update: The Ubuntu 12.04 server with vanilla asterisk that I'm using to test has LAMP installed. I copied the file (push.php) to the apache root directory with hard coded variables and it works from there.

I don't understand the nuances of asterisk's agi environment well enough to know why it isn't working from the agi-bin directory. Permissions aren't the issue.

I'm stumped, but will give it a go tomorrow.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Alternatively you could attach the debugging tools that are provided by FreePBX into your script which would help you out immensely.

See: http://wiki.freepbx.org/display/DC/Bootstrap....

Thanks tm1000, still wrestling with the vanilla asterisk version but will move onto freePBX if only for the debug tools if I can't get anywhere..... I haven't spent much time with it today. LAMP is installed on this box, and push works brilliantly with coded variables from the root apache directory.

It feels like maybe curl, or the php5-json extension, can't run beyond the root apache folder.
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,206
Reaction score
5,227
Try adding some entries to /etc/sudoers. That might solve any permissions issues. Could be that you need full paths to the executables as well.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Solved the issue, finally! Working quite well! For reasons not entirely clear, I had to format the mysqli query in the php agi file differently to get it going. The original query works perfectly when run within the apache root directory, so this was very elusive. After much hair pulling, things pointed back to a mysql issue when I couldn't pass the result back into the console via noop.
 

phonebuff

Guru
Joined
Feb 7, 2008
Messages
1,117
Reaction score
129
If you have time, I was how this approach and solution is working out for you ?

TIA, Phonebuff
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Phonebuff - Sorry I haven't checked in lately..... It's going well and will be used in a production environment next month. We've ramped up testing to ~40 simultaneous calls hitting the agi file that does the work- flawless so far. You really don't need much to kick off push notifications, so the agi file itself is small and tight. We're going to load balance SIP assets to try to get around any peak capacity bottlenecks.

I'm really happy we were able to do this from the asterisk box itself, as inbound calls are the trigger for these notifications. The alternative was to have another box watch a mysql table, which we could have done, but this is a better solution.
 

Members online

Forum statistics

Threads
25,824
Messages
167,826
Members
19,249
Latest member
jetest
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top