Class: Hsquare::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/hsquare/notification.rb

Overview

Public: Notification entries sent to Kakao push.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Notification

Public: Initializes new notification object.

attributes - Attributes of the notification.

:recipient_id  - ID of recipient (optinoal).
:recipient_ids - List of IDs of recipients (optinoal).
:message       - Message displayed to recipients (optional).
:badge         - Badge count (optinoal).
:sound         - Sound to play (optinoal).
:push_alert    - Whether notification is delivered through
                 notification center (default: true).
:collapse      - Collapse key for GCM (optional).
:idle_delay    - Whether GCM message delivery is delayed
                 while device is idle (default: false).
:extra         - Extra payload (optinoal).
:app_ids       - IDs of Hsquare applications to deliver.

Returns newly initialized notification object.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hsquare/notification.rb', line 48

def initialize(attributes = {})
  @recipient_ids = (attributes[:recipient_id] && [attributes[:recipient_id]]) || attributes[:recipient_ids]
  @message = attributes[:message]
  @badge = attributes[:badge]
  @sound = attributes[:sound]
  @push_alert = attributes.has_key?(:push_alert) ? attributes[:push_alert] : true
  @collapse = attributes[:collapse]
  @idle_delay = attributes.has_key?(:idle_delay) ? attributes[:idle_delay] : false
  @extra = attributes[:extra]
  @app_ids = attributes[:app_ids]
end

Instance Attribute Details

#app_idsObject

Public: IDs of Hsquare applications to deliver.



29
30
31
# File 'lib/hsquare/notification.rb', line 29

def app_ids
  @app_ids
end

#badgeObject

Public: Badge count to set.



11
12
13
# File 'lib/hsquare/notification.rb', line 11

def badge
  @badge
end

#collapseObject

Public: Collapse key for GCM.



20
21
22
# File 'lib/hsquare/notification.rb', line 20

def collapse
  @collapse
end

#extraObject

Public: Extra payload.



26
27
28
# File 'lib/hsquare/notification.rb', line 26

def extra
  @extra
end

#idle_delayObject

Public: Whether GCM message delivery is delayed while device is idle.



23
24
25
# File 'lib/hsquare/notification.rb', line 23

def idle_delay
  @idle_delay
end

#messageObject

Public: Message (alert in APNS) displayed to recipients.



8
9
10
# File 'lib/hsquare/notification.rb', line 8

def message
  @message
end

#push_alertObject

Public: Whether notification is delivered through notification center.



17
18
19
# File 'lib/hsquare/notification.rb', line 17

def push_alert
  @push_alert
end

#recipient_idsObject

Public: Array of recipient IDs.



5
6
7
# File 'lib/hsquare/notification.rb', line 5

def recipient_ids
  @recipient_ids
end

#soundObject

Public: Sound to play.



14
15
16
# File 'lib/hsquare/notification.rb', line 14

def sound
  @sound
end

Instance Method Details

#deliverObject

Public: Delivers notification to recipients’ devices.

Returns nothing.



72
73
74
75
76
77
78
# File 'lib/hsquare/notification.rb', line 72

def deliver
  Hsquare.config.applications.each do |application|
    if !app_ids || app_ids.include?(application.label)
      application.admin_client.post('/v1/push/send', body: payload)
    end
  end
end

#recipient_id=(recipient_id) ⇒ Object

Public: Helper method to set recipient_ids with single ID.

recipient_id - ID of single recipient.

Returns 1-length array of recipient_id.



65
66
67
# File 'lib/hsquare/notification.rb', line 65

def recipient_id=(recipient_id)
  @recipient_ids = [recipient_id]
end