# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'push/PushNotificationResposneBuilder.rb' require 'push/PushNotification.rb' require 'push/DeviceType.rb' module App42 module Push # # # The service is for pushing the notifications to any device using GCM(Google Cloud Messaging). # You have to upload your apikey that you received while registering for GCM and you have to store your # device token with particular username. This service allows you the feature of sending message to # particular channel, particular user or to all your users.For sending message to any channel, you have to # create the channel and send the message to channel. The users which have subscribed to that channel will receive # all the notification for that channel. For sending message to particular user, you have to pass username and # message. Notification will sent to the device of registered user. The most important feature you can send your message # to all your device whether it is iphone, android or blackberry. # # class PushNotificationService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "PushNotificationService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "push" @version = "1.0" end # Stores your device token on server with particular username # # @param Username # - username with which you want your device to be registered # @param deviceToken # - device id for android phones # @param type # - # # @return PushNotification Object # # @raise App42Exception # def store_device_token(userName, deviceToken, type) puts "storeDeviceToken Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "Username"); util.throwExceptionIfNullOrBlank(deviceToken, "deviceToken"); util.throwExceptionIfNullOrBlank(type, "Device Type"); begin if (DeviceType.new.isAvailable(type) == nil) raise App42NotFoundException.new("Device Type #{type} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => { "userName" => userName, "deviceToken" => deviceToken, "type" => type }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storeDeviceToken/#{userName}" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # Create Channel for app on which user can subscribe and get the notification for that # channel # # @param channel # - channel name which you want to create # @param description # - description for that channel # # @return PushNotification Object # # @raise App42Exception # def create_channel_for_app(channel, description) puts "createChannelForApp Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(channel, "Channel Name"); util.throwExceptionIfNullOrBlank(description, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => {"channel" => { "name" => channel, "description" => description }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/createAppChannel" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # # Subscribe to the channel # # @param channel # - the channel name which you want to subscribe # @param userName # - username which want to subscribe # # @return PushNotification Object # # @raise App42Exception # def subscribe_to_channel(channel, userName) puts "subscribeToChannel Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(channel, "Channel Name"); util.throwExceptionIfNullOrBlank(userName, "userName"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'push' => {"channel" => { "name" => channel, "userName" => userName }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/subscribeToChannel/#{userName}" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # Unsubscribe from particular channel # # @param channel # - channel name which you want to unsubscribe # @param userName # - username which want to unsubscribe # # @return PushNotification Object # # @raise App42Exception # def unsubscribe_from_channel(channel, userName) puts "unsubscribeFromChannel Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(channel, "Channel Name"); util.throwExceptionIfNullOrBlank(userName, "userName"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'push' => {"channel" => { "userName" => userName, "name" => channel }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/unsubscribeToChannel/#{userName}" response = connection.put(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # # send push message to channel containing string # # @param channel # - channel name which you want to send the message # @param message # - push message in string format # # @return PushNotification Object # # @raise App42Exception # def send_push_message_to_channel(channel, message) puts "sendPushMessageToChannel Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(channel, "channel"); util.throwExceptionIfNullOrBlank(message, "message"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => {"message" => { "channel" => channel, "expiry" => util.get_timestamp_utc, "payload" => message }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/sendPushMessageToChannel/#{channel}" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # # Send push message to all your users # # @param message # - push message # # @return PushNotification Object # # @raise App42Exception # def send_push_message_to_all(message) puts "sendPushMessageToAll Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(message, "message"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => {"message" => { "expiry" => util.get_timestamp_utc, "payload" => message }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/sendPushMessageToAll" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # # Send Push Message To paticular user in string format # # @param username # - username which you want to send the message # @param message # - push message # # @return PushNotification Object # # @raise App42Exception # def send_push_message_to_user(userName, message) puts "sendPushMessageToUser Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName"); util.throwExceptionIfNullOrBlank(message, "message"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => {"message" => { "userName" => userName, "expiry" => util.get_timestamp_utc, "payload" => message }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/sendMessage/#{userName}" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end # # Send push message to all by type # # @param message # - push message # # @return PushNotification Object # # @raise App42Exception # def send_push_message_to_all_by_type(message, type) puts "sendPushMessageToAll Called " puts "Base url #{@base_url}" response = nil; pushObj = nil; pushObj = PushNotification.new util = Util.new util.throwExceptionIfNullOrBlank(message, "message"); util.throwExceptionIfNullOrBlank(type, "type"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"push" => {"message" => { "payload" => message, "expiry" => util.get_timestamp_utc, "type" => type }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/sendMessageToAllByType" response = connection.post(signature, resource_url, query_params, body) push = PushNotificationResponseBuilder.new() pushObj = push.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return pushObj end end end end