From 069731e50135009f91f4005e50742d8dcb09d90f Mon Sep 17 00:00:00 2001 From: Sebastian Roth Date: Mon, 6 Feb 2012 15:07:13 +0800 Subject: [PATCH] Added custom hashes for the alert block which supports further customization --- README.textile | 10 ++++++++++ lib/apn_on_rails/app/models/apn/notification.rb | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 6a9cc7fd..f0e8384f 100644 --- a/README.textile +++ b/README.textile @@ -203,6 +203,16 @@ h2. Example (assuming you have created an app and stored your keys on it): >> notification.save +h3. Using custom hashes for the alert block + +In the above example, it's also possible to replace the static alert text with a hash as outlined in the Apple documentation: + +

+  >> notification.alert = { 'loc-key' => 'LocalizedString.Key' }.to_json
+
+ +h2. Delivery + You can use the following Rake task to deliver your individual notifications:

diff --git a/lib/apn_on_rails/app/models/apn/notification.rb b/lib/apn_on_rails/app/models/apn/notification.rb
index 068f13a8..acb423d4 100644
--- a/lib/apn_on_rails/app/models/apn/notification.rb
+++ b/lib/apn_on_rails/app/models/apn/notification.rb
@@ -51,7 +51,13 @@ def alert=(message)
   def apple_hash
     result = {}
     result['aps'] = {}
-    result['aps']['alert'] = self.alert if self.alert
+    if self.alert
+      begin
+        result['aps']['alert'] = JSON.parse(self.alert)
+      rescue JSON::ParserError => e
+        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