Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tests for the apple_hash method

  • Loading branch information...
commit 85e318adb75f7b7a1c663eda9b1a480df0983b7d 1 parent 08f7398
@markbates markbates authored
View
48 README
@@ -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).
View
12 lib/apn_on_rails/app/models/apn/notification.rb
@@ -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
View
6 lib/apn_on_rails/app/models/apple_push_notification.rb
@@ -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
@@ -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
View
12 lib/apn_on_rails/db/migrate/20090723132059_create_apn_notifications.rb
@@ -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
@@ -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
View
17 spec/apn_on_rails/app/models/apn/notification_spec.rb
@@ -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
View
32 spec/factories/notification_factory.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.