Class: JPush::PushPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/jpush/model/push_payload.rb

Overview

The object you should build for sending a push.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PushPayload

Returns a new instance of PushPayload.


8
9
10
11
12
13
14
# File 'lib/jpush/model/push_payload.rb', line 8

def initialize(opts = {})
  @platform = opts[:platform]
  @audience = opts[:audience]
  @message = opts[:message]
  @options = opts[:options]
  @notification = opts[:notification]
end

Instance Attribute Details

#audienceObject

Returns the value of attribute audience


7
8
9
# File 'lib/jpush/model/push_payload.rb', line 7

def audience
  @audience
end

#messageObject

Returns the value of attribute message


7
8
9
# File 'lib/jpush/model/push_payload.rb', line 7

def message
  @message
end

#notificationObject

Returns the value of attribute notification


7
8
9
# File 'lib/jpush/model/push_payload.rb', line 7

def notification
  @notification
end

#optionsObject

Returns the value of attribute options


7
8
9
# File 'lib/jpush/model/push_payload.rb', line 7

def options
  @options
end

#platformObject

Returns the value of attribute platform


7
8
9
# File 'lib/jpush/model/push_payload.rb', line 7

def platform
  @platform
end

Class Method Details

.build(opts = {}) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jpush/model/push_payload.rb', line 36

def self.build(opts = {})
  payload = JPush::PushPayload.new(opts)
  if payload.audience == nil || payload.platform == nil
    raise ArgumentError.new(' audience and platform both should be set.')
  end
  if payload.notification == nil && payload.message == nil
    raise ArgumentError.new('notification or message should be set at least one')
  end

  if payload.notification.to_s.bytesize + payload.message.to_s.bytesize > 1200
    raise ArgumentError.new('notfication and message size is longer than 1200 ')
  end
  if payload.options == nil
    options = JPush::Options.build(
    :time_to_live=> 86400,
    :apns_production=> false)
    payload.options = options
  end
  return payload
end

Instance Method Details

#isGlobalExceedObject

Check if the length is too long


67
68
69
70
71
72
73
# File 'lib/jpush/model/push_payload.rb', line 67

def isGlobalExceed
  if @notification.to_s.bytesize + @message.to_s.bytesize > 1200
  return false
  else
  return true
  end
end

#isIOSExceedLengthObject

used only the ios nitification is not nil


58
59
60
61
62
63
64
# File 'lib/jpush/model/push_payload.rb', line 58

def isIOSExceedLength
  if @notification.ios.to_s.bytesize > 220
  return false
  else
  return true
  end
end

#toJSONObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jpush/model/push_payload.rb', line 16

def toJSON
  array = {}
  if @platform != nil then
    array['platform'] = @platform.toJSON
  end
  if @audience != nil then
    array['audience'] = @audience.toJSON
  end
  if @message != nil then
    array['message'] = @message.toJSON
  end
  if @options != nil then
    array['options'] = @options.toJSON
  end
  if @notification != nil then
    array['notification'] = @notification.toJSON
  end
  return array
end