ProMotion-push 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd3c0fd84cbfe3e827eb20dc749f568c0ef7c6e5
4
+ data.tar.gz: 8cf014919a0aecc37e36d70014c5d6f06a09046c
5
+ SHA512:
6
+ metadata.gz: d4250274648da983f843f6e84293aaa080b700db71ba8eb59705326d9d6aef1f879498ab0eab0b5d45a3655b8126f03ca65a6d6d2dad0a3ea05996ab9beea0bf
7
+ data.tar.gz: 761a0da895bdecd0489e7b957db4a0e39a1be4f359f1120f8063463431633cb5de064a737c94ccb66333acf1a265cacd67c51131d2e4145e63f4642851155bc8
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # ProMotion-push
2
+
3
+ ProMotion-push is push notification support, extracted from the
4
+ popular RubyMotion gem [ProMotion](https://github.com/clearsightstudio/ProMotion).
5
+
6
+ ## Installation
7
+
8
+ ```ruby
9
+ gem 'ProMotion-push'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ### AppDelegate
15
+
16
+ ProMotion-push adds a few methods to PM::Delegate.
17
+
18
+ ```ruby
19
+ # app/app_delegate.rb
20
+ class AppDelegate < PM::Delegate
21
+
22
+ def on_load(app, options)
23
+ register_for_push_notifications :badge, :sound, :alert, :newsstand
24
+ PM.logger.info registered_push_notifications
25
+ # ...
26
+ end
27
+
28
+ def on_unload
29
+ unregister_for_push_notifications
30
+ end
31
+
32
+ def on_push_registration(token, error)
33
+ PM.logger.info token.description
34
+ end
35
+
36
+ def on_push_notification(notification, launched)
37
+ PM.logger.info notification.to_json
38
+ end
39
+ end
40
+ ```
41
+
42
+ #### register_for_push_notifications(*types)
43
+
44
+ Method you can call to register your app for push notifications. You'll also want to implement
45
+ `on_push_notification` and `on_push_registration`.
46
+
47
+ ```ruby
48
+ def on_load(app, options)
49
+ register_for_push_notifications :badge, :sound, :alert, :newsstand # or :all
50
+ # ...
51
+ end
52
+ ```
53
+
54
+ #### unregister_for_push_notifications
55
+
56
+ Unregisters from all push notifications.
57
+
58
+ ```ruby
59
+ def logging_out
60
+ unregister_for_push_notifications
61
+ end
62
+ ```
63
+
64
+ **NOTE:** From a screen you'll have to reference the app_delegate:
65
+
66
+ ```ruby
67
+ def log_out
68
+ app_delegate.unregister_for_push_notifications
69
+ end
70
+ ```
71
+
72
+ #### on_push_registration(token, error)
73
+
74
+ Method that is called after you attempt to register for notifications. Either `token` or `error`
75
+ will be provided.
76
+
77
+ ```ruby
78
+ def on_push_registration(token, error)
79
+ if token
80
+ # Push token to your server
81
+ else
82
+ # Display the error
83
+ end
84
+ end
85
+ ```
86
+
87
+ #### on_push_notification(notification, launched)
88
+
89
+ Method called when the app is launched via a notification or a notification is received
90
+ in-app. `notification` is a
91
+ [PM::PushNotification](https://github.com/clearsightstudio/ProMotion/wiki/API-Reference:-ProMotion::PushNotification)
92
+ object which is a thin wrapper around the notification hash provided by iOS. `launched`
93
+ is a boolean letting you know whether the notification initiated your app's launch (true) or
94
+ if your app was already running (false).
95
+
96
+ ```ruby
97
+ def on_push_notification(notification, launched)
98
+ notification.to_json # => '{"aps":{"alert":"My test notification","badge":3,"sound":"default"}, "custom": "Jamon Holmgren"}'
99
+ notification.alert # => "My test notification"
100
+ notification.badge # => 3
101
+ notification.sound # => "default"
102
+ notification.custom # => "Jamon Holmgren"
103
+ end
104
+ ```
105
+
106
+ #### registered_push_notifications
107
+
108
+ Returns the currently registered notifications as an array of symbols.
109
+
110
+ ```ruby
111
+ def some_method
112
+ registered_push_notifications # => [ :badge, :sound, :alert, :newsstand ]
113
+ end
114
+ ```
115
+
116
+
117
+ ### ProMotion::PushNotification
118
+
119
+ You receive this object in your AppDelegate's `on_push_notification` method.
120
+
121
+ ```ruby
122
+ def on_push_notification(notification, launched)
123
+ notification.to_json # => '{"aps":{"alert":"My test notification","badge":3,"sound":"default"}, "custom": "Jamon Holmgren"}'
124
+ notification.alert # => "My test notification"
125
+ notification.badge # => 3
126
+ notification.sound # => "default"
127
+ notification.custom # => "Jamon Holmgren"
128
+ end
129
+ ```
130
+
131
+ The best way to test push notifications is on a device, but it's often useful to test
132
+ them in the simulator. We provide a way to do that from the REPL or in code.
133
+
134
+ ```ruby
135
+ # In REPL
136
+ PM::PushNotification.simulate(alert: "My test", badge: 4, sound: "default", custom: "custom", launched: true)
137
+ ```
138
+ ```ruby
139
+ def on_push_notification(notification, launched)
140
+ notification.aps # => { alert: "My test", badge: sound: "default"}
141
+ notification.alert # => "My test"
142
+ notification.custom # => 'custom'
143
+ end
144
+ ```
145
+
146
+ #### alert
147
+
148
+ Returns the alert string for the push notification object.
149
+
150
+ ```ruby
151
+ notification.alert # => "My test notification"
152
+ ```
153
+
154
+ #### badge
155
+
156
+ Returns the badge number for the push notification object, if it exists.
157
+
158
+ ```ruby
159
+ notification.badge # => 3
160
+ ```
161
+
162
+ #### sound
163
+
164
+ Returns a string representing the sound for the push notification object, if it exists.
165
+
166
+ ```ruby
167
+ notification.sound # => "sound"
168
+ ```
169
+
170
+ #### to_json
171
+
172
+ Returns a json string representation of the push notification object.
173
+
174
+ ```ruby
175
+ notification.to_json # => '{"aps":{"alert":"My test notification","sound":"default"},"custom":"something custom"}'
176
+ ```
177
+
178
+ #### (custom methods)
179
+
180
+ A `method_missing` implementation will respond to all methods that are keys in the notification hash. It
181
+ will raise a NoMethodError if there isn't a corresponding key.
182
+
183
+ ```ruby
184
+ # Given: '{"aps":{"alert":"My test notification","sound":"default"}, "my_custom_key": "My custom data"}'
185
+ notification.my_custom_key # => "My custom data"
186
+ notification.no_key_here # => NoMethodError
187
+ ```
188
+
189
+ #### notification
190
+
191
+ Returns the raw notification object provided by iOS.
192
+
193
+ ```ruby
194
+ notification.notification # => Hash
195
+ ```
196
+
197
+ ## Contributing
198
+
199
+ 1. Fork it
200
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
201
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
202
+ 4. Make some specs pass
203
+ 5. Push to the branch (`git push origin my-new-feature`)
204
+ 6. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "ProMotion-push must be required within a RubyMotion project."
5
+ end
6
+
7
+ Motion::Project::App.setup do |app|
8
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
9
+ app.files << File.join(lib_dir_path, "ProMotion/push_notification.rb")
10
+ app.files << File.join(lib_dir_path, "ProMotion/delegate_notifications.rb")
11
+ app.files << File.join(lib_dir_path, "ProMotion/delegate_module.rb")
12
+ end
@@ -0,0 +1,10 @@
1
+ # Re-open ProMotion's DelegateModule and Delegate to include our module
2
+ module ProMotion
3
+ module DelegateModule
4
+ include ProMotion::DelegateNotifications
5
+ end
6
+
7
+ class Delegate < ProMotion::DelegateParent
8
+ include ProMotion::DelegateNotifications
9
+ end
10
+ end
@@ -0,0 +1,71 @@
1
+ module ProMotion
2
+ # @requires class:PushNotification
3
+ module DelegateNotifications
4
+
5
+ attr_accessor :aps_notification
6
+
7
+ def check_for_push_notification(options)
8
+ if options && options[UIApplicationLaunchOptionsRemoteNotificationKey]
9
+ received_push_notification options[UIApplicationLaunchOptionsRemoteNotificationKey], true
10
+ end
11
+ end
12
+
13
+ def register_for_push_notifications(*notification_types)
14
+ notification_types = Array.new(notification_types)
15
+ notification_types = [ :badge, :sound, :alert, :newsstand ] if notification_types.include?(:all)
16
+
17
+ types = UIRemoteNotificationTypeNone
18
+ notification_types.each { |t| types = types | map_notification_symbol(t) }
19
+
20
+ UIApplication.sharedApplication.registerForRemoteNotificationTypes types
21
+ end
22
+
23
+ def unregister_for_push_notifications
24
+ UIApplication.sharedApplication.unregisterForRemoteNotifications
25
+ end
26
+
27
+ def registered_push_notifications
28
+ mask = UIApplication.sharedApplication.enabledRemoteNotificationTypes
29
+ types = []
30
+
31
+ types << :badge if mask & UIRemoteNotificationTypeBadge > 0
32
+ types << :sound if mask & UIRemoteNotificationTypeSound > 0
33
+ types << :alert if mask & UIRemoteNotificationTypeAlert > 0
34
+ types << :newsstand if mask & UIRemoteNotificationTypeNewsstandContentAvailability > 0
35
+
36
+ types
37
+ end
38
+
39
+ def received_push_notification(notification, was_launched)
40
+ @aps_notification = ProMotion::PushNotification.new(notification)
41
+ on_push_notification(@aps_notification, was_launched) if respond_to?(:on_push_notification)
42
+ end
43
+
44
+ # CocoaTouch
45
+
46
+ def application(application, didRegisterForRemoteNotificationsWithDeviceToken:device_token)
47
+ on_push_registration(device_token, nil) if respond_to?(:on_push_registration)
48
+ end
49
+
50
+ def application(application, didFailToRegisterForRemoteNotificationsWithError:error)
51
+ on_push_registration(nil, error) if respond_to?(:on_push_registration)
52
+ end
53
+
54
+ def application(application, didReceiveRemoteNotification:notification)
55
+ received_push_notification(notification, application.applicationState != UIApplicationStateActive)
56
+ end
57
+
58
+ protected
59
+
60
+ def map_notification_symbol(symbol)
61
+ {
62
+ none: UIRemoteNotificationTypeNone,
63
+ badge: UIRemoteNotificationTypeBadge,
64
+ sound: UIRemoteNotificationTypeSound,
65
+ alert: UIRemoteNotificationTypeAlert,
66
+ newsstand: UIRemoteNotificationTypeNewsstandContentAvailability
67
+ }[symbol] || UIRemoteNotificationTypeNone
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,58 @@
1
+ module ProMotion
2
+ class PushNotification
3
+
4
+ attr_accessor :notification
5
+
6
+ def initialize(n)
7
+ self.notification = n
8
+ end
9
+
10
+ def to_s
11
+ self.notification.inspect
12
+ end
13
+
14
+ def to_json
15
+ ProMotion.logger.warn "ProMotion::PushNotification.to_json not implemented yet."
16
+ end
17
+
18
+ def aps
19
+ self.notification["aps"]
20
+ end
21
+
22
+ def alert
23
+ aps["alert"] if aps
24
+ end
25
+
26
+ def badge
27
+ aps["badge"] if aps
28
+ end
29
+
30
+ def sound
31
+ aps["sound"] if aps
32
+ end
33
+
34
+ def method_missing(meth, *args, &block)
35
+ aps[meth.to_s] || aps[meth.to_sym] || self.notification[meth.to_s] || self.notification[meth.to_sym] || super
36
+ end
37
+
38
+ # For testing from the REPL
39
+ # > ProMotion::PushNotification.simulate alert: "My test message", badge: 4
40
+ def self.simulate(args = {})
41
+ UIApplication.sharedApplication.delegate.on_push_notification self.fake_notification(args), args[:launched]
42
+ end
43
+
44
+ def self.fake_notification(args = {})
45
+ self.new({
46
+ "aps" => {
47
+ "alert" => args.delete(:alert) || "Test Push Notification",
48
+ "badge" => args.delete(:badge) || 2,
49
+ "sound" => args.delete(:sound) || "default"
50
+ },
51
+ "channels" => args.delete(:channels) || [
52
+ "channel_name"
53
+ ]
54
+ }.merge(args))
55
+ end
56
+
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ProMotion-push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jamon Holmgren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ProMotion
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: motion-stump
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: motion-redgreen
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Adds push notification support to ProMotion.
70
+ email:
71
+ - jamon@clearsightstudio.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/ProMotion-push.rb
78
+ - lib/ProMotion/delegate_module.rb
79
+ - lib/ProMotion/delegate_notifications.rb
80
+ - lib/ProMotion/push_notification.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Adds push notification support to ProMotion. Extracted from ProMotion.
105
+ test_files: []