Skip to content

Commit

Permalink
Added tests for the apple_hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jul 23, 2009
1 parent 08f7398 commit 85e318a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 17 deletions.
48 changes: 45 additions & 3 deletions README
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
README
========================================================================
apn_on_rails was developed by: markbates
Apple Push Notification
=====================

This plugin helps you use the Apple Push Notification system.

Converting Your Certificate
---------------------------

Once you have the certificate from Apple for your application, export your key
and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:

1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
2. Right click and choose `Export 2 items...`.
3. Choose the p12 format from the drop down and name it `cert.p12`.

Now covert the p12 file to a pem file:

$ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts

Put `apple_push_notification.pem` in config/

Installing
----------

Simply run the following commands to add apple-push-notification as a submodule to your repo, a plugin to your rails app, and install it.

$ cd ~/my_rails_app
$ script/plugin install git://github.com/samsoffes/apple_push_notification.git
$ rake apn:migrate

Example
-------

*Note: the spaces in `device_token` are optional.*

$ ./script/console
>> a = ApplePushNotification.new
>> a.device_token = "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX"
>> a.badge = 5
>> a.sound = true
>> a.alert = "foobar"
>> a.send_notification
=> nil

Copyright (c) 2009 Fabien Penso. Released under the MIT license. Modified by [Sam Soffes](http://samsoff.es).
12 changes: 12 additions & 0 deletions lib/apn_on_rails/app/models/apn/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@ class APN::Notification < ActiveRecord::Base

belongs_to :device, :class_name => 'APN::Device'

def apple_hash
result = {}
result['aps'] = {}
result['aps']['alert'] = self.alert if self.alert
result['aps']['badge'] = self.badge.to_i if self.badge
if self.sound
result['aps']['sound'] = self.sound if self.sound.is_a? String
result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass)
end
result
end

end
6 changes: 3 additions & 3 deletions lib/apn_on_rails/app/models/apple_push_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
# end
#
# def to_apple_json
# logger.debug "Sending #{self.apple_array.to_json}"
# self.apple_array.to_json
# logger.debug "Sending #{self.apple_hash.to_json}"
# self.apple_hash.to_json
# end
#
# def apn_message_for_sending
Expand All @@ -80,7 +80,7 @@
# [self.device_token.delete(' ')].pack('H*')
# end
#
# def apple_array
# def apple_hash
# result = {}
# result['aps'] = {}
# result['aps']['alert'] = alert if alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def self.up
t.string :device_language, :size => 5 # if you don't want to send localized strings
t.text :payload
t.string :sound
t.string :alert
t.integer :badge
t.text :app_data
t.datetime :sent_at
t.timestamps
end
Expand All @@ -21,12 +21,4 @@ def self.down
drop_table :apn_notifications
end

end

# add_column :apple_push_notifications, :payload, :text
# add_column :apple_push_notifications, :sound, :string
# add_column :apple_push_notifications, :badge, :integer
# add_column :apple_push_notifications, :alert, :string
# add_column :apple_push_notifications, :appdata, :text
# add_column :apple_push_notifications, :sent_at, :datetime
# add_column :apple_push_notifications, :device_id, :integer
end
17 changes: 16 additions & 1 deletion spec/apn_on_rails/app/models/apn/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

describe APN::Notification do

it 'should do something'
describe 'apple_hash' do

it 'should return a hash of the appropriate params for Apple' do
noty = APN::Notification.first
noty.apple_hash.should == {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"}}
noty.badge = nil
noty.apple_hash.should == {"aps" => {"sound" => "my_sound.aiff", "alert" => "Hello!"}}
noty.alert = nil
noty.apple_hash.should == {"aps" => {"sound" => "my_sound.aiff"}}
noty.sound = nil
noty.apple_hash.should == {"aps" => {}}
noty.sound = true
noty.apple_hash.should == {"aps" => {"sound" => "1.aiff"}}
end

end

end
32 changes: 32 additions & 0 deletions spec/factories/notification_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module NotificationFactory

class << self

def new(options = {})
device = APN::Device.first
options = {:device_id => device.id, :sound => 'my_sound.aiff',
:badge => 5, :alert => 'Hello!'}.merge(options)
return APN::Notification.new(options)
end

def create(options = {})
notification = NotificationFactory.new(options)
notification.save
return notification
end

end

end

NotificationFactory.create

# t.integer :device_id, :null => false
# t.integer :errors_nb, :default => 0 # used for storing errors from apple feedbacks
# t.string :device_language, :size => 5 # if you don't want to send localized strings
# t.text :payload
# t.string :sound
# t.integer :badge
# t.text :app_data
# t.datetime :sent_at
# t.timestamps

0 comments on commit 85e318a

Please sign in to comment.