Page 1 of 1

Prowl - iPhone push notification

Posted: Fri Oct 18, 2013 3:38 pm
by Osler
I have been absent a while but wanted to share a COM component I just wrote for Prowl ( http://www.prowlapp.com ).

It's a simple device. Source code included below. If you alter the code and recompile please generate a new Guid for the COM and class interfaces in the attribute decorations:

Code: Select all

Imports System.Net
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Runtime.InteropServices



Namespace Prowl

    <ComVisible(True)> _
    <Guid("0F7D00C3-CF88-4D4A-AA6F-72866C432D78"), _
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
    Public Interface IProwlMessenger
        Property APIKey As String
        Property NotificationURL As String
        Property NotificationApplication As String
        Property NotificationEvent As String
        Property NotificationDescription As String
        Property ProviderKey As String
        Property Priority As Integer
        Function SendNotification() As String
    End Interface

    <ComVisible(True)> _
    <Guid("7E0547BE-6C28-4DDA-AD8F-3A2BD5394F31"), _
    ClassInterface(ClassInterfaceType.None), _
    ProgId("Prowl.Messenger")>
    Public Class Messenger
        Implements IProwlMessenger

        Dim _apiKey As String = ""
        Dim _url As String = ""
        Dim _application As String = ""
        Dim _event As String = ""
        Dim _description As String = ""
        Dim _providerKey As String = ""
        Dim _priority As Integer = 0
        Dim _timeFormat As String = "MM-dd-yyyy HH:mm:ss"

        Public Sub New()
            MyBase.New()
        End Sub

        Public Function SendNotification() As String Implements IProwlMessenger.SendNotification
            If _apiKey = "" Or _event = "" Or _description = "" Or _application = "" Then
                Return "The apikey, event, description, and application need to be set prior to sending notification."
                Exit Function
            Else
                Dim prowlAddress As Uri
                Dim request As HttpWebRequest
                Dim response As HttpWebResponse = Nothing
                Dim data As StringBuilder
                Dim byteData() As Byte
                Dim postStream As Stream = Nothing
                'Dim reader As StreamReader

                prowlAddress = New Uri("https://api.prowlapp.com/publicapi/add")

                request = DirectCast(WebRequest.Create(prowlAddress), HttpWebRequest)
                request.Method = "POST"
                request.ContentType = "application/x-www-form-urlencoded"

                data = New StringBuilder
                data.Append("apikey=" & System.Uri.EscapeDataString(_apiKey))
                data.Append("&providerkey=" & System.Uri.EscapeDataString(_providerKey))
                data.Append("&priority=" & System.Uri.EscapeDataString(_priority.ToString))
                data.Append("&application=" & System.Uri.EscapeDataString(_application))
                data.Append("&event=" & System.Uri.EscapeDataString(_event))
                data.Append("&description=" & System.Uri.EscapeDataString(_description))

                byteData = UTF8Encoding.UTF8.GetBytes(data.ToString)

                request.ContentLength = byteData.Length

                Try
                    postStream = request.GetRequestStream()
                    postStream.Write(byteData, 0, byteData.Length)
                Catch wex As WebException
                    Return wex.Message.ToString
                Finally
                    If Not postStream Is Nothing Then postStream.Close()
                End Try

                Try
                    response = DirectCast(request.GetResponse(), HttpWebResponse)
                    Return response.StatusCode.ToString
                Catch wex As WebException
                    Return wex.Message.ToString
                Finally
                    If Not response Is Nothing Then response.Close()
                End Try

                response = Nothing
                postStream = Nothing
                request = Nothing
                byteData = Nothing
                data = Nothing
                postStream = Nothing

            End If
        End Function

#Region "Public Properties"
        Public Property Priority() As Integer Implements IProwlMessenger.Priority
            Get
                Return _priority
            End Get
            Set(value As Integer)
                _priority = value
                If _priority < -2 Then _priority = -2
                If _priority > 2 Then _priority = 2
            End Set
        End Property

        Public Property APIKey() As String Implements IProwlMessenger.APIKey
            Get
                Return _apiKey
            End Get
            Set(value As String)
                _apiKey = value
            End Set
        End Property

        Public Property NotificationEvent() As String Implements IProwlMessenger.NotificationEvent
            Get
                Return _event
            End Get
            Set(value As String)
                _event = Left(value, 1024)
            End Set
        End Property

        Public Property NotificationURL() As String Implements IProwlMessenger.NotificationURL
            Get
                Return _url
            End Get
            Set(value As String)
                _url = Left(value, 512)
            End Set
        End Property

        Public Property NotificationApplication() As String Implements IProwlMessenger.NotificationApplication
            Get
                Return _application
            End Get
            Set(value As String)
                _application = Left(value, 256)
            End Set
        End Property

        Public Property NotificationDescription() As String Implements IProwlMessenger.NotificationDescription
            Get
                Return _description
            End Get
            Set(value As String)
                _description = Left(value & " " & Now.ToString(_timeFormat), 10000)
            End Set
        End Property

        Public Property ProviderKey() As String Implements IProwlMessenger.ProviderKey
            Get
                Return _providerKey
            End Get
            Set(value As String)
                _providerKey = Left(value, 40)
            End Set
        End Property
#End Region

    End Class
End Namespace
Osler

Re: Prowl - iPhone push notification

Posted: Fri Nov 01, 2013 4:43 am
by Richard Naninck
I did about the same thing using Windows Phone 8 together with PushALot.
PushALot is a free App for WP8 and the complete api is very well explained on the website. I now get push notifications on my WP8 produced by HouseBot. very nice!