Class: ActionNotifier::Base

Inherits:
AbstractController::Base
  • Object
show all
Defined in:
lib/action_notifier/action_notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/action_notifier/action_notifier.rb', line 7

def respond_to?(method, include_private = false)
  super || action_methods.include?(method.to_s)
end

Instance Method Details

#notify(app, device_token, message, custom = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_notifier/action_notifier.rb', line 21

def notify(app, device_token, message, custom = {})
  if app.is_a? Rpush::Apns::App
    _notify = :notify_apns
  elsif app.is_a? Rpush::Gcm::App
    _notify = :notify_gcm
  else
    raise ArgumentError, "Unsupported app"
  end

  return send _notify, app, device_token, message, custom
end

#notify_apns(app, device_token, message, custom = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/action_notifier/action_notifier.rb', line 33

def notify_apns(app, device_token, message, custom = {})
  n = Rpush::Apns::Notification.new
  n.app = app
  n.device_token = device_token
  n.alert = message.truncate(110)  # TODO: They can be longer now, right?
  if custom[:badge]
    n.badge = custom.delete(:badge)
  end
  n.sound = custom.delete(:sound) || 'default'
  n.data = custom
  n.save!
  return n
end

#notify_gcm(device_token, message, custom = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/action_notifier/action_notifier.rb', line 47

def notify_gcm(device_token, message, custom = {})
  n = Rpush::Gcm::Notification.new
  n.app = Rpush::Gcm::App.find_by_name("android")
  n.registration_ids = [device_token]
  n.data = { message: message }.merge(custom)
  n.save!
  return n
end