Class: NotifyUser::PubNub

Inherits:
Object
  • Object
show all
Defined in:
app/models/notify_user/pub_nub.rb

Class Method Summary collapse

Class Method Details

.push_notification(notification) ⇒ Object

sends push notification



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/notify_user/pub_nub.rb', line 5

def self.push_notification(notification)
  pubnub = Pubnub.new(
    :origin => ENV['PN_ORIGIN'],
    :publish_key   => ENV['PN_PUBLISH_KEY'],
    :subscribe_key => ENV['PN_SUBSCRIBE_KEY'],
    :secret_key => ENV['PN_SECRET_KEY'],
    :logger => Logger.new(STDOUT)
  )

  pubnub.grant( auth_key: ENV['PN_SECRET_KEY'],
                :read => true,
                :write => true,
                :ttl => 525600,
                :http_sync => true
              )

  pn_apns = {
    aps: {
      alert: notification.mobile_message,
      badge: notification.count_for_target,
      type: notification.type
    }
  }

  pn_apns[:aps][:action_id] = notification.params[:action_id] if notification.params[:action_id]
  pn_apns[:aps]['content-available'] = notification.params['content-available'] if notification.params['content-available']

  pn_gcm = {
    data: {
      notification_id: notification.id,
      message: notification.mobile_message,
      type: notification.type,
      unread_count: notification.count_for_target
    }
  }

  pn_gcm[:data][:action_id] = notification.params[:action_id] if notification.params[:action_id]

  pubnub.publish(
    channel: notification.target.uuid,
    http_sync: true,
    message: {
      pn_apns: pn_apns,
      pn_gcm: pn_gcm
    }
  )
end