diff --git a/README.md b/README.md index c48cd3c..b405758 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/pushmeup.rb b/lib/pushmeup.rb index f60f14f..926d786 100644 --- a/lib/pushmeup.rb +++ b/lib/pushmeup.rb @@ -1,3 +1,4 @@ require "pushmeup/version" require "pushmeup/apple" require "pushmeup/android" +require "pushmeup/windows_phone" \ No newline at end of file diff --git a/lib/pushmeup/mpns/core.rb b/lib/pushmeup/mpns/core.rb new file mode 100644 index 0000000..11522f2 --- /dev/null +++ b/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 diff --git a/lib/pushmeup/mpns/notification.rb b/lib/pushmeup/mpns/notification.rb new file mode 100644 index 0000000..b68bb33 --- /dev/null +++ b/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 += "#{value}" + end + "#{self.title}#{self.message}#{data_params}" + end + + end +end diff --git a/lib/pushmeup/windows_phone.rb b/lib/pushmeup/windows_phone.rb new file mode 100644 index 0000000..7fb542b --- /dev/null +++ b/lib/pushmeup/windows_phone.rb @@ -0,0 +1,2 @@ +require "pushmeup/mpns/core" +require "pushmeup/mpns/notification"