Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Microsoft Push Notification Service (Windows Phone) #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 68 additions & 1 deletion README.md
Expand Up @@ -12,7 +12,7 @@ Pushmeup is an attempt to create an push notifications center that could send pu
- Windows Phone
- And many others

Currently we have only support for ``iOS`` and ``Android`` but we are planning code for more plataforms.
Currently we have only support for ``iOS``, ``Android`` and ``Windows Phone`` but we are planning code for more plataforms.

## Installation

Expand Down Expand Up @@ -185,6 +185,73 @@ You can use multiple keys to send notifications, to do it just do this changes i
GCM.send_notifications( [n1, n2, n3] )
# In this case, every notification has his own parameters, options and key

## MPNS (Microsoft Push Notification Service)

#### Sending a single notification:

destination = ["device_url_1", "device_url_2", "device_url_3"]
# can be an string or an array of strings containing the device push url's of the devices you want to push to

title = 'My title'
# Notification title

message = 'My message'
# Notification message (optional)

data = {:key => "value", :key2 => ["array", "value"]}
# must be an hash with all values you want inside your custom notification data (optional)

MPNS.send_notification( destination, title )
# Notification with only title

MPNS.send_notification( destination, title, message )
# Notification with title and message

MPNS.send_notification( destination, title, message, data )
# Notification with title, message and custom data

MPNS.send_notification( destination, title, message, data, options = { notification_class: 12 } )
# Notification with title, message, custom data and custom options

for more information on parameters check documentation: [Windows Phone Dev Center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202945)

#### Sending multiple notifications:

destination1 = "device_url_1"
destination2 = ["device_url_2"]
destination3 = ["device_url_1", "device_url_2", "device_url_3"]
# can be an string or an array of strings containing the regIds of the devices you want to send

title = 'My title'
# Notification title

message = 'My message'
# Notification message

data1 = {:key => "value", :key2 => ["array", "value"]}
# must be an hash with all values you want inside you notification

options = { notification_class: 12 }
# options for the notification

n1 = MPNS::Notification.new(destination1, title, message, data1, options)
n2 = MPNS::Notification.new(destination2, title, message, data2, options)
n3 = MPNS::Notification.new(destination3, title, message, data3, options)

MPNS.send_notifications( [n1, n2, n3] )
# In this case, every notification has his own parameters

for more information on parameters check documentation: [Windows Phone Dev Center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202945)

#### Getting your Microsoft Push Notification device url (device_url)

Check this link [Windows Phone dev center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202940)
and this for a detailed example [Windows Phone dev center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967)

#### Known limitations

- Currently only supports 'Toast' notifications. No support for 'Raw' or 'Tile' notifications (yet).

## Status

#### Build Status [![Build Status](https://secure.travis-ci.org/NicosKaralis/pushmeup.png?branch=master)](http://travis-ci.org/NicosKaralis/pushmeup) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/NicosKaralis/pushmeup)
Expand Down
Binary file added lib/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions lib/pushmeup.rb
@@ -1,3 +1,4 @@
require "pushmeup/version"
require "pushmeup/apple"
require "pushmeup/android"
require "pushmeup/windows_phone"
44 changes: 44 additions & 0 deletions lib/pushmeup/mpns/core.rb
@@ -0,0 +1,44 @@
module MPNS

@pem = nil

class << self
attr_accessor :pem
end

def self.send_notification(device_url, title, message = '', data = {}, options = {})
n = MPNS::Notification.new(device_url, title, message, data, options)
self.send_notifications([n])
end

def self.send_notifications(notifications)
responses = []
notifications.each do |n|
responses << self.send(n)
end
responses
end

protected

def self.send(n)
return self.send_to_server(n.device_url, n.packaged_message)
end

def self.send_to_server(url, body)
# Had to use system call to curl since we could't get client authentication to work from within ruby (Net::HTTP)
# Details here: http://stackoverflow.com/questions/16603814/connect-to-microsoft-push-notification-service-for-windows-phone-8-from-ruby
system "curl --cert #{@pem} -H \"Content-Type:text/xml\" -H \"X-WindowsPhone-Target:Toast\" -H \"X-NotificationClass:2\" -X POST -d \"#{body}\" #{url}"
return build_response($?.success?)
end

def self.build_response(response)
case response
when true
{:response => 'success'}
else
{:response => 'failure'}
end
end

end
21 changes: 21 additions & 0 deletions lib/pushmeup/mpns/notification.rb
@@ -0,0 +1,21 @@
module MPNS
class Notification
attr_accessor :device_url, :title, :message, :data

def initialize(device_url, title, message = '', data = {}, options = {})
self.device_url = device_url
self.title = title
self.message = message
self.data = data
end

def packaged_message
data_params = ''
self.data.each_pair do |key, value|
data_params += "<wp:#{key.capitalize}>#{value}</wp:#{key.capitalize}>"
end
"<?xml version='1.0' encoding='utf-8'?><wp:Notification xmlns:wp='WPNotification'><wp:#{@target.capitalize}><wp:Text1>#{self.title}</wp:Text1><wp:Text2>#{self.message}</wp:Text2>#{data_params}</wp:#{@target.capitalize}></wp:Notification>"
end

end
end
2 changes: 2 additions & 0 deletions lib/pushmeup/windows_phone.rb
@@ -0,0 +1,2 @@
require "pushmeup/mpns/core"
require "pushmeup/mpns/notification"