Class: RubyPushNotifications::WNS::WNSNotification

Inherits:
Object
  • Object
show all
Includes:
NotificationResultsManager
Defined in:
lib/ruby-push-notifications/wns/wns_notification.rb

Overview

Encapsulates a WNS Notification. Actually support for raw, toast, tiles notifications (msdn.microsoft.com/en-us/library/windowsphone/develop/hh202945)

Instance Attribute Summary collapse

Attributes included from NotificationResultsManager

#results

Instance Method Summary collapse

Constructor Details

#initialize(device_urls, data) ⇒ WNSNotification

Initializes the notification

Parameters:

  • . (Array)

    Array with the receiver’s device urls.

  • . (Hash)

    Payload to send. Toast :title => a bold message

    :message => the small message
    :param => a string parameter that is passed to the app
    

    Tile :image => a new image for the tile

    :count => a number to show on the tile
    :title => the new title of the tile
    :back_image => an image for the back of the tile
    :back_title => a title on the back of the tile
    :back_content => some content (text) for the back
    

    Raw :message => the full XML message body



42
43
44
45
46
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 42

def initialize(device_urls, data)
  @device_urls = device_urls
  @data = data
  @data[:type] ||= :raw
end

Instance Attribute Details

#dataHash (readonly)

Returns . Payload to send. Toast :title => a bold message

:message => the small message
:param => a string parameter that is passed to the app

Tile :image => a new image for the tile

:count => a number to show on the tile
:title => the new title of the tile
:back_image => an image for the back of the tile
:back_title => a title on the back of the tile
:back_content => some content (text) for the back

Raw :message => the full Hash message body.

Returns:

  • (Hash)

    . Payload to send. Toast :title => a bold message

    :message => the small message
    :param => a string parameter that is passed to the app
    

    Tile :image => a new image for the tile

    :count => a number to show on the tile
    :title => the new title of the tile
    :back_image => an image for the back of the tile
    :back_title => a title on the back of the tile
    :back_content => some content (text) for the back
    

    Raw :message => the full Hash message body



23
24
25
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 23

def data
  @data
end

#device_urlsArray (readonly)

Returns . Array with the receiver’s WNS device URLs.

Returns:

  • (Array)

    . Array with the receiver’s WNS device URLs.



26
27
28
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 26

def device_urls
  @device_urls
end

Instance Method Details

#as_wns_xmlString

Returns:

  • (String)

    . The WNS’s XML format for the payload to send.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 50

def as_wns_xml
  xml = Builder::XmlMarkup.new
  xml.instruct!
  if data[:type] != :raw
    case data[:type]
    when :toast
      xml.tag!('toast', **launch_params(data)) do
        xml.tag!('visual') do
          xml.tag!('binding', template: data[:template] || 'ToastText02') do
            xml.tag!('text', id: 1) { xml.text!(data[:title].to_s) }
            xml.tag!('text', id: 2) { xml.text!(data[:message].to_s) }
          end
        end
      end
    when :tile
      xml.tag!('tile') do
        xml.tag!('visual') do
          xml.tag!('binding', template: data[:template] || 'TileWideImageAndText01') do
            xml.tag!('image', src: data[:image].to_s)
            xml.tag!('text') { xml.text!(data[:message].to_s) }
          end
        end
      end
    when :badge
      xml.tag!('badge', value: data[:value])
    end
  else
    xml.root { build_hash(xml, data[:message]) }
  end
  xml.target!
end

#build_hash(xml, options) ⇒ Object



88
89
90
91
92
93
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 88

def build_hash(xml, options)
  return unless options
  options.each do |k, v|
    xml.tag!(k.to_s) { v.is_a?(Hash) ? build_hash(xml, v) : xml.text!(v.to_s) }
  end
end

#each_deviceObject



82
83
84
85
86
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 82

def each_device
  @device_urls.each do |url|
    yield(URI.parse url)
  end
end

#launch_params(data) ⇒ Object



95
96
97
98
# File 'lib/ruby-push-notifications/wns/wns_notification.rb', line 95

def launch_params(data)
  return {} unless data[:param]
  { launch: data[:param].to_json }
end