Class: APN::Device

Inherits:
Base
  • Object
show all
Includes:
AASM
Defined in:
lib/urbanairship_on_rails/models/apn/device.rb

Overview

Represents an iPhone (or other APN enabled device). An APN::Device can have many APN::Notification.

In order for the APN::Feedback system to work properly you MUST touch the last_registered_at column everytime someone opens your application. If you do not, then it is possible, and probably likely, that their device will be removed and will no longer receive notifications.

Example:

Device.create(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz')

Constant Summary

Constants inherited from Base

Base::MAX_RETRIES, Base::OPEN_TIMEOUT, Base::READ_TIMEOUT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#http_delete, #http_get, #http_post, #http_put, table_name

Instance Attribute Details

#feedback_atObject

The feedback_at accessor is set when the device is marked as potentially disconnected from your application by Apple.



28
29
30
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 28

def feedback_at
  @feedback_at
end

Class Method Details

.find_by_ua_token(ua_token) ⇒ Object



84
85
86
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 84

def self.find_by_ua_token(ua_token)
  find_by_token(ua_token.downcase.scan(/.{8}/).join(" "))
end

Instance Method Details

#readObject

You can read a device token’s alias with an HTTP GET to /api/device_tokens/<device_token>, which returns application/json: “some device token”,“alias”: “your_user_id”



72
73
74
75
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 72

def read
  puts "APN::Device.read"
  http_get("/api/device_tokens/#{self.token_for_ua}")
end

#register(options = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 59

def register(options=nil)
  puts "APN::Device.register"
  # options = options.merge({:alias => self.user.id}) if self.user
  result = http_put("/api/device_tokens/#{self.token_for_ua}", options)
  self.response_code = result.code.to_s
  self.response_message = result.message.to_s
  self.response_body = result.body.to_s
  self.save
  self.activate!
end

#token=(token) ⇒ Object

Stores the token (Apple’s device ID) of the iPhone (device).

If the token comes in like this:

'<5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz>'

Then the ‘<’ and ‘>’ will be stripped off.



51
52
53
54
55
56
57
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 51

def token=(token)
  res = token.scan(/\<(.+)\>/).first
  unless res.nil? || res.empty?
    token = res.first
  end
  write_attribute('token', token)
end

#token_for_uaObject

The DELETE returns HTTP 204 No Content, and needs no payload. When a token is DELETEd in this manner, any alias or tags will be cleared.



80
81
82
# File 'lib/urbanairship_on_rails/models/apn/device.rb', line 80

def token_for_ua
  self.token.gsub(' ', '').upcase
end