From 634da688fd06f0faa94b1d32ae1937df8187a6ce Mon Sep 17 00:00:00 2001 From: Alexander Balsam Date: Fri, 30 Mar 2012 12:24:15 +0200 Subject: [PATCH 1/5] alert as hash --- .../app/models/apn/notification.rb | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb index 068f13a8..4b5f6b8c 100644 --- a/lib/apn_on_rails/app/models/apn/notification.rb +++ b/lib/apn_on_rails/app/models/apn/notification.rb @@ -18,6 +18,7 @@ class APN::Notification < APN::Base include ::ActionView::Helpers::TextHelper extend ::ActionView::Helpers::TextHelper serialize :custom_properties + serialize :alert belongs_to :device, :class_name => 'APN::Device' has_one :app, :class_name => 'APN::App', :through => :device @@ -48,10 +49,27 @@ def alert=(message) # apn.sound = true # apn.custom_properties = {"typ" => 1} # apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff"}, "typ" => "1"} + # + # Example 3: + # apn = APN::Notification.new + # apn.badge = 0 + # apn.sound = true + # apn.custom_properties = {"typ" => 1} + # apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff", "alert" => {"body" => "test", "action-loc-key" => "test-loc-key"}}, "typ" => "1"} + def apple_hash result = {} result['aps'] = {} - result['aps']['alert'] = self.alert if self.alert + if self.alert + if self.alert.kind_of?(Hash) + result['aps']['alert'] = {} + self.alert.each do |key,value| + result['aps']['alert']["#{key}"] = "#{value}" + end + else + result['aps']['alert'] = self.alert + end + end result['aps']['badge'] = self.badge.to_i if self.badge if self.sound result['aps']['sound'] = self.sound if self.sound.is_a? String From d34c6af126bb00d855d259290864da7c7bf88e50 Mon Sep 17 00:00:00 2001 From: Tim Buchwaldt Date: Thu, 24 May 2012 16:50:26 +0200 Subject: [PATCH 2/5] add mesage size to debug output --- lib/apn_on_rails/app/models/apn/notification.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb index 4b5f6b8c..5a428ec0 100644 --- a/lib/apn_on_rails/app/models/apn/notification.rb +++ b/lib/apn_on_rails/app/models/apn/notification.rb @@ -99,7 +99,7 @@ def to_apple_json def message_for_sending json = self.to_apple_json message = "\0\0 #{self.device.to_hexa}\0#{json.length.chr}#{json}" - raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256 + raise APN::Errors::ExceededMessageSizeError.new("#{message} #{message.size.to_i}") if message.size.to_i > 256 message end From 9fc04682dd58f2d6ded2c5944e9a3515f315e8ac Mon Sep 17 00:00:00 2001 From: Tim Buchwaldt Date: Thu, 24 May 2012 17:04:57 +0200 Subject: [PATCH 3/5] truncating message further, to solve errors occuring with too long messages --- lib/apn_on_rails/app/models/apn/notification.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb index 5a428ec0..44d26019 100644 --- a/lib/apn_on_rails/app/models/apn/notification.rb +++ b/lib/apn_on_rails/app/models/apn/notification.rb @@ -28,8 +28,8 @@ class APN::Notification < APN::Base # If the message is over 150 characters long it will get truncated # to 150 characters with a ... def alert=(message) - if !message.blank? && message.size > 150 - message = truncate(message, :length => 150) + if !message.blank? && message.size > 145 + message = truncate(message, :length => 145) end write_attribute('alert', message) end From 8c11d5b35d12cead14bcc877b198da1a47166f1d Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Mon, 3 Sep 2012 10:55:49 +0200 Subject: [PATCH 4/5] Changed bytesize detection --- .../app/models/apn/notification.rb | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb index 44d26019..bbce9b57 100644 --- a/lib/apn_on_rails/app/models/apn/notification.rb +++ b/lib/apn_on_rails/app/models/apn/notification.rb @@ -1,6 +1,8 @@ -# Represents the message you wish to send. +# encoding: utf-8 + +# Represents the message you wish to send. # An APN::Notification belongs to an APN::Device. -# +# # Example: # apn = APN::Notification.new # apn.badge = 5 @@ -8,10 +10,10 @@ # apn.alert = 'Hello!' # apn.device = APN::Device.find(1) # apn.save -# +# # To deliver call the following method: # APN::Notification.send_notifications -# +# # As each APN::Notification is sent the sent_at column will be timestamped, # so as to not be sent again. class APN::Notification < APN::Base @@ -19,23 +21,22 @@ class APN::Notification < APN::Base extend ::ActionView::Helpers::TextHelper serialize :custom_properties serialize :alert - + belongs_to :device, :class_name => 'APN::Device' has_one :app, :class_name => 'APN::App', :through => :device - + # Stores the text alert message you want to send to the device. - # + # # If the message is over 150 characters long it will get truncated # to 150 characters with a ... def alert=(message) - if !message.blank? && message.size > 145 - message = truncate(message, :length => 145) - end - write_attribute('alert', message) + msg = message.force_encoding("UTF-8") + msg = "#{msg.byteslice(0, 147)}..." if !msg.blank? && msg.bytesize > 150 + write_attribute('alert', msg) end - + # Creates a Hash that will be the payload of an APN. - # + # # Example: # apn = APN::Notification.new # apn.badge = 5 @@ -43,14 +44,14 @@ def alert=(message) # apn.alert = 'Hello!' # apn.apple_hash # => {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"}} # - # Example 2: + # Example 2: # apn = APN::Notification.new # apn.badge = 0 # apn.sound = true # apn.custom_properties = {"typ" => 1} # apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff"}, "typ" => "1"} - # - # Example 3: + # + # Example 3: # apn = APN::Notification.new # apn.badge = 0 # apn.sound = true @@ -65,9 +66,9 @@ def apple_hash result['aps']['alert'] = {} self.alert.each do |key,value| result['aps']['alert']["#{key}"] = "#{value}" - end + end else - result['aps']['alert'] = self.alert + result['aps']['alert'] = self.alert end end result['aps']['badge'] = self.badge.to_i if self.badge @@ -82,9 +83,9 @@ def apple_hash end result end - + # Creates the JSON string required for an APN message. - # + # # Example: # apn = APN::Notification.new # apn.badge = 5 @@ -94,7 +95,7 @@ def apple_hash def to_apple_json self.apple_hash.to_json end - + # Creates the binary message needed to send to Apple. def message_for_sending json = self.to_apple_json @@ -102,10 +103,10 @@ def message_for_sending raise APN::Errors::ExceededMessageSizeError.new("#{message} #{message.size.to_i}") if message.size.to_i > 256 message end - + def self.send_notifications ActiveSupport::Deprecation.warn("The method APN::Notification.send_notifications is deprecated. Use APN::App.send_notifications instead.") APN::App.send_notifications end - -end # APN::Notification \ No newline at end of file + +end # APN::Notification From 3c9b1affcc6285f8a0aee7bdf7baa2ddeaca4300 Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Mon, 3 Sep 2012 22:46:39 +0200 Subject: [PATCH 5/5] Fix nil error (not yet deployed) --- lib/apn_on_rails/app/models/apn/notification.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb index bbce9b57..6cb5f2b3 100644 --- a/lib/apn_on_rails/app/models/apn/notification.rb +++ b/lib/apn_on_rails/app/models/apn/notification.rb @@ -30,9 +30,11 @@ class APN::Notification < APN::Base # If the message is over 150 characters long it will get truncated # to 150 characters with a ... def alert=(message) - msg = message.force_encoding("UTF-8") - msg = "#{msg.byteslice(0, 147)}..." if !msg.blank? && msg.bytesize > 150 - write_attribute('alert', msg) + unless message.blank? + message = message.force_encoding("UTF-8") + message = "#{message.byteslice(0, 147)}..." if message.bytesize > 150 + end + write_attribute('alert', message) end # Creates a Hash that will be the payload of an APN.