4elements web design & consultancy



  • dislike -3 02

    Generate Notifications From Your Web App With the Pushover API

    4elements | web design Den Haag blog • Generate Notifications From Your Web App With the Pushover API

    Final product image
    What You'll Be Creating

    Pushover is one of my favorite services over the past few years; it makes it easy and inexpensive for developers to send push notifications to iOS and Android devices without having to develop their own application. 

    It also recently added desktop browser notifications as well. The service is largely free for low level usage and the apps only require a $4.99 fee after a five-day free trial; there is no monthly fee. Working with its API is straightforward and there are libraries available for a handful of different languages.

    While a number of front-end app developers integrate with Pushover, e.g. IT, I find it most useful as a developer for receiving notifications related to my own applications and web servers. For example, I've integrated Pushover into my Simple Monitoring application to alert me to website failures as well as sending twice daily heartbeat notifications when nothing is wrong. If MySQL fails or my DNS goes down, I'll receive a speedy alert to my iPhone. 

    Similarly, I integrated Pushover into SimplifyEmail to send notifications when emails with specific keywords arrive such as "urgent" or from specific senders such as "Tom McFarlin" (Envato's Code editor).

    Here's what Pushover alerts look like on your phone:

    Sample heartbeat and failure alerts from Simple Monitor using Pushover

    Pushover offers a variety of sounds to customize notifications and it also supports user-configurable quiet hours, so you don't have to be woken up in the middle of the night unless you want to be:

    Configure Quiet Hours within Pushover

    Just remember, real SysAdmins don't sleep!

    Getting Started With Pushover

    To get started with Pushover, visit the website and sign up:

    Sign Up for Pushover

    Then, install either mobile application or the desktop browser notification support. Each offers five days of free usage before you'll be required to purchase each for $4.99. For the iOS and Android apps, you can just download them from the app stores.

    Install Pushover for iOS

    When you install the mobile app, it will ask you to log in and specify a device name of your choosing, e.g. iPhone.

    Setting up the desktop browser-based notifications is a bit different. Visit the Pushover for Desktop page and choose a simple name for your Pushover browser notifications:

    Choose Your Desktop Notification Device Name

    Instruct your browser to allow notifications:

    Then, you'll see the Pushover desktop in-browser app:

    The Pushover Browser-based App

    Ultimately, desktop notifications from Pushover will look like this:

    Example of a Pushover Web Notification

    Once you have installed Pushover on your device and web browsers, you can begin sending test notifications from the Pushover website:


    But the fun is just beginning—let's get started using the Pushover API.

    Developing With the Pushover API

    Pushover requires that you register each application. Just visit your apps page:

    The Pushover Apps Page

     And register a new application:

    Create a New Pushover Application

    You'll be given an API token to use to send notifications from your application:

    An API Token for Pushover Apps

    The Pushover home page offers some simple examples of sending notifications with curl:

    curl_setopt_array($ch = curl_init(), array(
      CURLOPT_URL => "",
      CURLOPT_POSTFIELDS => array(
        "token" => "your-api-token",
        "user" => "your-user-id",
        "message" => "hello world",
      CURLOPT_SAFE_UPLOAD => true,

    But, for Simple Monitor and Simplify Email, I use Chris Schalenborgh's PHP Pushover library. For example, in SimpleMonitor, I use a background CronController that runs all of the tests the end user configures through the web user interface: 

    class CronController extends Controller
      public function actionIndex()
          $result = Content::model()->testAll();

    Here's an example of Simple Monitor's list of my checks which trigger different Pushover notification sounds:

    Using Pushover Sounds with Simple Monitor

    My TestAll method, called by cron, processes each of the user-configured tests. If there is a failure, it notifies each registered device. If there is no failure, it determines whether it's time to resend a heartbeat notification, letting me know the monitor is still up and running:

    public function testAll() {
          $str = '';
            $errors = false;	  
      	  // monitor all content items
          $checks = Content::model()->findAll();
          foreach ($checks as $item) {
            // perform the test
            $str.='<p>Checking '.$item['name'].'...';
            $result = Content::model()->test($item['id']);
            // if there is an error send notification to the device
            if (!$result->status) {
              if ($item['type']==self::TYPE_CHANGES)
                $temp_result_string = 'Page Changed';
                $temp_result_string = 'Failed';
              $str.=$temp_result_string.'<br />'; 
              $str.='Please check <a href="'.$item['url'].'">'.$item['url'].'</a><br />';
               if ($item['device_id']==0) {
                 //  send to all devices
                 $devices = Device::model()->findAll();
                 foreach ($devices as $device) {
                   Content::model()->notify($device,$item['name'].' '.$temp_result_string,'Please check into...',$item['url'],'this page',1,$item['sound']);                            
                   $str.='Notifying '.$device['name'].'<br />';
               } else {
                 $device = Device::model()->findByPk($item['device_id']);
                 Content::model()->notify($device,$item['name'].' '.$temp_result_string,'Please check into...',$item['url'],'this page',1,$item['sound']) ;               
                 $str.='Notifying '.$device['name'].'<br />';
              $errors = true;
            } else {
          // check for sending heartbeart
          if (!$errors) {
            // only notify me with heartbeat every heartbeat_interval hours
            // note: cron must run every ten minutes or change 10 below to fit your interval
            // the point of date('i')<10 is to send heartbeat only in first part of any hour
            $setting = Setting::model()->getSettings();
            if ((date('G')% $setting['pushover_heartbeat_interval']) == 0 and date('i')<10) {
              $str.='<p>Heartbeat sent.</p>';
            } else {
              $str.='<p>Skipped heartbeat for now.</p>';        
      	  return $str;    

    My Notify method is what actually calls the Pushover PHP library to send the notifications. This is what you might use in your own application:

      // send notification to admin mobile device via pushover
      public function notify($device,$title='',$message='',$url='',$urlTitle='',$priority=1,$sound='gamelan',$debug=false) {
        if ($device['send_email']<>Device::SEND_EMAIL) {
          // load pushover key from Settings
          $setting = Setting::model()->getSettings();
          $po = new Pushover();
          if ($url<>'') {
          if ($urlTitle<>'') {
          $go = $po->send();
          if ($debug) {
            echo '<pre>';
            echo '</pre>';      

    As a developer, I've found Pushover to be tremendously useful for delivering notifications in the absence of a dedicated mobile application. To me, Pushover's mobile app is like a SysAdmin dashboard that I didn't have to build. But it's also great for sending notifications for important emails or other server events. It's also fun to prank your friends if you can get hold of their user tokens and device names; but I would never do that.


    I hope you enjoy using Pushover as much as I have. If you'd like to explore alternative services to Pushover, check out Boxcar and Panacea. I'd love to hear your thoughts. 

    Please post any comments, corrections, or additional ideas below. You can browse my other Tuts+ tutorials on my author page or follow me on Twitter @reifman.

    martijn broeders

    founder/ strategic creative bij 4elements web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    telefoon: 06 38 55 28 54

Per - categorie

    Op - datum