From 2a4995dea69296daa6c4c8e3c9a9b4b0c99aea7e Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Wed, 8 May 2013 12:02:03 +0200 Subject: [PATCH 1/6] Adding support for Microsoft Push Notification Service (used to push to Windows Phone devices) --- lib/.DS_Store | Bin 0 -> 6148 bytes lib/pushmeup.rb | 1 + lib/pushmeup/mpns/core.rb | 40 ++++++++++++++++++++++++++++++ lib/pushmeup/mpns/notification.rb | 33 ++++++++++++++++++++++++ lib/pushmeup/windows_phone.rb | 2 ++ 5 files changed, 76 insertions(+) create mode 100644 lib/.DS_Store create mode 100644 lib/pushmeup/mpns/core.rb create mode 100644 lib/pushmeup/mpns/notification.rb create mode 100644 lib/pushmeup/windows_phone.rb diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 headers, :body => body} + response = self.post(url, params) + return build_response(response) + end + + def self.build_response(response) + case response.code + when 200 + {:response => 'success', :headers => response.headers, :status_code => response.code} + else + {:response => response.body, :headers => response.headers, :status_code => response.code} + end + end + +end diff --git a/lib/pushmeup/mpns/notification.rb b/lib/pushmeup/mpns/notification.rb new file mode 100644 index 0000000..27e97ea --- /dev/null +++ b/lib/pushmeup/mpns/notification.rb @@ -0,0 +1,33 @@ +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 + + @content_type = 'text/xml' + @target = 'toast' + @notification_class = options[:notification_class] ? options[:notification_class] : '2' + end + + def headers + { + 'Content-Type' => @content_type, + 'X-WindowsPhone-Target' => @target, + 'X-NotificationClass' => @notification_class + } + 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" From b521273c6aa3ee8de7f27c1b13cdc9f40b197627 Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Wed, 8 May 2013 15:06:37 +0200 Subject: [PATCH 2/6] Adding readme text for Microsoft Push Notification Service support --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index c48cd3c..5a64543 100644 --- a/README.md +++ b/README.md @@ -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(v=vs.105).aspx) + +#### 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(v=vs.105).aspx) + +#### 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(v=vs.105).aspx) +and this for a detailed example [Windows Phone dev center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967(v=vs.105).aspx) + +#### 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) From f552082a07df1157ae272e9e45c8befc25439f05 Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Wed, 8 May 2013 15:09:36 +0200 Subject: [PATCH 3/6] Added Windows Phone as supported platform in the readme introduction text --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a64543..bab2cd7 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 From fe7d5afe81279fd80edb7a4bcf544258ac3b7a18 Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Wed, 8 May 2013 15:16:31 +0200 Subject: [PATCH 4/6] Fixing broken links in readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bab2cd7..b405758 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ You can use multiple keys to send notifications, to do it just do this changes i 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(v=vs.105).aspx) +for more information on parameters check documentation: [Windows Phone Dev Center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202945) #### Sending multiple notifications: @@ -241,12 +241,12 @@ for more information on parameters check documentation: [Windows Phone Dev Cente 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(v=vs.105).aspx) +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(v=vs.105).aspx) -and this for a detailed example [Windows Phone dev center](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967(v=vs.105).aspx) +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 From bcdb739133f9c0a0e9e3a06a2a9dace0790ceaf2 Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Fri, 17 May 2013 17:36:47 +0200 Subject: [PATCH 5/6] Couldn't get client authentication to work from ruby (see comment for details) so we had to make a system call to curl instead --- lib/pushmeup/mpns/core.rb | 28 ++++++++++++++++------------ lib/pushmeup/mpns/notification.rb | 10 +--------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/pushmeup/mpns/core.rb b/lib/pushmeup/mpns/core.rb index fa8834b..11522f2 100644 --- a/lib/pushmeup/mpns/core.rb +++ b/lib/pushmeup/mpns/core.rb @@ -1,7 +1,10 @@ -require 'httparty' - module MPNS - include HTTParty + + @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) @@ -19,21 +22,22 @@ def self.send_notifications(notifications) protected def self.send(n) - return self.send_to_server(n.device_url, n.headers, n.packaged_message) + return self.send_to_server(n.device_url, n.packaged_message) end - def self.send_to_server(url, headers, body) - params = {:headers => headers, :body => body} - response = self.post(url, params) - return build_response(response) + 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.code - when 200 - {:response => 'success', :headers => response.headers, :status_code => response.code} + case response + when true + {:response => 'success'} else - {:response => response.body, :headers => response.headers, :status_code => response.code} + {:response => 'failure'} end end diff --git a/lib/pushmeup/mpns/notification.rb b/lib/pushmeup/mpns/notification.rb index 27e97ea..6b13c8f 100644 --- a/lib/pushmeup/mpns/notification.rb +++ b/lib/pushmeup/mpns/notification.rb @@ -12,21 +12,13 @@ def initialize(device_url, title, message = '', data = {}, options = {}) @target = 'toast' @notification_class = options[:notification_class] ? options[:notification_class] : '2' end - - def headers - { - 'Content-Type' => @content_type, - 'X-WindowsPhone-Target' => @target, - 'X-NotificationClass' => @notification_class - } - end def packaged_message data_params = '' self.data.each_pair do |key, value| data_params += "#{value}" end - "#{self.title}#{self.message}#{data_params}" + "#{self.title}#{self.message}#{data_params}" end end From 91265479f3a831fabfb16e5be3108238da184248 Mon Sep 17 00:00:00 2001 From: Daniel Grahn Date: Fri, 17 May 2013 17:49:27 +0200 Subject: [PATCH 6/6] Removing some unused header variables for notifications. Not needed anymore after previous commit. --- lib/pushmeup/mpns/notification.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/pushmeup/mpns/notification.rb b/lib/pushmeup/mpns/notification.rb index 6b13c8f..b68bb33 100644 --- a/lib/pushmeup/mpns/notification.rb +++ b/lib/pushmeup/mpns/notification.rb @@ -7,10 +7,6 @@ def initialize(device_url, title, message = '', data = {}, options = {}) self.title = title self.message = message self.data = data - - @content_type = 'text/xml' - @target = 'toast' - @notification_class = options[:notification_class] ? options[:notification_class] : '2' end def packaged_message