Pushover Notifications using HTTP


(Concipio) #1

I’ve just finished updating my previously developed “Something Left Open” SmartApp to allow for optional notifications to be sent and controlled using the Pushover service (http://pushover.net) for mobile devices. Pushover allows you to control which devices certain notifications go to and also allows users to specify the level of notification (low, normal, high, emergency) for an alert. To use this capability, you must sign up for Pushover account (FREE) to obtain a “User Key” and register a Pushover application (FREE) with your account to obtain an “API Key” (7,500 free notifications per month!). The following code shows you how to obtain the configuration information from the user during SmartApp installation and send Pushover notifications using the user inputs.

Configuration Information:

section("Send Pushover alert (optional)"){
        input "apiKey", "text", title: "Pushover API Key", required: false
        input "userKey", "text", title: "Pushover User Key", required: false
        input "deviceName", "text", title: "Pushover Device Name", required: false
        input "priority", "enum", title: "Pushover Priority", required: false,
        metadata :[
           values: [ 'Low', 'Normal', 'High', 'Emergency'
           ]
        ]
    }

Sending Pushover Notification:

def onContactLeftOpenHandler() {
	log.debug "onContactLeftOpenHandler";
    log.debug "Something Left Open...Sending Notifications!"
    
	sendPush(messageText);
      
    if(phoneNumber)
    {
      log.debug "Sending to Phone"
      sendSms(phoneNumber, messageText);
    }
      
    if(alertDevice)
    {
      log.debug "Sending Beep to Device"
      alertDevice.beep();
    }
    
    if(apiKey && userKey)
    {
      log.debug "Sending Pushover with API Key [$apiKey] and User Key [$userKey]"
      
      def postBody = []
      def pushPriority = 0
      
      // Set Priority for Pushover Notification
      if(priority == "Low")
      {
        pushPriority = -1
      }
      else if(priority == "Normal")
      {
        pushPriority = 0
      }
      else if(priority == "High")
      {
        pushPriority = 1
      }
      else if(priority == "Emergency")
      {
        pushPriority = 2
      }
      
      if(deviceName)
      {
        log.debug "Sending Pushover to Device: $deviceName"
        
        if(pushPriority == 2)
        {
          postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: "$messageText", priority: "$pushPriority", retry: "60", expire: "3600"]
        }
        else
        {
          postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: "$messageText", priority: "$pushPriority"]
        }
        
        log.debug postBody
      }
      else
      {
        log.debug "Sending Pushover to All Devices"
        
        if(pushPriority == 2)
        {
          postBody = [token: "$apiKey", user: "$userKey", message: "$messageText", priority: "$pushPriority", retry: "60", expire: "3600"]
        }
        else
        {
          postBody = [token: "$apiKey", user: "$userKey", message: "$messageText", priority: "$pushPriority"]
        }
        
        log.debug postBody
      }
      
      def params = [
      		uri: 'https://api.pushover.net/1/messages.json',
            body: postBody
            ]
      
      httpPost(params){ response ->
          log.debug "Response Received: Status [$response.status]"
          
          if(response.status != 200)
          {
            sendPush("Received HTTP Error Response. Check Install Parameters.")
          }
      }
    }
}

(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Great example of device-to-cloud-to-cloud-to-device!

Thanks.