Sha256: f604d5c20290dcb127e92dcebab492a51e6e9c606ae19000870da17b8953265f

Contents?: true

Size: 911 Bytes

Versions: 6

Compression:

Stored size: 911 Bytes

Contents

module FwtPushNotificationServer

	module Notifier

		class GCM < Notifier::Base

			def notify_once(message, device_tokens)

				@device_tokens = device_tokens.is_a?(Array) ? device_tokens : [device_tokens]
				
				@gcm = ::GCM.new(FwtPushNotificationServer.gcm_api_key)

				@registration_ids = []
				@payload = { :data => { :message => message } }
				@device_tokens.each do |device|
					@registration_ids << device.token
					if @registration_ids.count == 1000
						send_batch
					end
				end
				send_batch
			end

			private
			def send_batch
				response = @gcm.send_notification(@registration_ids, @payload)
				body = JSON.parse(response.fetch(:body, "{}"))
				results = body.fetch("results", [])
				results.each_with_index do |result, idx|
					if result["error"]
						@device_tokens[idx].update_attribute('is_valid', false)
					end
				end
				@registration_ids = []
			end

		end

	end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fwt_push_notification_server-0.1.0.b4 lib/notifier/gcm.rb
fwt_push_notification_server-0.1.0.b3 lib/notifier/gcm.rb
fwt_push_notification_server-0.1.0.b2 lib/notifier/gcm.rb
fwt_push_notification_server-0.1.0.b1 lib/notifier/gcm.rb
fwt_push_notification_server-0.0.7 lib/notifier/gcm.rb
fwt_push_notification_server-0.0.7.b4 lib/notifier/gcm.rb