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
52
53
54
55
56
57
58
59
60 
     | 
    
      # File 'app/models/notification.rb', line 6
def self.create_and_send_notification(options={})
  source = options[:source]
  notifier = options[:notifier]
  managers = options[:managers]
  notification = source.notifications.create(
    :summary => options[:summary],
    :notification_type => options[:type],
    :notified_by => notifier
  )
  notification.add_notifiers(managers)
  load_pubnub
  managers.try(:each) do |manager|
    notifier.member.where(:organization_id => options[:organizationID]).each do |mem|
      @pubnub.publish(
        :channel => mem.organization.id.to_s + "_" + manager.id.to_s,
        :message => {
          :id => notification.id,
          :type => notification.notification_type,
          :created_at => notification.created_at,
          :updated_at => notification.updated_at,
          :notified_by => options[:notifier],
          :summary => notification.summary,
          :responded => notification.responded,
          :source => source
        }
      ) { |data| puts data.msg }
    end
  end
  push_tokens = []
  managers.each do |manager|
    manager.member.where(:organization_id => options[:organizationID]).each do |member|
      member.member_devices.each do |member_device|
        if member_device.device == 'iOS'
          push_tokens << member_device.push_token
        end
      end
    end
  end
  
  if push_tokens.size > 0
    payload = {
      :data => {
        :id => notification.id,
        :type => notification.notification_type,
        :created_at => notification.created_at,
        :updated_at => notification.updated_at,
        :notified_by => options[:notifier],
        :summary => notification.summary,
        :responded => notification.responded,
        :source => source
      }
    }
    s = Pushwoosh.notify_devices(notification.summary, push_tokens)
  end
end
     |