Class: RubyPushNotifications::MPNS::MPNSNotification

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-push-notifications/mpns/mpns_notification.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_urls, data) ⇒ MPNSNotification

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



44
45
46
47
48
# File 'lib/ruby-push-notifications/mpns/mpns_notification.rb', line 44

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



25
26
27
# File 'lib/ruby-push-notifications/mpns/mpns_notification.rb', line 25

def data
  @data
end

#device_urlsArray (readonly)

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

Returns:

  • (Array)

    . Array with the receiver’s MPNS device URLs.



28
29
30
# File 'lib/ruby-push-notifications/mpns/mpns_notification.rb', line 28

def device_urls
  @device_urls
end

#resultsMPNSResponse

Returns . MPNSResponse with the results from sending this notification.

Returns:

  • (MPNSResponse)

    . MPNSResponse with the results from sending this notification.



12
13
14
# File 'lib/ruby-push-notifications/mpns/mpns_notification.rb', line 12

def results
  @results
end

Instance Method Details

#as_mpns_xmlString

Returns:

  • (String)

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



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/mpns/mpns_notification.rb', line 53

def as_mpns_xml
  xml = Builder::XmlMarkup.new
  xml.instruct!
  if data[:type] != :raw
    xml.tag!('wp:Notification', 'xmlns:wp' => 'WPNotification') do
      case data[:type]
      when :toast
        xml.tag!('wp:Toast') do
          xml.tag!('wp:Text1') { xml.text!(data[:title]) }
          xml.tag!('wp:Text2') { xml.text!(data[:message]) }
          xml.tag!('wp:Param') { xml.text!(data[:param]) } if data[:param]
        end
      when :tile
        xml.tag!('wp:Tile') do
          xml.tag!('wp:BackgroundImage') { xml.text!(data[:image]) } if data[:image]
          xml.tag!('wp:Count') { xml.text!(data[:count].to_s) } if data[:count]
          xml.tag!('wp:Title') { xml.text!(data[:title]) } if data[:title]
          xml.tag!('wp:BackBackgroundImage') { xml.text!(data[:back_image]) } if data[:back_image]
          xml.tag!('wp:BackTitle') { xml.text!(data[:back_title]) } if data[:back_title]
          xml.tag!('wp:BackContent') { xml.text!(data[:back_content]) } if data[:back_content]
        end
      end
    end
  else
    xml.root { build_hash(xml, data[:message]) }
  end
  xml.target!
end

#build_hash(xml, options) ⇒ Object



88
89
90
91
92
# File 'lib/ruby-push-notifications/mpns/mpns_notification.rb', line 88

def build_hash(xml, 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/mpns/mpns_notification.rb', line 82

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