Class: App42::Push::PushNotificationService

Inherits:
Object
  • Object
show all
Defined in:
lib/push/PushNotificationService.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ PushNotificationService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL

36
37
38
39
40
41
42
43
# File 'lib/push/PushNotificationService.rb', line 36

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

Instance Method Details

#create_channel_for_app(channel, description) ⇒ Object

Create Channel for app on which user can subscribe and get the notification for that channel

Parameters:

  • channel
    • channel name which you want to create

  • description
    • description for that channel

Returns:

  • PushNotification Object

Raises:

  • App42Exception


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/push/PushNotificationService.rb', line 115

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

#send_push_message_to_all(message) ⇒ Object

Send push message to all your users

Parameters:

  • message
    • push message

Returns:

  • PushNotification Object

Raises:

  • App42Exception


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/push/PushNotificationService.rb', line 317

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_all_by_type(message, type) ⇒ Object

Send push message to all by type

Parameters:

  • message
    • push message

Returns:

  • PushNotification Object

Raises:

  • App42Exception


417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/push/PushNotificationService.rb', line 417

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

#send_push_message_to_channel(channel, message) ⇒ Object

send push message to channel containing string

Parameters:

  • channel
    • channel name which you want to send the message

  • message
    • push message in string format

Returns:

  • PushNotification Object

Raises:

  • App42Exception


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/push/PushNotificationService.rb', line 267

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_user(userName, message) ⇒ Object

Send Push Message To paticular user in string format

Parameters:

  • username
    • username which you want to send the message

  • message
    • push message

Returns:

  • PushNotification Object

Raises:

  • App42Exception


367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/push/PushNotificationService.rb', line 367

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

#store_device_token(userName, deviceToken, type) ⇒ Object

Stores your device token on server with particular username

Parameters:

  • Username
    • username with which you want your device to be registered

  • deviceToken
    • device id for android phones

  • type

    -

Returns:

  • PushNotification Object

Raises:

  • App42Exception


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/push/PushNotificationService.rb', line 59

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

#subscribe_to_channel(channel, userName) ⇒ Object

Subscribe to the channel

Parameters:

  • channel
    • the channel name which you want to subscribe

  • userName
    • username which want to subscribe

Returns:

  • PushNotification Object

Raises:

  • App42Exception


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/push/PushNotificationService.rb', line 166

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_channel(channel, userName) ⇒ Object

Unsubscribe from particular channel

Parameters:

  • channel
    • channel name which you want to unsubscribe

  • userName
    • username which want to unsubscribe

Returns:

  • PushNotification Object

Raises:

  • App42Exception


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/push/PushNotificationService.rb', line 216

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