Class: StackMob::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/stackmob/push.rb

Constant Summary collapse

PUSH_SVC =
:push
DEVICE_TOKEN_PATH =
"/register_device_token_universal"
REMOVE_DEVICE_TOKEN =
"/remove_token_universal"
BROADCAST_PATH =
"/push_broadcast_universal"
PUSH_TOKENS_PATH =
"/push_tokens_universal"
PUSH_USERS_PATH =
"/push_users_universal"
GET_USERS_TOKEN_PATH =
"/get_tokens_for_users_universal"
DEFAULT_BADGE =
0
DEFAULT_SOUND =
""

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cl) ⇒ Push

Returns a new instance of Push.



31
32
33
# File 'lib/stackmob/push.rb', line 31

def initialize(cl)
  self.client = cl
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



18
19
20
# File 'lib/stackmob/push.rb', line 18

def client
  @client
end

Instance Method Details

#aps_data(opts) ⇒ Object



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

def aps_data(opts)
  alert = opts[:alert] || (raise ArgumentError.new("Push requires alert message"))
  badge = opts[:badge] || DEFAULT_BADGE
  sound = opts[:sound] || DEFAULT_SOUND
  
  {:badge => badge, :sound => sound, :alert => alert}
end

#broadcast(opts) ⇒ Object



43
44
45
46
# File 'lib/stackmob/push.rb', line 43

def broadcast(opts)
  payload = { :kvPairs => opts }
  self.client.request(:post, PUSH_SVC, BROADCAST_PATH, payload)
end

#get_tokens_for_users(to) ⇒ Object



76
77
78
79
# File 'lib/stackmob/push.rb', line 76

def get_tokens_for_users(to)
  payload = {:userIds => Array(to).join(",") }
  response = self.client.request(:get, PUSH_SVC, GET_USERS_TOKEN_PATH, payload)
end

#register(user_id, device_token) ⇒ Object



35
36
37
# File 'lib/stackmob/push.rb', line 35

def register(user_id, device_token)
  self.client.request(:post, PUSH_SVC, DEVICE_TOKEN_PATH, :userId => user_id, :token => device_token) # using non-convential symbols to conform to StackMob Push API
end

#remove(device_token) ⇒ Object



39
40
41
# File 'lib/stackmob/push.rb', line 39

def remove(device_token)
  self.client.request(:post, PUSH_SVC, REMOVE_DEVICE_TOKEN, device_token)
end

#send_message(to, opts) ⇒ Object

Deprecated



57
58
59
60
61
62
63
# File 'lib/stackmob/push.rb', line 57

def send_message(to, opts)
  warn "[DEPRECATION] `send_message` is deprecated and does not support android, please use `send_message_to_tokens` or `send_message_to_users`"
  recipients_are_device_tokens = (opts[:recipients_are_users]) ? false : true
  payload = {:recipients => Array(to), :aps => aps_data(opts), :areRecipientsDeviceTokens => recipients_are_device_tokens, :exclude_tokens => []}

  self.client.request(:post, PUSH_SVC, "/push", payload)
end

#send_message_to_tokens(to, opts) ⇒ Object



65
66
67
68
69
# File 'lib/stackmob/push.rb', line 65

def send_message_to_tokens(to, opts)
  to = [to] if to.is_a? Hash
  payload = {:tokens => Array(to), :payload => { :kvPairs => opts }}
  self.client.request(:post, PUSH_SVC, PUSH_TOKENS_PATH, payload)
end

#send_message_to_users(to, opts) ⇒ Object



71
72
73
74
# File 'lib/stackmob/push.rb', line 71

def send_message_to_users(to, opts)
  payload = {:userIds => Array(to), :kvPairs => opts}
  self.client.request(:post, PUSH_SVC, PUSH_USERS_PATH, payload)
end